Meet OutOfMemoryError with JAVA

Asked by Bob

When we try to prepare the test case script with java through the sikuli-script.jar, we found that we may meet java.lang.OutOfMemoryError sometimes. If we run the test case one by one in just one static main function, it always occurs this error around the 8th test case. Here is the detail error message as below:
Exception in thread "main" 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 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:1029)
 at org.sikuli.script.Region$RepeatableFind.run(Region.java:1232)
 at org.sikuli.script.Region$Repeatable.repeat(Region.java:1203)
 at org.sikuli.script.Region.wait(Region.java:497)
We think it is caused by the image resource which is used in the previous test cases. I just wonder that if the sikuli API has prepared some methods so that we can use them to release the image resource used before. Or could you please kindly geive us some other suggestions? Thank you very much for your help.

Question information

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

Yes it is true, that the error is produced when Sikuli convertes an image to the representation, that is used for matching in the native code of OpenCV.

But this is only a symptom, since these are the most memory consuming operations, that could temporarily add some Megabytes to the heap. But all these possible internal memory leaks are fixed meanwhile. If your program is written in a way. that your images can be garbage collected when necessary, this should not give any problems.

So check your code about holding images longer as needed resulting from capture operations like
image = Screen.capture();

If you do not need this image any more, this should help, to let the stuff get garbage collected.
image = null;

Revision history for this message
Bob (bwq-q) said :
#2

Got it. Thanks very much for your help.
That means we should do two operations to optimize the project:
First we should change the configuration file to give JVM more memory. But this is just used as prevention.
The most import one is that we need to check the source code to set the objects as null if we do not need them any more. So that the GC can help on releasing the image resource.
Am I right? Could you please kindly let me know if there is any misunderstanding?
Thank you very much. It is very kind of you to give me the suggestion so fast.

Revision history for this message
Bob (bwq-q) said :
#3

Thanks RaiMan, that solved my question.

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

yes, that's it.