clear Java memory

跟風遠走 提交于 2019-12-22 08:25:16

问题


I am using Matlab 2012b to connect to another program (Imaris) from which I import data. The communication actually happens via java as interface. The problem I have is that the data is not deleted from the Java memory and piles up over time until Matlab ultimately crashes. "clear Java" does not work and produces the following warning: "Objects of Ice/ConnectionRefusedException class exist - not clearing"

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

When searching through the web I found that quite a few people have encoutered the same problem. (http://www.mathworks.de/matlabcentral/newsreader/view_thread/283708)

However I did not find an answer how to solve the issue. Does anybody know a solution?


回答1:


if you are getting messages back from clear java that there are objects of a class that exist, and clear all isn't removing them, then something somewhere has a reference to the object. this can often happen with callbacks, listeners etc, or when you add a reference to an object into an anonymous function handle or the like.

the clear commands remove the reference to the object from the workspace(s), but as the anonymous (or otherwise) reference exists in a callback the object can't be garbage collected so remains in memory (and potentially orphaned)

i see this a lot when i am lazy about writing good destructors




回答2:


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

Ideally, you can figure out when Matlab item (usually a variable, either in the workspace or some persistent scope) which is an instance of the class Ice/ConnectionRefusedException, and clear it. For variables that you have access to (e.g. in the workspace) you can just run clear variablename, or for variables held as persistents in some other function, you can run clear functionname.

Then try calling clear java again, as you have been doing.

That said, sometimes it is hard to track down the location where the offending object is located. I have been known to restart Matlab as a failsafe restart; of course that doesn't work if you want to work with some recently acquired data.




回答3:


You can try to call the garbage collector, however there is no guarantee his will actually be executed as you do not call the collector, but raise a request.

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

java.lang.System.gc()



回答4:


You need to set static java. As you do not close matlab. You can only process a dozen of file because permgen space is only 4 MB. Loading ten times of imarislib.jar.

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



来源:https://stackoverflow.com/questions/15343285/clear-java-memory

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!