0
Is dispose method internally available in Java
If the dispose method is internally available in java, why when I insert the method in my code get the Eclipse system message that "create a method called dispose"? Is there any class relevant to the method I should import? Please see my code below: https://code.sololearn.com/cVJOG66IePq0/?ref=app
13 Réponses
+ 3
https://tess4j.sourceforge.net/docs/docs-3.3/net/sourceforge/tess4j/Tesseract.html#dispose--
protected void dispose()
Releases all of the native resources used by this instance.
Because it is protected, you cannot call it directly from external code. So I assume the Tesseract instance uses this method internally.
+ 2
The code you linked is the actual source code of the Tesseract library. It seems that all the public methods of this library API already call this dispose method automatically, for example when you use the doOCR method, at the end dispose is run automatically anyway.
+ 1
If that the case do you have any idea why I have a memory leak problem in my code as shown in a linked code exception above, and what is the possible way to fix this problem?
do you think calling tesseract instance = null; and System.gc(); will solve the problem?
like:
Tesseract tess = new Tesseract();
tess = null;
System.gc();
+ 1
Sibusiso Mbambo I think what you are proposing might be a good idea to try. You got this specific response on SO:
"I would dispose of the instance and start a new one after so many OCR operations; otherwise, the memory leaks in the native code will eventually crash the JVM."
I think the author meant just what you wrote now, to set the tess instance reference to null, so that it can be garbage collected.
But in my opinion this is a difficult problem. Tesseract was written in C++ and the Java API is merely a wrapper around it, using foreign interface. Even if you use a Java profiler, it would be difficult to figure out where are those 'memory leaks' the author mentioned and how to prevent them.
+ 1
Ok, just for my knowledge. What I head on the internet while searching for my solution is that setting instance to null do not make the instance garbage collected. Only the reference to the instance can be set to null. So creating an instant like this:
Tesseract tess = new Tesseract();
tess = null;
Am I not nullifying an instance or object Or "tess" is the reference?
+ 1
tess is the reference (a variable that points to an object instance) and when you do
tess = null;
You are technically removing the link between the variable and the object which stays in memory. Eventually the virtual machine runs GC and the Tesseract instance which has no more references to it, is freed up from memory. It is not even needed to explicitly run System.gc() as this is not executed immediately, only when the JVM finds it convenient.
+ 1
Very good explanation Tibor Santa. But my code runs continuously which needs the "tess" to be available to read text. So when nullifying the the "tess" the code won't be able to read text.
+ 1
Yes, I see that your code runs continuously and takes a picture every 10 seconds.
You also said that it's failing after 4-5 days.
After you set tess to null, you have to create a new Tesseract instance and configure it as you do in the beginning of your code. I am sure this has also some overhead in time and memory, and you probably should not do this every 10 seconds. Maybe once a day or once an hour is enough. You can also introduce a counter incremented with each image and when it reaches a limit then create the new instance (inside your while loop) and reset your counter.
+ 1
I got what you saying, the text that I'm capturing on screen are random numbers that change every few seconds and I need to capture them and add them to the ArrayList. Every 24 hours I clear the ArrayList then send the results to my email address. So I cannot avoid not taking screenshots every few seconds.
0
00
0
Gary Ragan I don't understand your answer "00"
0
Hey can you help me code possibly
0
Or can i help you in someway