title: java object array size
body: <p>When calculate memory size of an object array, following code gives <strong>"used 24 bytes"</strong> as expected, which, as far as I know, consists of:</p>
  
  <p><code>4bytes(element pointer)+16bytes(object header)+4bytes(element space) = 24bytes</code></p>
  
  <pre><code>// with JVM argument -XX:-UseTLAB
  public static void main(String[] args) {
      long size = memoryUsed();
      Object[] o = new Object[]{1};
      //Object[] o = new Object[]{1L};
      size = memoryUsed() - size;
      System.out.printf("used %,d bytes%n", size);
      //Output: used 24 bytes
  }
  
  public static long memoryUsed() {
      return Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
  }
  </code></pre>
  
  <p>But when element type changed to Long (1L), the result is confusing, most of the time it's <strong>"used 9,264 bytes"</strong>, anyone can help enlighten me? what is the difference in memory allocation between this two element type?</p>
  
  <pre><code>// with JVM argument -XX:-UseTLAB
  public static void main(String[] args) {
      long size = memoryUsed();
      //Object[] o = new Object[]{1};
      Object[] o = new Object[]{1L};
      size = memoryUsed() - size;
      System.out.printf("used %,d bytes%n", size);
      //Output: used 9,264 bytes
  }
  </code></pre>