Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OpenCV Java JNA wrapper and memoty leaks #35

Closed
Artgit opened this issue Jun 21, 2018 · 1 comment
Closed

OpenCV Java JNA wrapper and memoty leaks #35

Artgit opened this issue Jun 21, 2018 · 1 comment

Comments

@Artgit
Copy link

Artgit commented Jun 21, 2018

I use the following OpenCV Java JNA wrapper:

<dependency>
	<groupId>org.openpnp</groupId>
	<artifactId>opencv</artifactId>
	<version>3.2.0-1</version>
</dependency>

According to the application logic, I need very often to convert Mat to BufferedImage and viсe versa. In order to do this - I use the following functions:

private BufferedImage mat2BufferedImage(Mat matrix) throws Exception {
	MatOfByte mob = new MatOfByte();
	Imgcodecs.imencode(".png", matrix, mob);
	byte ba[] = mob.toArray();
	return ImageIO.read(new ByteArrayInputStream(ba));
}

private Mat bufferedImageToMat(BufferedImage bi) throws IOException {
	ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
	ImageIO.write(bi, "png", byteArrayOutputStream);
	byteArrayOutputStream.flush();
	return Imgcodecs.imdecode(new MatOfByte(byteArrayOutputStream.toByteArray()), Imgcodecs.CV_LOAD_IMAGE_UNCHANGED);
}

Right now I faced a huge memory leak during the application execution on image processing step where these functions are involved.

So my question is - do I need to manually call MatOfByte.release() in both of these functions(before return statement) in order to try to prevent the memory leak?

And the same question for the objects of the following classes:

import org.opencv.core.MatOfPoint;
import org.opencv.core.MatOfPoint2f;

Do I need to manually call release() for them also?

@vonnieda
Copy link
Member

vonnieda commented Aug 4, 2018

Sorry for the long response time on this - yes, you need to call Mat.release() for any Mat you create. OpenCV bundles a finalize method, but these often don't get called, so best practice is to release it yourself.

@vonnieda vonnieda closed this as completed Aug 4, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants