Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<blockquote> <p>how to calculate out the actual size. But how do they know that?</p> </blockquote> <p>from experience.</p> <blockquote> <p>I did not find any evidence from the official oracle documents.</p> </blockquote> <p>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.</p> <blockquote> <p>Or the data was just come from some guesses based on some experiments? </p> </blockquote> <p>Essentially, yes.</p> <blockquote> <p>can anybody explain to me what is the 'approximately' means?</p> </blockquote> <p>When you measure the size of an object it can means many different things</p> <ul> <li>How big is the object? (Shallow depth)</li> <li>How much memory does it use? (Object allocation is 8-byte aligned i.e. always a multiple of 8)</li> <li>How much space does the object and all the objects referenced use? (Deep depth)</li> <li>How much space might be freed if it were discarded? (how many objects are shared and does it appear in the middle of two fragments of freed memory)</li> <li>Do you count the space used on the stack, or space used in off heap memory?</li> </ul> <p>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.</p> <hr> <p>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.</p> <pre><code>static long memTaken() { final Runtime rt = Runtime.getRuntime(); return rt.totalMemory() - rt.freeMemory(); } public static void main(String... args) { long used1 = memTaken(); Float i = new Float(0); long used2 = memTaken(); System.out.println("new Float(0) used "+(used2 - used1)+" bytes."); } </code></pre> <p>run without options</p> <pre><code>new Float(0) used 0 bytes. </code></pre> <p>Turn off the TLAB and you see with <code>-XX:-UseTLAB</code></p> <pre><code>new Float(0) used 336 bytes. </code></pre> <p>This 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</p> <pre><code>Float j = new Float(1); </code></pre> <p>you get</p> <pre><code>new Float(0) used 16 bytes </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload