Java: how to avoid java.lang.OutOfMemoryError: Java heap space at long run

Asked by Anirban

***** the problem comes up, using the following loop:

int count = 1;
Screen s;
while (true) {
 s = new Screen();
 System.out.println("Count = "+ count++);
 s.click("start.png", 0);
}

this seems to bind some internal objects, so that GC is not able to free heap memory.

Since this is generally not "good programming" (no need for a new Screen object at each loop turn), the recommended approach does not fill up the heap:

int count = 1;
Screen s = new Screen();
while (true) {
 System.out.println("Count = "+ count++);
 s.click("start.png", 0);
}

----------------------------------------------------------------------------

I am running sikuli script from Java program in Win XP 32 bit platform. While running the same script iteratively for long time I am getting the "OutOfMemory" error which I suspect is because of the memory leak problem of sikuli.

While running a simple script, I set the heap space of JVM to 1GB. With that, a simple script having 4 calls to API "click" and one call to API "exists", ran for around 2 hours and gave the following exception:

Exception in thread "main" java.lang.reflect.InvocationTargetException
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoa
der.java:58)
Caused by: 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(Unknow
n Source)
        at java.awt.image.BufferedImage.<init>(Unknown Source)
        at sun.java2d.loops.GraphicsPrimitive.convertFrom(Unknown Source)
        at sun.java2d.loops.GraphicsPrimitive.convertFrom(Unknown Source)
        at sun.java2d.loops.MaskBlit$General.MaskBlit(Unknown Source)
        at sun.java2d.loops.Blit$GeneralMaskBlit.Blit(Unknown Source)
        at sun.java2d.pipe.DrawImage.blitSurfaceData(Unknown Source)
        at sun.java2d.pipe.DrawImage.renderImageCopy(Unknown Source)
        at sun.java2d.pipe.DrawImage.copyImage(Unknown Source)
        at sun.java2d.pipe.DrawImage.copyImage(Unknown Source)
        at sun.java2d.pipe.ValidatePipe.copyImage(Unknown Source)
        at sun.java2d.SunGraphics2D.drawImage(Unknown Source)
        at sun.java2d.SunGraphics2D.drawImage(Unknown Source)
        at org.sikuli.script.OpenCV.convertBufferedImageToByteArray(OpenCV.java:
35)
        at org.sikuli.script.OpenCV.convertBufferedImageToMat(OpenCV.java:41)
        at org.sikuli.script.Finder.<init>(Finder.java:70)
        at org.sikuli.script.Region.doFind(Region.java:965)
        at org.sikuli.script.Region$RepeatableFind.run(Region.java:1168)
        at org.sikuli.script.Region$Repeatable.repeat(Region.java:1139)
        at org.sikuli.script.Region.wait(Region.java:453)
        at org.sikuli.script.Region.find(Region.java:333)
        at org.sikuli.script.Region.getLocationFromPSRML(Region.java:1065)
        at org.sikuli.script.Region.click(Region.java:562)

Question information

Language:
English Edit question
Status:
Solved
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:
Revision history for this message
RaiMan (raimund-hocke) said :
#1

this not a real bug.

The situation can be avoided, when respecting the general rules to avoid memory leaks.

Revision history for this message
RaiMan (raimund-hocke) said :
#2

workaround available