How can we find out the size of a java object??
Example:
class Person{
String name;
int age;
public Person(String n, int a){
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.
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:
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*yMaybe 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...
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.
You can use the sizeof4j library to calculate the size of any object in java like SizeOf.shallowSize(new Integer(2))
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.
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