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

Need help on possible memory leak. #21

Closed
balachandarsv opened this issue Oct 20, 2023 · 3 comments
Closed

Need help on possible memory leak. #21

balachandarsv opened this issue Oct 20, 2023 · 3 comments

Comments

@balachandarsv
Copy link

I am trying to use lept4j for contour detection. Attaching the code and image used here.
In the code there is a constant increase in memory when reading boxaGetBox and pixaGetPix, if i comment those two lines, the memory usage is steady. Am I missing something?

Using latest lept4j jar.

Image:
This is an screenshot image.
sample-text
Code:

import java.io.FileNotFoundException;

import com.sun.jna.ptr.PointerByReference;

import net.sourceforge.lept4j.Box;
import net.sourceforge.lept4j.Boxa;
import net.sourceforge.lept4j.Leptonica1;
import net.sourceforge.lept4j.Pix;
import net.sourceforge.lept4j.Pixa;
import net.sourceforge.lept4j.util.LeptUtils;

public class Mulit {

	public static void test(Pix image) {

		Pix adapativePix = Leptonica1.pixConvertTo1Adaptive(image);

		Pix pix2 = Leptonica1.pixAutoPhotoinvert(adapativePix, 127, null, null);

		PointerByReference pbr = new PointerByReference();

		Boxa connectedComponents = Leptonica1.pixConnComp(pix2, pbr, 8);

		Pixa ccs = new Pixa(pbr.getValue());
		for (int i = 0; i < connectedComponents.n; i++) {
			Pix cPix = Leptonica1.pixaGetPix(ccs, i, Leptonica1.L_COPY);
			Box cBox = Leptonica1.boxaGetBox(connectedComponents, i, Leptonica1.L_COPY);
			// Do some operations here
			LeptUtils.dispose(cPix);
			LeptUtils.dispose(cBox);

		}

		LeptUtils.dispose(ccs);
		LeptUtils.dispose(connectedComponents);
		LeptUtils.dispose(pix2);
		LeptUtils.dispose(adapativePix);
		Leptonica1.pixDestroy(pbr);

	}

	public static void main(String[] args) throws FileNotFoundException, Exception {
		while (true) {

			String filename = "/tmp/sample-text.png";

			Pix load = Leptonica1.pixRead(filename);
			test(load);
			LeptUtils.dispose(load);

		}
	}

}
@balachandarsv
Copy link
Author

@nguyenq Can you please look at this and let me where the issue is when you find time? Thank you.

@nguyenq
Copy link
Owner

nguyenq commented Dec 5, 2023

@balachandarsv pbr is of PointerByReference type, so your current method to dispose or destroy it will not work. I recall Leptonica has a function called lept_free that may work for you.

@balachandarsv
Copy link
Author

Thank you. Let me check that once.

@nguyenq nguyenq closed this as completed Mar 2, 2024
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