Giter Club home page Giter Club logo

Comments (5)

GoogleCodeExporter avatar GoogleCodeExporter commented on July 30, 2024
You don't need to release it, just clear it with cvClearMemStorage() on every 
iteration 

Can you try to use it in the same way as in the Test2 sample provided in the 
README.txt file?

Original comment by [email protected] on 15 Jan 2011 at 1:39

from javacv.

GoogleCodeExporter avatar GoogleCodeExporter commented on July 30, 2024
Thanks for your reply. I already tried creating the memory store only once at 
the start of the program and then calling cvClearMemStorage after each 
iteration (as shown in Test2 from the README.txt file). The behaviour that I 
see is the same as in my memoryLeakExample above. i.e. The time per iteration 
remains constant but the memory taken by the process gradually increases...

I have created a stand alone test that doesn't rely on any external images to 
illustrate this. I would be grateful if you could have a look.

I have also tried checking running it against the latest JavaCV code from the 
SVN and I see the same behaviour. I am using the recommended version of JNA 
(3.2.7). 

Any help would be gratefully received.


import static com.googlecode.javacv.jna.cv.CV_HOUGH_STANDARD;
import static com.googlecode.javacv.jna.cv.cvCanny;
import static com.googlecode.javacv.jna.cv.cvHoughLines2;
import static com.googlecode.javacv.jna.cxcore.cvClearMemStorage;

import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;

import com.googlecode.javacv.JavaCvErrorCallback;
import com.googlecode.javacv.jna.cxcore.CvMemStorage;
import com.googlecode.javacv.jna.cxcore.CvSeq;
import com.googlecode.javacv.jna.cxcore.IplImage;

public class MemoryTest {

    // class to test creating and releasing images from JavaCV
    public static void main(String[] args) throws IOException,
            InterruptedException {

        new JavaCvErrorCallback().redirectError();

        // create a random image
        IplImage image = createRandomImage();
        IplImage edge = IplImage.create(image.width, image.height, image.depth,
                image.nChannels);
        cvCanny(image, edge, 1000, 1200, 3);
        CvMemStorage storage = CvMemStorage.create();

        long totalTime = 0;

        int iteration = 0;
        while (true) {
            iteration++;

            // carry out the hough lines and store the time taken
            long start = System.currentTimeMillis();
            CvSeq hough = cvHoughLines2(edge, storage.getPointer(),
                    CV_HOUGH_STANDARD, 1, Math.PI / 360, 1000, 20, 40);
            long end = System.currentTimeMillis();
            long time = end - start;
            totalTime += time;
            cvClearMemStorage(storage);

            if (iteration % 1000 == 0) {
                System.out.println(iteration + " mean time per iteration "
                        + (totalTime / 1000));
                System.gc();
                Thread.sleep(500);
                totalTime = 0;
            }

        }

    }

    public static IplImage createRandomImage() {
        int width = 100;
        int height = 100;
        BufferedImage image = new BufferedImage(width, height,
                BufferedImage.TYPE_BYTE_GRAY);
        Graphics g = image.getGraphics();
        for (int i = 0; i < 50; i++) {
            int x = (int) (width * Math.random());
            int y = (int) (height * Math.random());
            int x2 = (int) (width * Math.random());
            int y2 = (int) (height * Math.random());
            g.drawLine(x, y, x2, y2);
        }
        return IplImage.createFrom(image);
    }

}

Original comment by [email protected] on 15 Jan 2011 at 4:33

from javacv.

GoogleCodeExporter avatar GoogleCodeExporter commented on July 30, 2024
There is a possibility that OpenCV itself is leaking memory.. Have you tried 
something similar in C/C++ to make sure? I get lots of hit on Google search 
about cvHoughLines2 and memory leaks..

Original comment by [email protected] on 16 Jan 2011 at 1:54

from javacv.

GoogleCodeExporter avatar GoogleCodeExporter commented on July 30, 2024
I managed to fix the code. I am not sure why this work, but if I called 
cvClearMemStorage on the storage attribute *within* the CvSeq rather than the 
CvMemStorage itself there is no slowdown and no memory leak. To illustrate 
this, in the above example, I simply replaced:

cvClearMemStorage(storage);

with 

cvClearMemStorage(hough.storage);

to solve the problem....



Original comment by [email protected] on 16 Jan 2011 at 1:30

from javacv.

GoogleCodeExporter avatar GoogleCodeExporter commented on July 30, 2024
Ah, looks like there is a bug in OpenCV. cvHoughLines2() must ignore the 
storage given as argument and always creates its own. Good to know, thanks for 
reporting

Original comment by [email protected] on 16 Jan 2011 at 1:42

  • Changed state: Done

from javacv.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.