Code covered by the BSD License  

Highlights from
Java Heap Cleaner

5.0

5.0 | 7 ratings Rate this file 76 Downloads (last 30 days) File Size: 2.33 KB File ID: #36757

Java Heap Cleaner

by

 

18 May 2012 (Updated )

Simple function cleaning up, at runtime, Java heap memory, thus preventing java OutOfMemory error

| Watch this File

File Information
Description

Java Heap Cleaner is a MATLAB wrapper for a Java class that cleans up the Java heap from memory leaks, preventing the infamous Java OutOfMemory exception.

The Java code re-initializes some JVM classes responsible for memory leaks, then forcing a garbage collection. Re-initialized Java classes are those used by MATLAB for displaying wievs (command history, current folder etc...).

Installation:

1) Unpack the archive in [PATH_OF_YOUR_CHOICE]
2) Add [PATH_OF_YOUR_CHOICE] to the MATLAB Path
3) Add [PATH_OF_YOUR_CHOICE]\MatlabGarbageCollector.jar to your static MATLAB classpath.txt file (in MATLAB_ROOT/toolboxes/local/classpath.txt)

Usage:

Call jheapcl from within your MATLAB code to prevent Java heap memory from completely filling up. The Java Heap Cleaner is very fast and low resouce consuming.

Please consider that there is no general rule about where/when to call jheapcl, it strongly depends on your code and system. You’re encouraged to experiment to find out the best compromise for your setup.

If you like please rate!

Note:

Thanks to Pete's for the following comment: 
"For people who don't understand step 3, and/or are getting 'Undefined variable "org" or class' errors, navigate to the appropriate directory and run: javaaddpath(which('MatlabGarbageCollector.jar'))"

Required Products MATLAB
MATLAB release MATLAB 7.13 (R2011b)
Other requirements Description update
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (22)
09 Oct 2014 Davide Tabarelli

@Robert: to monitor Heap Memory consumption of the Java virtual machine you can use the graphical tool "jvisualvm.exe" that is included in the Java SDK release.

By the way, jheapcl just clean the Heap space, therefore it has no effect if you call it only once, you have to use it when the used heap memory approach the upper limit; this depends on your code and on your matlab settings.

Have a nice day.

03 Oct 2014 Robert

@Davide: How would I monitor the heap memory? Is there a way to see it in Activity Monitor? I've set it to the maximum (256 MB) in the MATLAB Preferences but am still running into the Java Heap Memory errors when I'm repeatedly executing long code that generates lots of images (the problem does not occur in the first run but only after 3-4 runs). jheapcl is not preventing the error from happening, even if I execute it in the preamble.

26 Jul 2014 Jordan

Works. Had to restart MATLAB. I can't get silent mode to work tho :(

21 Feb 2014 Peter

Brilliant, seems to work on 2013a. Even works after it has started displaying error messages.

10 Jan 2014 lin

it is still got error when plot several times. And i got not idea if i just type in the jheapcl to call the function?

31 Oct 2013 laurie

thank you so much for this! saving me heaps of time..

31 Oct 2013 Deef

Thanks so much!

I also had problems with Java memory after running my psychtoolbox script a couple of times. Now I simply run 'jheapcl' at the end of each script and there is no problem anymore.

30 Sep 2013 Davide Tabarelli

@ Robert: did you try to monitor your heap memory? Maybe you don't call jheapcl enough often or maybe there is another memory sink somewhere ...

19 Sep 2013 Robert

I need to correct myself: I'm still getting the Java heap memory error message, in spite of running jheapcl at the end of the problematic script. Would have been too nice…

18 Sep 2013 Robert

Installed the file and added 'javaaddpath(which('~/Documents/MATLAB/jheapcl/jheapcl/MatlabGarbageCollector.jar');' to my startup.m file. No more errors since then, thank you!

13 Sep 2013 Davide Tabarelli

Hello, a short comment to to clarify how to set up the java classpath for jheapcl to work: the "javaaddpath" solution is session scoped; if you want the path to be updated across all sessions follow step 3 of instructions and add [PATH_OF_YOUR_CHOICE]\MatlabGarbageCollector.jar to your static MATLAB classpath.txt file (in MATLAB_ROOT/toolboxes/local/classpath.txt) "

