Thursday, December 16, 2010

The largest dimension of a prefuse Display

It is an interesting finding to answer the question in the title.

Today, I try put my software to its edge. I create a Display with W=1024 and H=180,000 pixels. Then an OutOfMemeoryException was thrown.

At first, I thought the data is too big to fit in the main memory. So I checked the memory usage, which showed that I didn't reach the maximum memory, and only 60% of the max memory was used. Then I tried to google the origins of the OutOfMemoryException. Some similar links popped up. It turns out to be an issue about the largest size of a BufferedImage that a JVM can located. Here is my findings.
  • In Prefuse, Display is the "canvas" allowing a Visualization to paint on. Its source codes show that, a Display uses a BufferedImage to create the Graphics2D for Visualizations.
  • So how large can a BufferedImage be? A BufferedImage is a combination of a 2D int array, which indicate the X-Y coordination, and a 1D of colors, which indicate the color in each X-Y pixels. Therefore,

the size of a BufferedImage = int[][] * a color

  • Theoretically, the largest size of an int[][] = the largest size of an int[], which Max2D array = Max1D array = 2^32B in JAVA. An integer will take 32 bits = 4 Bytes.
  • A color in Java2D is a 32 bits number, with each Byte represent 0-255, for RGB color and a an alpha value for transparency. So a color takes 32 bits = 4Bytes;
OK, now it is clear, the Max size (Dimension) of a BufferedImage (2^32 Bytes/4)/4Bytes = 2^27 for W*H. If we have width as 1024, the max height = 2^17 = 131072.

Notices: the Max2D array could be limited by the VM in your machine and the max memory you located for your application. So the final max dimension of a display could vary from one machine to another machine.

No comments:

Post a Comment