I am trying to determine how many heaps any given TYPE_INT_ARGB BufferedImage will use so that for a program that does some image I can set a reasonable maximum heap based on the size of the image that we feed it.
As a test, I wrote the following program, which I then used to determine the smallest maximum heap at which it would work without OutOfMemoryError :
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) >> 20); } }
(The printed value is the expected size of the int[] in which the BufferedImage pixel data is stored.) I expected to find that the required maximum heap is something like x + c , where x is the size of the data array and c is a constant consisting from the sizes of the loaded classes, the BufferedImage object, etc. This is what I found instead (all values are in MB):
4 * w * h min max heap
----- ------------
5 -
10 15
20 31
40 61
80 121
160,241
1.5x well suited for observation. (Note that I did not find a minimum for a 5 MB image.) I do not understand what I see. What are these extra bytes?
source share