opencv-java / getting-started

Getting started with OpenCV and JavaFX
105 stars 176 forks source link

Huge memory leak on Mat object (not thread safe) #15

Closed jcbritobr closed 4 years ago

jcbritobr commented 4 years ago

There is a huge memory leak here. Im trying to fix, but even calling Mat.release(), didn't worked. I think may there is an issue calling read(Mat mat) from another thread.

    private Mat grabFrame()
    {
        // init everything
        Mat frame = new Mat();

        // check if the capture is open
        if (this.capture.isOpened())
        {
            try
            {
                // read the current frame
                this.capture.read(frame);

                // if the frame is not empty, process it
                if (!frame.empty())
                {
                    Imgproc.cvtColor(frame, frame, Imgproc.COLOR_BGR2GRAY);
                }

            }
            catch (Exception e)
            {
                // log the error
                System.err.println("Exception during the image elaboration: " + e);
            }
        }

        return frame;
    }
jcbritobr commented 4 years ago

I did the same code using swing. The leak doesn't happens. I'm investigating the javafx imageview component .

jcbritobr commented 4 years ago

Found that without -Xmx javafx hasn't any control of the heap. This vm argument solve the issue.