I’ve searched about the size of a java object for a long time, there are a lot of answers like this, everyone is telling me that the size of the overhead of a java object, and how to calculate out the actual size. But how do they know that? I did not find any evidence from the official oracle documents. What is the evidence for that conclusion? Or the data was just come from some guesses based on some experiments?
Another thing. It is mentioned in the official document that there is a ‘approximative’ way to measure the object – the Instrumentation way, can anybody explain to me what is the ‘approximately’ means? When it is accurate, when it is not. Better to have the evidence.
from experience.
Its up to the JVM. For OpenJDK based JVMs, 32-bit JVM has a different header size to 64-bit JVM as the header contains a reference. Other JVMs could be different again.
Essentially, yes.
When you measure the size of an object it can means many different things
Given you can have many difference answers depending on what you need to know, it is useful to have one number which is approximately close to all of these which you use for calculations.
Where you get a problem using Runtime is that the TLAB allocated data in large blocks. These large blocks can be further allocated in a multi-thread way. The downside is you don’t get accurate memory used information.
run without options
Turn off the TLAB and you see with
-XX:-UseTLABThis is much higher than you might expect because the class itself had to be loaded. If you create one instance of a Float first by adding to the start
you get