Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sizeof Java object [duplicate]

Tags:

java

sizeof

How can we find out the size of a Java object?

Example:

class Person{
    String name;  
    int age;  

    public Person(String n, int a){  
        name = n;  
        age = a;  
    }
}  

Person person = new Person("Andy", 30);  

How can I know the size of person object?

like image 953
daydreamer Avatar asked Nov 06 '10 21:11

daydreamer


People also ask

What is duplicate object in Java?

To summarize, duplicate objects, i.e. multiple instances of the same class with identical contents, can become a burden in a Java app. They may waste a lot of memory and/or increase the GC pressure. The best way to measure the impact of such objects is to obtain a heap dump and use a tool like JXRay to analyze it.

What is the size of object in Java?

Minimum object size is 16 bytes for modern 64-bit JDK since the object has 12-byte header, padded to a multiple of 8 bytes. In 32-bit JDK, the overhead is 8 bytes, padded to a multiple of 4 bytes.

Can set have duplicate objects in Java?

A Set is a Collection that cannot contain duplicate elements. It models the mathematical set abstraction. The Set interface contains only methods inherited from Collection and adds the restriction that duplicate elements are prohibited.

Can we use sizeof in Java?

Similarly, there is no sizeof() operator in Java. All primitive values have a predefined size, like int is 4 bytes, char is 2 byte, short is 2 byte, long and float is 8 byte, and so on.


2 Answers

The question is not meaningful, at least not without further context.

The notion of "size" in Java is only reasonably well defined for primitives: A byte is 8 bit (unsurprisingly) an int is 32 bit, a long 64bit, etc. (see e.g. http://download.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html for a full list).

For object instances, it's more complicated, because:

  • Object instances can (and usually will) contain references to other instances internally, so you must decide whether to count these dependent instances, and how. What if several instances share a dependency?
  • Sometimes, object instances may be reused (e.g. interning of java.lang.String, see http://en.wikipedia.org/wiki/String_interning ). So if you use x objects of size y, the total size may be smaller than x*y
  • The JVM has a lot of leeway about how to implement objects and instances internally. It may use different techniques for different instances (e.g. sharing internal data structures), so there may not even be a meaningful "size" to assign to a single object.

Maybe you could explain why you are interested in object sizes.

There are some rules of thumb for estimating the heap memory used by instances (e.g. in the Sun JVM, a java.lang.Object instance uses 8 byte), but these will depend on the JVM you use.

Generally, if you want to know about your heap usage, use a memory / heap profiler.

Edit:

Well, there is (as of JDK 6) a way to get an approximation of the amount of memory used by an object: http://download.oracle.com/javase/6/docs/api/java/lang/instrument/Instrumentation.html#getObjectSize%28java.lang.Object%29

It's still only an approximation...

like image 179
sleske Avatar answered Sep 16 '22 21:09

sleske


I think you can get something that might help you if you do something like this:

    /* First trigger classloading */
    MyObject delegate = new MyObject();

    Runtime.getRuntime().gc();

    long before = Runtime.getRuntime().freeMemory();
    MyObject delegate2 = new MyObject();
    long after = Runtime.getRuntime().freeMemory();

    System.out.println("Memory used:"+(before-after));

This will hopefully give you the answer you want, or at least an approximation.

like image 36
Knubo Avatar answered Sep 20 '22 21:09

Knubo