Clear Java memory

I am using Matlab 2012b to connect to another program (Imaris) from which I am importing data. Communication actually happens through java as an interface. The problem is that the data is not deleted from the Java memory and accumulates over time until Matlab ultimately fails. "clear Java" does not work and issues the following warning: "Objects of class Ice / ConnectionRefusedException exist - are not cleared"

The only solution I found to really clear Java memory is to restart Matlab, which is not an option in my case.

When I searched the Internet, I found that quite a few people took advantage of the same problem. ( http://www.mathworks.de/matlabcentral/newsreader/view_thread/283708 )

However, I did not find an answer how to solve the problem. Does anyone know a solution?

+4
source share
4 answers

if you receive messages from explicit java that there are objects of the class that exist, and clearing everything does not delete them, then something has a link to the object somewhere. this often happens with callbacks, listeners, etc., or when you add an object reference to an anonymous function descriptor or the like.

clear commands remove a reference to an object from the workspace, but since there is an anonymous (or otherwise) link in the callback, the object cannot be garbage collected, so it remains in memory (and potentially an orphan)

I see this a lot when I'm lazy about writing good destructors

+1
source

You can try to call the garbage collector, but there is no guarantee that its execution will actually be executed, since you do not call the collector, but raise the request.

Run from matlab command line or from your m-code (Fixed thanks to comments)

java.lang.System.gc() 
0
source

clear java seems to reset a complete Java virtual machine. As a protection for you as a developer, Matlab will not perform this action if you have Java objects in memory that will be destroyed by this action.

Ideally, you can define a Matlab element (usually a variable, either in the workspace or in some constant area) that is an instance of the Ice/ConnectionRefusedException class, and clear it. For variables that you have access to (for example, in the workspace), you can simply run clear variablename or for variables held as constants in some other function, you can run clear functionname .

Then try calling clear java again, as you did.

However, it is sometimes difficult to find the place where the abusive object is located. As you know, I restarted Matlab as a fail-safe restart; Of course, this does not work if you want to work with some recently acquired data.

0
source

You need to install static java. Since you are not closing Matlab. You can only process a dozen files, because the space with the pergene is only 4 MB. Download ten times imarislib.jar .

You must add the path to javaaddpath.txt and copy it to prefdir. Then delete all lines with javaaddpath in all extensions, whether imaris embedded it or wrote it.

-1
source

All Articles