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
Peter Lawrey
source share