johnson-pkt / javacv

Automatically exported from code.google.com/p/javacv
GNU General Public License v2.0
0 stars 0 forks source link

Memory leak in getBufferedImage #138

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
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.

Original issue reported on code.google.com by kdo...@gmail.com on 13 Dec 2011 at 11:09

GoogleCodeExporter commented 8 years ago
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)

Original comment by samuel.a...@gmail.com on 29 Dec 2011 at 10:10

GoogleCodeExporter commented 8 years ago
[deleted comment]
GoogleCodeExporter commented 8 years ago
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;
    }

Original comment by vlada...@gmail.com on 14 Jan 2012 at 1:48

GoogleCodeExporter commented 8 years ago
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.

Original comment by samuel.a...@gmail.com on 24 Jan 2012 at 11:00

GoogleCodeExporter commented 8 years ago
Judging by the lack of feedback, I assume the issue has been resolved. Please 
let me know if this is not the case.

Original comment by samuel.a...@gmail.com on 19 Feb 2012 at 4:47