6

We are cbse 12th grade ip students.. we are facing this exception and we do not know how to rectify this exception.

run:
Exception in thread "Image Fetcher 1" java.lang.OutOfMemoryError: Java heap space
        at java.awt.image.DataBufferInt.<init>(DataBufferInt.java:41)
        at java.awt.image.Raster.createPackedRaster(Raster.java:458)
        at java.awt.image.DirectColorModel.createCompatibleWritableRaster(DirectColorModel.java:1015)
        at sun.awt.image.ImageRepresentation.createBufferedImage(ImageRepresentation.java:230)
        at sun.awt.image.ImageRepresentation.setPixels(ImageRepresentation.java:484)
        at sun.awt.image.ImageDecoder.setPixels(ImageDecoder.java:120)
        at sun.awt.image.JPEGImageDecoder.sendPixels(JPEGImageDecoder.java:97)
        at sun.awt.image.JPEGImageDecoder.readImage(Native Method)
        at sun.awt.image.JPEGImageDecoder.produceImage(JPEGImageDecoder.java:119)
        at sun.awt.image.InputStreamImageSource.doFetch(InputStreamImageSource.java:246)
        at sun.awt.image.ImageFetcher.fetchloop(ImageFetcher.java:172)
        at sun.awt.image.ImageFetcher.run(ImageFetcher.java:136)
Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
        at java.awt.image.DataBufferInt.<init>(DataBufferInt.java:41)
        at java.awt.image.Raster.createPackedRaster(Raster.java:458)
        at java.awt.image.DirectColorModel.createCompatibleWritableRaster(DirectColorModel.java:1015)
        at sun.awt.image.SunVolatileImage.getBackupImage(SunVolatileImage.java:225)
        at sun.awt.image.VolatileSurfaceManager.getBackupSurface(VolatileSurfaceManager.java:252)
        at sun.awt.image.VolatileSurfaceManager.initialize(VolatileSurfaceManager.java:108)
        at sun.awt.image.SunVolatileImage.<init>(SunVolatileImage.java:72)
        at sun.awt.image.SunVolatileImage.<init>(SunVolatileImage.java:82)
        at sun.awt.image.SunVolatileImage.<init>(SunVolatileImage.java:93)
        at sun.awt.image.SunVolatileImage.<init>(SunVolatileImage.java:87)
        at sun.awt.windows.WComponentPeer.createVolatileImage(WComponentPeer.java:604)
        at java.awt.Component.createVolatileImage(Component.java:3316)
        at java.awt.Component$BltBufferStrategy.createBackBuffers(Component.java:4038)
        at java.awt.Component$BltBufferStrategy.<init>(Component.java:3983)
        at java.awt.Component$BltSubRegionBufferStrategy.<init>(Component.java:4236)
        at java.awt.Component.createBufferStrategy(Component.java:3567)
        at java.awt.Window.createBufferStrategy(Window.java:3012)
        at javax.swing.BufferStrategyPaintManager$BufferInfo.createBufferStrategy(BufferStrategyPaintManager.java:844)
        at javax.swing.BufferStrategyPaintManager$BufferInfo.createBufferStrategy(BufferStrategyPaintManager.java:795)
        at javax.swing.BufferStrategyPaintManager$BufferInfo.getBufferStrategy(BufferStrategyPaintManager.java:728)
        at javax.swing.BufferStrategyPaintManager.prepare(BufferStrategyPaintManager.java:505)
        at javax.swing.BufferStrategyPaintManager.paint(BufferStrategyPaintManager.java:263)
        at javax.swing.RepaintManager.paint(RepaintManager.java:1217)
        at javax.swing.JComponent.paint(JComponent.java:1013)
        at java.awt.GraphicsCallback$PaintCallback.run(GraphicsCallback.java:21)
        at sun.awt.SunGraphicsCallback.runOneComponent(SunGraphicsCallback.java:60)
        at sun.awt.SunGraphicsCallback.runComponents(SunGraphicsCallback.java:97)
        at java.awt.Container.paint(Container.java:1780)
        at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:814)
        at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:714)
        at javax.swing.RepaintManager.seqPaintDirtyRegions(RepaintManager.java:694)
        at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(SystemEventQueueUtilities.java:128)
Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
        at java.awt.image.DataBufferInt.<init>(DataBufferInt.java:41)
        at java.awt.image.Raster.createPackedRaster(Raster.java:458)
        at java.awt.image.DirectColorModel.createCompatibleWritableRaster(DirectColorModel.java:1015)
        at sun.awt.image.SunVolatileImage.getBackupImage(SunVolatileImage.java:225)
        at sun.awt.image.VolatileSurfaceManager.getBackupSurface(VolatileSurfaceManager.java:252)
        at sun.awt.image.VolatileSurfaceManager.initialize(VolatileSurfaceManager.java:108)
        at sun.awt.image.SunVolatileImage.<init>(SunVolatileImage.java:72)
        at sun.awt.image.SunVolatileImage.<init>(SunVolatileImage.java:82)
        at sun.awt.image.SunVolatileImage.<init>(SunVolatileImage.java:93)
        at sun.awt.image.SunVolatileImage.<init>(SunVolatileImage.java:87)
        at sun.awt.windows.WComponentPeer.createVolatileImage(WComponentPeer.java:604)
        at java.awt.Component.createVolatileImage(Component.java:3316)
        at java.awt.Component$BltBufferStrategy.createBackBuffers(Component.java:4038)
        at java.awt.Component$BltBufferStrategy.<init>(Component.java:3983)
        at java.awt.Component$BltSubRegionBufferStrategy.<init>(Component.java:4236)
        at java.awt.Component.createBufferStrategy(Component.java:3567)
        at java.awt.Window.createBufferStrategy(Window.java:3012)
        at javax.swing.BufferStrategyPaintManager$BufferInfo.createBufferStrategy(BufferStrategyPaintManager.java:844)
        at javax.swing.BufferStrategyPaintManager$BufferInfo.createBufferStrategy(BufferStrategyPaintManager.java:795)
        at javax.swing.BufferStrategyPaintManager$BufferInfo.getBufferStrategy(BufferStrategyPaintManager.java:728)
        at javax.swing.BufferStrategyPaintManager.prepare(BufferStrategyPaintManager.java:505)
        at javax.swing.BufferStrategyPaintManager.paint(BufferStrategyPaintManager.java:263)
        at javax.swing.RepaintManager.paint(RepaintManager.java:1217)
        at javax.swing.JComponent._paintImmediately(JComponent.java:5070)
        at javax.swing.JComponent.paintImmediately(JComponent.java:4880)
        at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:803)
        at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:714)
        at javax.swing.RepaintManager.seqPaintDirtyRegions(RepaintManager.java:694)
        at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(SystemEventQueueUtilities.java:128)
        at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:597)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
        at java.awt.image.DataBufferInt.<init>(DataBufferInt.java:41)
        at java.awt.image.Raster.createPackedRaster(Raster.java:458)
        at java.awt.image.DirectColorModel.createCompatibleWritableRaster(DirectColorModel.java:1015)
        at sun.awt.image.SunVolatileImage.getBackupImage(SunVolatileImage.java:225)
        at sun.awt.image.VolatileSurfaceManager.getBackupSurface(VolatileSurfaceManager.java:252)
        at sun.awt.image.VolatileSurfaceManager.initialize(VolatileSurfaceManager.java:108)
        at sun.awt.image.SunVolatileImage.<init>(SunVolatileImage.java:72)
        at sun.awt.image.SunVolatileImage.<init>(SunVolatileImage.java:82)
        at sun.awt.image.SunVolatileImage.<init>(SunVolatileImage.java:93)
        at sun.awt.image.SunVolatileImage.<init>(SunVolatileImage.java:87)
        at sun.awt.windows.WComponentPeer.createVolatileImage(WComponentPeer.java:604)
        at java.awt.Component.createVolatileImage(Component.java:3316)
        at java.awt.Component$BltBufferStrategy.createBackBuffers(Component.java:4038)
        at java.awt.Component$BltBufferStrategy.<init>(Component.java:3983)
        at java.awt.Component$BltSubRegionBufferStrategy.<init>(Component.java:4236)
        at java.awt.Component.createBufferStrategy(Component.java:3567)
        at java.awt.Window.createBufferStrategy(Window.java:3012)
        at javax.swing.BufferStrategyPaintManager$BufferInfo.createBufferStrategy(BufferStrategyPaintManager.java:844)
        at javax.swing.BufferStrategyPaintManager$BufferInfo.createBufferStrategy(BufferStrategyPaintManager.java:795)
        at javax.swing.BufferStrategyPaintManager$BufferInfo.getBufferStrategy(BufferStrategyPaintManager.java:728)
        at javax.swing.BufferStrategyPaintManager.prepare(BufferStrategyPaintManager.java:505)
        at javax.swing.BufferStrategyPaintManager.paint(BufferStrategyPaintManager.java:263)
        at javax.swing.RepaintManager.paint(RepaintManager.java:1217)
        at javax.swing.JComponent._paintImmediately(JComponent.java:5070)
        at javax.swing.JComponent.paintImmediately(JComponent.java:4880)
        at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:803)
        at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:714)
        at javax.swing.RepaintManager.seqPaintDirtyRegions(RepaintManager.java:694)
        at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(SystemEventQueueUtilities.java:128)
        at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:597)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
