How to check how many bytes are used by an object reference in Java?

I would like to check how many bytes are used by an object reference in the Java virtual machine that I use. Do you guys know how to check this?

Thanks!

+8
java object reference memory byte
source share
4 answers

Taking the question literally, on most JVMs, all references to 32-bit JVMs take 4 bytes, one 64-bit JVM, the link takes 8 bytes if -XX: + UseCompressedOops is not used, in which case 4 -bytes are required.

I assume that you are asking how to determine how much space an object takes. You can use Instrumentation (not a simple matter), but this will give you only a small depth. Java, as a rule, breaks into many objects something that C ++ can be a single structure, so it is not so useful.

However, if you have a memory problem, I offer you a memory profiler. This will give you the opportunity to use shallow and deep space objects and give you a picture of the entire system. This is often more useful because you can start with the largest consumers and optimize them, even if you have been developing Java for ten years +, you will only guess where it is best to optimize if you do not have hard data.

Another way to get the size of an object, if you do not want to use the profiler, is to allocate a large array and see how much memory is consumed, you have to do this many times to get an idea of ​​what the average size is. I would set the young space very high to avoid GCs that confuse your results, for example. -XX:NewSize=1g

+10
source share

If you need to be accurate enough, check out the Tool Map .

+2
source share

It may differ from JVM to JVM, but "Size for Java" says

You may recall the "hint with text 130: do you know your data size?" which described a method based on creating a large number of identical class instances and carefully measuring the resulting JVM size increase. When applicable, this idea works very well, and I actually use it to download an alternative approach in this article.

+2
source share

This one is the one I use. Adored these 16-byte links! alphaworks.ibm.heapanalyzer

+1
source share

All Articles