This site is currently read-only as we are migrating to Oracle Forums for an improved community experience. You will not be able to initiate activity until January 31st, when you will be able to use this site as normal.

    Forum Stats

  • 3,890,899 Users
  • 2,269,649 Discussions
  • 7,916,821 Comments

Discussions

OutOfMemory error in java.awt.image.DataBufferInt.<init>

843807
843807 Member Posts: 46,582
edited Jul 22, 2010 11:43PM in Abstract Window Toolkit (AWT)
We have an applet application that performs Print Preview of the images in the canvas. The images are like a network of entities (it has pictures of the entities involve (let's say Person) and how it links to other entities). We are using IE to launch the applet.

We set min heap space to 128MB, JVM max heap space to 256MB, java plugin max heap space to 256MB using the Control Panel > Java.

When the canvas width is about 54860 and height is 1644 and perform Print Preview, it thows an OutOfMemoryError in java.awt.image.DataBufferInt.<int>, hence, the Print Preview page is not shown. The complete stack trace (and logs) is as follows:
Width: 54860 H: 1644
Max heap: 254 # using Runtime.getRuntime().maxMemory()
javaplugin.maxHeapSize: 256M # using System.getProperties("javaplugin.maxHeapSize")
n page x n page : 1x1
Exception in thread "AWT-EventQueue-2" java.lang.OutOfMemoryError: Java heap space
at java.awt.image.DataBufferInt.<init>(Unknown Source)
at java.awt.image.Raster.createPackedRaster(Unknown Source)
at java.awt.image.DirectColorModel.createCompatibleWritableRaster(Unknown Source)
at java.awt.image.BufferedImage.<init>(Unknown Source)
at com.azeus.gdi.chart.GDIChart.preparePreview(GDIChart.java:731)
at com.azeus.gdi.chart.GDIChart.getPreview(GDIChart.java:893)
at com.azeus.gdi.ui.GDIUserInterface.printPreviewOp(GDIUserInterface.java:1526)
at com.azeus.gdi.ui.GDIUserInterface$21.actionPerformed(GDIUserInterface.java:1438)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

Drilling down the cause of the problem. The OutOfMemory occurred in the constructor of DataBufferInt when it tried to create an int array:
public DataBufferInt(int size) {
super(STABLE, TYPE_INT, size);
data = new int[size]; # this part produce out of memory error when size = width X height
bankdata = new int[1][];
bankdata[0] = data;
}

The OutOfMemory error occurred when size is width * height (54860 X 1644) which is 90,189,840 bytes (~86MB).

I can replicate the OutOfMemory error when initiating an int array using a test class when it uses the default max heap space but if I increase the heap space to 256MB, it cannot be replicated in the test class.

Using a smaller width and height with product not exceeding 64MB, the applet can perform Print Preview successfully.

Given this, I think the java applet is not using the value assigned in javaplugin.maxHeapSize to set the max heap space, hence, it still uses the default max heap size and throws OutOfMemory in int array when size exceeds the default max heap space which is 64MB.

For additional information, below is some of the java properties (when press S in java applet console):
browser = sun.plugin
browser.vendor = Sun Microsystems, Inc.
browser.version = 1.1
java.awt.graphicsenv = sun.awt.Win32GraphicsEnvironment
java.awt.printerjob = sun.awt.windows.WPrinterJob
java.class.path = C:\PROGRA~1\Java\jre6\classes
java.class.version = 50.0
java.class.version.applet = true
java.runtime.name = Java(TM) SE Runtime Environment
java.runtime.version = 1.6.0_17-b04
java.specification.version = 1.6
java.vendor.applet = true
java.version = 1.6.0_17
java.version.applet = true
javaplugin.maxHeapSpace = 256M
javaplugin.nodotversion = 160_17
javaplugin.version = 1.6.0_17
javaplugin.vm.options = -Xms128M -Djavaplugin.maxHeapSpace=256M -Xmx256m -Xms128M
javawebstart.version = javaws-1.6.0_17

Kindly advise if this is a bug in JRE or wrong setting. If wrong setting, please advise on the proper way to set the heap space to prevent OutOfMemory in initializing int array.

Thanks a lot.

Edited by: rei_xanther on Jun 28, 2010 12:01 AM

Edited by: rei_xanther on Jun 28, 2010 12:37 AM

Comments

  • User_64CKJ
    User_64CKJ Member Posts: 7,279 Silver Badge
    From [Primitive Data Types|http://java.sun.com/docs/books/tutorial/java/nutsandbolts/datatypes.html]
    int: The int data type is a *32-bit* signed two's complement integer.
  • 843807
    843807 Member Posts: 46,582
    Thanks, Andrew.

    But the maximum value of the int data type is 2,147,483,647. The value that I passed in the int array size is only 90,189,840.

    new int[size] -- size is 90,189,840

    I assumed that one element in the int array is 1 byte. Thus, it tries to allocate about 90,189,840 bytes or ~86MB.

    Thanks.
  • User_64CKJ
    User_64CKJ Member Posts: 7,279 Silver Badge
    rei_xanther wrote:
    ..But the maximum value of the int data type is 2,147,483,647.
    That is the maximum positive integer value that can be stored in (the 4 bytes of) a signed int, but..
    ..The value that I passed in the int array size is only 90,189,840.
    ..its only connection with RAM is that each int requires 4 bytes of memory to hold it.
    new int[size] -- size is 90,189,840
    Sure. So the number of bytes required to hold those 90,189,840 ints is 360,759,360.
    I assumed that one element in the int array is 1 byte. ..
    Your assumption is wrong. How could it be possible to store 32 bits (4 bytes) in 8 bits (1 byte)? (a)

    a) Short of some clever compression algorithm applied to the data.
  • 843807
    843807 Member Posts: 46,582
    Thanks Andrew for the information.

    I increased the java heap size (-Xmx) to 512M and javaplugin.maxHeapSize to 512M.

    Using the same data, I still encounter OutOfMemory error.

    Same logs I provided, the difference is the display of the heap space used:
    max heap: 508
    javaplugin.maxHeapSize: 512M

    Thanks.
  • 843807
    843807 Member Posts: 46,582
    We are able to resolve the issue by calling System.gc() manually before processing the Print Preview.

    But as far as I know, it is not recommended to call System.gc() in the codes.

    Thanks.
  • 843807
    843807 Member Posts: 46,582
    It seems that there are times JVM does not perform garbage collection, that's why there are some instances wherein the OutOfMemory error are replicated.

    Kindly advise. Thanks.
This discussion has been closed.