BUILD SUCCESSFUL (total time: 25 seconds)
Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
user424845
  • 61
  • 1
  • 2
  • Its not clear from the stack trace whats causing this (e.g. no user classes present in the stack trace). Is there more stack trace available? Whats causing this? – naikus Aug 19 '10 at 06:26

5 Answers5

8

Increase the heap space. Java uses 64MByte heap memory if you don't specify a higher maximum. Start your application like this:

java -Xmx128m MyClass

to have 128MByte.


It look like you start the application within an ant build. I would separate building and execution. Let ant create a batch file which will be used to start the application.

Now you may have to increase heap space for ant itself. This is done with an environment variable (solution shown for windows):

set ANT_OPTS=-Xms512m -Xmx512m

Further Reading

Andreas Dolk
  • 113,398
  • 19
  • 180
  • 268
  • The default heap size depends on your JVM version, your JVM arguments and (in some cases) how much "physical" memory your "machine" has. – Stephen C Aug 19 '10 at 06:47
  • Indeed, the -X switches are JVM specific and may vary from JVM to JVM; the heap control switch shown is for the Sun (now Oracle) JVM, though in practice all JVMs are likely to use the same switch. – Lawrence Dol Aug 19 '10 at 08:09
  • OK this did fix it for me. Unfortunately it was using MAVEN_OPTS which still didn't work since I was using surefire in "forking" mode which apparently doesn't use MAVEN_OPTS by default, but specifying them and finally this particular heap failure is gone http://stackoverflow.com/questions/4066424/java-lang-outofmemoryerror-java-heap-space-in-maven – rogerdpack Apr 20 '15 at 13:59