15 Aug 2013 Jeffrey Johnson

Installed this today, and so far so good. I'm just running jheapcl at the end of a psychtoolbox script that was giving Java Heap errors after running a few times in succession.

Regarding the "javaaddpath" solution for step 3 above, it appears that this does not carry across Matlab sessions. There may be a command somewhere to save the Java path; I just put the javaaddpath commands in my startup.m.

15 Aug 2013 Jeffrey Johnson  
16 May 2013 Davide Tabarelli

Hi pratibha rane, wha is giving you "access denied" can you give me more details?

D.

11 May 2013 pratibha rane

I added path at the end of classpath.txt, But giving me access denied.Please help

15 Jan 2013 Alexandru

I have the same problem, I've followed the steps but get the below error:

Undefined variable "org" or class "org.dt.matlab.utilities.JavaMemoryCleaner.clear".

Error in jheapcl (line 7)
org.dt.matlab.utilities.JavaMemoryCleaner.clear(verbose)

I'm using Matlab R2011b

22 Nov 2012 Pete

Seems good. Couple of points:

typo in the 'silent cleanup' line (1=>0)

For people who don't understand step 3, and/or are getting 'Undefined variable "org" or class' errors, navigate to the appropriate directory and run: javaaddpath(which('MatlabGarbageCollector.jar'))

28 Sep 2012 Jorge Leandro

Yes, David

I unpacked the Jheadcl function in "C:\Program Files\MATLAB\R2009a\toolbox\jheapcl", added this path to the Matlab path by using "File > Set path" and then added "$matlabroot/toolbox/jheapcl/MatlabGarbageCollector.jar" to the last line of the file classpath.txt which is in "C:\Program Files\MATLAB\R2009a\toolbox\local".

However, every now and then, Matlab halts and delivers messages like:

java.lang.OutOfMemoryError: Java heap space
at com.mathworks.jmi.OpaqueJavaInterface.getExceptionMessage(OpaqueJavaInterface.java:1305)
??? Error using ==> set
Failed to retrieve Exception Message

Exception in thread "Timer-1" java.lang.OutOfMemoryError: Java heap space
at java.lang.String.toLowerCase(Unknown Source)
at java.io.Win32FileSystem.hashCode(Unknown Source)
at java.io.File.hashCode(Unknown Source)
at java.util.HashMap.put(Unknown Source)
at com.mathworks.mlwidgets.explorer.control.DocumentContextImpl.getDocumentChanges(DocumentContextImpl.java:457)
at com.mathworks.mlwidgets.explorer.control.DocumentContextImpl.refreshContents(DocumentContextImpl.java:569)
at com.mathworks.mlwidgets.explorer.control.RefreshDaemon$2.run(RefreshDaemon.java:104)
at java.util.TimerThread.mainLoop(Unknown Source)
at java.util.TimerThread.run(Unknown Source)

Did I miss something?

Regards

26 Sep 2012 Davide Tabarelli

Niko, Jorge

did you add the PATH in which you extract the archive to the Matklab Java claspath? See point 3) of the instructions.

D.

12 Sep 2012 Niko

Great idea, but I have the same problem:

Undefined variable "org" or class "org.dt.matlab.utilities.JavaMemoryCleaner.clear".

10 Sep 2012 Jorge Leandro

I'm using Matlab 7.8.0 (2009a) and have had problems with memory due to Java heap overflow.
So, I tried to follow your guidelines, but ended up with following error messages. Is it because of my Matlab version?

??? Undefined variable "org" or class
"org.dt.matlab.utilities.JavaMemoryCleaner.clear".

Error in ==> jheapcl at 7
org.dt.matlab.utilities.JavaMemoryCleaner.clear(verbose)

Cheers

21 May 2012 Julien

Nice job, it seems really efficient... Could you give more details about what you do in your java class ?

Updates
21 May 2012

Brief description of java code added.

22 Apr 2013

Description update

23 Apr 2013

Update of instructions

Contact us