Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Java not clearing from memory?

287 views
Skip to first unread message

Christopher

unread,
Jun 2, 2010, 12:42:04 PM6/2/10
to
Hello,
I'm working with a matlab/java setup to communicate with a robotics simulation environment. The java part of the system contains its own thread for recieving messages from the simulator.

Now, the problem is that the java classes persist even after being deleted from the matlab stack. I can call "clear java" and the variable containing the java class is deleted, but the class, and therefore the thread, persists in memory. I can tell because System.out.print will still display messages in MATLAB even after a "clear java" call. I notice significant performance losses after running simulations overnight because there are so many java classes in memory.

Is there any way to remove the java class instances from memory entirely?

Thanks!
Chris

us

unread,
Jun 2, 2010, 12:50:04 PM6/2/10
to
"Christopher " <cw...@drexel.edu> wrote in message <hu61ks$q4t$1...@fred.mathworks.com>...

to see what's still living in your ML cache, look at

[a,b,c]=inmem;
% and (most likely), you'll be surprised...

us

Christopher

unread,
Jun 2, 2010, 1:10:22 PM6/2/10
to
Hmm, after I clear java, the class is no longer shown by inmem, but it will still System.out.print to the matlab console.

-Chris

us

unread,
Jun 2, 2010, 1:33:06 PM6/2/10
to
"Christopher " <cw...@drexel.edu> wrote in message <hu639u$j0a$1...@fred.mathworks.com>...

hmm...
but do you really do this

import java.lang.*
System.out.print('foo');
% foo>>
clear java;
clear import; % <- !
System.out.print('foo')
% foo??? Undefined variable "System" or class "System.out.print".

us

Christopher

unread,
Jun 2, 2010, 2:12:06 PM6/2/10
to
">
> hmm...
> but do you really do this
>
> import java.lang.*
> System.out.print('foo');
> % foo>>
> clear java;
> clear import; % <- !
> System.out.print('foo')
> % foo??? Undefined variable "System" or class "System.out.print".
>
> us

OK, my machine throws te same error wth that code.

I tried "clear java;clear import" with my class instances, but it still doesn't kill the threads from memory. They're still printing to matlab. It's like the java classes can talk to me, but I can't talk to them.

-Chris

Christopher

unread,
Jun 2, 2010, 2:38:05 PM6/2/10
to
Shoot, I should probaly also say that when I create a new instance of the class after I either already have one or have cleared one in the current matlab session, it gives me these warnings:

Warning: Duplicate entry,D:\[path]
>In javaclasspath>local_validate_dynamic_path at 276
In javaclasspath>local_javapath at 184
In javaclasspath at 119
[...]
Warning: Objects of [class] class exist - not clearing java
>In javaclasspath>local_javapath at 194
In javaclasspath at 119
[...]


I don't know if this is helpful or not...


-Chris

us

unread,
Jun 2, 2010, 2:49:06 PM6/2/10
to
"Christopher " <cw...@drexel.edu> wrote in message <hu68ed$6l5$1...@fred.mathworks.com>...

well... a slightly different problem...

% now, you must
clear 'your_class';
% as well

us

Christopher

unread,
Jun 2, 2010, 3:12:04 PM6/2/10
to

>
> well... a slightly different problem...
>
> % now, you must
> clear 'your_class';
> % as well
>
> us


Hmm, nope. Clearing th java path does't help either. inmem still dos't repor it, but it's definitely still there.

-Chris

Mark Stevenson

unread,
Jun 29, 2010, 8:29:25 PM6/29/10
to
I am having a similar problem. I have a JAR file containing classes for loading large data files into memory. I import them and use them to load data and return a matrix of numbers.
No matter what i try, i cannot clear the data from memory. The only way i can do it is to restart matlab.

A simple example:

*******************************************

import utils.Extract;

dataMatrix = Extract.subsetArray(fileName, 010901,100601);

clear all
clear java
clear import


********************************************

Extract.subsetArray is a java method that returns a matrix of data from the file 'filename' between the dates 100901- 100601.
Nothing i do frees the memory, only restarting. I can't work around this as i need to put this in a loop.
Any further ideas?

Regards,

Mark

Lindsey Amos

unread,
Aug 23, 2010, 1:15:22 PM8/23/10
to
Hi,

I also have a similar problem. I am running some simulations which use jPar to parallelise. I want to be able to run as many simulations as I like without having to restart MATLAB but I eat up the java memory running a sim and after a couple I will run out of memory.

I just want to be able to free this memory without having to restart MATLBA, but I cannot find anywhere how to do this?!

Any solutions?

Thanks

Lindsey Amos

unread,
Aug 26, 2010, 12:22:05 PM8/26/10
to
"Lindsey Amos" <lindse...@redbulltechnology.com> wrote in message <i4uab9$ai5$1...@fred.mathworks.com>...

jPar authors fixed the memory leak in the java code... :)

Christopher

unread,
Sep 3, 2010, 1:41:04 PM9/3/10
to
"Lindsey Amos" <lindse...@redbulltechnology.com> wrote in message <i564bd$9ng$1...@fred.mathworks.com>...

> "Lindsey Amos" <lindse...@redbulltechnology.com> wrote in message <i4uab9$ai5$1...@fred.mathworks.com>...
> jPar authors fixed the memory leak in the java code... :)

Oh, very good! Do you know which version of MATLAB carries the fix?

Malcolm Lidierth

unread,
Sep 3, 2010, 2:04:07 PM9/3/10
to
Not sure if this is relevant in this context or for that matter whether this is an entirely accurate description of the 'nuts and bolts'.
Calling clear when a reference to a Java object exists outside of MATLABs user workspace (e.g. when its referenced in the variable editor) appears to leave an instance behind that can not be accessed from MATLAB.
It might be worth trying drawnow() or pause() to flush the event queue before clearing the objects.

Mengda Ren

unread,
Oct 19, 2016, 8:40:08 PM10/19/16
to
"Christopher" wrote in message <hu61ks$q4t$1...@fred.mathworks.com>...
you should ues JAVA not java.

I have the same problem and tried a lot and find below works best:


try
% if exist('JAVA')
clear JAVA
% end
end

clear CLASSES
clear ALL
clear

pause(10);

clearvars -except ### ### ###

Mengda Ren

unread,
Mar 13, 2017, 1:35:07 PM3/13/17
to
"Christopher" wrote in message <hu61ks$q4t$1...@fred.mathworks.com>...
%%A reference.

Maybe u need to set static java. As u do not close matlab, you only can process a dozen of file because permgen space is only 4 mb and max. loading ten times of imarislib.jar.

the dynamic java path is really a shit of imaris.

U should add the path to javaaddpath.txt and copy it to prefdir. and delete all the lines with javaaddpath in all extensions whether imaris embedded or u wrote.
0 new messages