2

You can set Heap size using following options:

  -Xms32m -Xmx128m

check VM options here :

http://www.oracle.com/technetwork/java/javase/tech/vmoptions-jsp-140102.html

But I will suggest you to check whats consuming up your heap. You can do it using profiling tools like :

YoK
  • 14,329
  • 4
  • 49
  • 67
1

The other answers are not full and complete answers. They are merely band-aids. The root cause is because you are creating an ImageIcon object, which creates those threads in the background. I had this problem also, due to corrupt JPGs. It doesn't matter if you increase the memory size to 1 terabyte, if a user uploads a corrupt image it could take everything down. Instead, use ImageIO and catch the out of memory error around the creation:

try { // catch OutOfMemoryError for corrupt JPG
    BufferedImage inImage = ImageIO.read(new File(jpgAbsPath));
} catch (OutOfMemoryError oome) {
    logger.log(Level.SEVERE, "Out of memory error when trying to read image " + jpgAbsPath +" .", oome);
    return;
}

See here for more: http://www.coderanch.com/t/376018/java/java/Image-Fetcher-Threads http://docs.oracle.com/javase/tutorial/2d/images/loadimage.html

Chloe
  • 25,162
  • 40
  • 190
  • 357
0

Your JVM is running out of heap space. Try using the -Xmx switch to the JVM to increase the amount of heap space available to your program.

java -Xmx256m YourClassName 
Chinmay Kanchi
  • 62,729
  • 22
  • 87
  • 114
0

By default, without modifiyin JVM start params, you have 128m of heap space.

The memory allocated by your program is greater than this 128m.

You can increase your heap space limits:

java -Xms<initial heap size> -Xmx<maximum heap size>
sourcerebels
  • 5,140
  • 1
  • 32
  • 52