开发者

sizeof Java object [duplicate]

开发者 https://www.devze.com 2023-01-23 15:55 出处:网络
This question already has answers here: In Java, wh开发者_开发技巧at is the best way to determine the size of an object?
This question already has answers here: In Java, wh开发者_开发技巧at is the best way to determine the size of an object? (28 answers) Closed 1 year ago.

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?


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...


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.


EhCache provides a SizeOf class that will try to use the Instrumentation agent and will fall back to a different approach if the agent is not loaded or cannot be loaded (details here).

Also see the agent from Heinz Kabutz.


You can use the sizeof4j library to calculate the size of any object in java like SizeOf.shallowSize(new Integer(2))


Objects. To determine the memory usage of an object, we add the amount of memory used by each instance variable to the overhead associated with each object, typically 16 bytes. The overhead includes a reference to the object’s class, garbage collection information, and synchronization information. Moreover, the memory usage is typically padded to be a multiple of 8 bytes (machine words, on a 64-bit machine). For example, an Integer object uses 24 bytes (16 bytes of overhead, 4 bytes for its int instance variable, and 4 bytes of padding)

Reference : http://www.amazon.com/Algorithms-4th-Edition-Robert-Sedgewick/dp/032157351X


Java has no built in sizeof operator.

You could try and use a serialization method, by serializing an object to memory and getting the size of that data, but that wouldn't necessarily be the size you want.

You could also have a method called sizeOf(), like this:

// returns the size of this object in bytes
int sizeOf()
{
     int size = 0;
     size += name.Length * 2; // each character in name is 2 bytes, no?
     size += 4; // for n, which is 32 bits = 4 bytes
}

Note that this implementation doesn't include the metadata inside name, only the number of bytes necessary to make the char[] for it.

0

精彩评论

暂无评论...
验证码 换一张
取 消