title: Calculate the memory usage of a HashMap in java
body: <p>I want to calculate the memory usage of two HashMaps in Java and I did these two methods. </p>
  
  <p>1) I use the <em>visualVM</em> and I got following result. </p>
  
  <p><strong>Is it just the size of the Hashmap's pointer not the real amount of memory usage with this Hashmap?</strong> </p>
  
  <p>Because, it just used ''67283648'' bytes.
  <a href="https://i.stack.imgur.com/8aeUY.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/8aeUY.png" alt="enter image description here"></a></p>
  
  <p>2) I run the following code which fill two HashMaps and I got ''576132056'' bytes as a result.</p>
  
  <pre><code>    Runtime runtime = Runtime.getRuntime();
      long memory = runtime.totalMemory()- runtime.freeMemory();
      HashMap&lt;String, TypePosting&gt; typeInvIndex=qpu.loadInvTypeIndex();
      HashMap&lt;Integer,String&gt; map=qpu.loadMapTypeEntityId();
      long memory1 = runtime.totalMemory()- runtime.freeMemory();
      System.out.println(memory1- memory);
  </code></pre>
  
  <p>I just asked this question to be sure that I the second result is the correct value of space usage of these two Hashmaps correctly. Am I correct?</p>