title: Why does a BufferedImage require so much memory beyond the size of its data array?
body: <p>I'm trying to determine how much heap any given TYPE_INT_ARGB <a href="http://download.oracle.com/javase/6/docs/api/java/awt/image/BufferedImage.html" rel="noreferrer"><code>BufferedImage</code></a> will use so that, for a program which is doing some image processing, I can set a reasonable max heap based on the size of image we feed it.</p>
  
  <p>I wrote the following program as a test, which I then used to determine the least maximum heap under which it would run without an <code>OutOfMemoryError</code>:</p>
  
  <pre><code>import java.awt.image.BufferedImage;
  
  public class Test {
    public static void main(String[] args) {
      final int w = Integer.parseInt(args[0]);
      final int h = Integer.parseInt(args[1]);
  
      final BufferedImage img =
        new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
  
      System.out.println((4*w*h) &gt;&gt; 20);
    }
  }
  </code></pre>
  
  <p>(The printed value is the expected size of the <code>int[]</code> in which the <code>BufferedImage</code>'s pixel data is stored.) What I expected to find was that the required max heap is something like <code>x + c</code>, where <code>x</code> is the size of the data array and <code>c</code> is a constant consisting of the sizes of the classes which are loaded, the <code>BufferedImage</code> object, etc. This is what I found instead (all values are in MB):</p>
  
  <pre>
  4*w*h   min max heap
  -----   ------------
    5          -
   10         15
   20         31
   40         61
   80        121
  160        241
  </pre>
  
  <p><code>1.5x</code> is a good fit for the observations. (Note that I found no minimum for the 5MB image.) I don't understand what I'm seeing. What are these extra bytes?</p>