본문 바로가기

JavaCV/Memory leaks

Memory leaks problems

Reported by kdo...@gmail.comDec 13, 2011
What steps will reproduce the problem?
1. about 1000 images
2. run this function with getBufferedImage only (normalImage is global, it released and all about it is ok)

What is the expected output? What do you see instead?
I have 8G memory, the java program (with this function only) gradually consume 3Gb (it's like 300m-700m-400m-800m) and even after the program finished running successfully, the 3GB memory is still occupied (not by Java)

What version of the product are you using? On what operating system?
Ubuntu 10.04, OpenCV 2.3.1 without python and android support, jdk 1.6, Xmx1024m, Xms256m

Please provide any additional information below.

Reading images is ok, but this function have memory leak (normalImage is global, it released later in another function, there is no trouble with it):

    public void someFunction()
    {
        BufferedImage image = null;

        try
        {
            image = normalImage.getBufferedImage();
        }
        finally
        {
            if(image != null)
            {
                image.flush();
                image = null;
            }

        }
    }

What am I doing wrong? I have this trouble always when I use getBufferedImage in my functions. Thank you.
Comment 1 by project member samuel.a...@gmail.comDec 29, 2011
I observe no memory leak at all when running this loop:
        normalImage = cvLoadImage("lena.jpg");
        for (int i = 0; i < 1000000; i++) {
            someFunction();
        }
where "lena.jpg" is a file that comes with OpenCV.

My Java:
OpenJDK Runtime Environment (IcedTea6 1.9.10) (fedora-55.1.9.10.fc14-x86_64)
OpenJDK 64-Bit Server VM (build 19.0-b09, mixed mode)
Comment 3 by vlada...@gmail.comJan 14, 2012

I was able to reproduce memory leak with following code. I am not sure if this is expected behavior of JNI but using cvReleaseImage() fixes the problem. Assigned variables within functions are not garbage collected. public static void main(String[] args) { for (int i = 0; i < 100000; i++) { IplImage someImage = getImage(); // causes memory leak // cvReleaseImage(someImage); // fixes memory leak } } public static IplImage getImage() { // IplImage normalImage = cvLoadImage("bg.bmp"); IplImage normalImage = cvCreateImage(cvSize(320, 240), IPL_DEPTH_8U, 3); return normalImage; }

Comment 4 by project member samuel.a...@gmail.comJan 24, 2012
Yes that's normal, cvCreateImage() is nothing more than a normal C function. Use the IplImage.create() factory method if you want garbage collection behavior.
Comment 5 by project member samuel.a...@gmail.comFeb 18, 2012
Judging by the lack of feedback, I assume the issue has been resolved. Please let me know if this is not the case.
Status: Done