# A solution for the infamous Matlab-Java &quot;out of heap memory error&quot;

**URL:** <https://psychtoolbox.discourse.group/t/a-solution-for-the-infamous-matlab-java-quot-out-of-heap-memory-error-quot/432>\
**Category:** Uncategorized\
**Created:** [May 21, 2012, 1:03pm UTC](https://psychtoolbox.discourse.group/t/a-solution-for-the-infamous-matlab-java-quot-out-of-heap-memory-error-quot/432 "2012-05-21T13:03:38Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Allen\_Ingling](https://avatars.discourse-cdn.com/v4/letter/a/a183cd/32.png) [@Allen\_Ingling](https://psychtoolbox.discourse.group/u/Allen_Ingling)\
**Post date:** [May 21, 2012, 1:03pm UTC](https://psychtoolbox.discourse.group/t/a-solution-for-the-infamous-matlab-java-quot-out-of-heap-memory-error-quot/432/1 "2012-05-21T13:03:38Z")

</div>

Dear All,

  

I'm writing to share with the community the solution I've developed to solve the bothering OutOfMemory error that some times occurs when using PTB.

  

**A short summary of the issue**

After some (random) time Matlab crashes with the following exception:

> > Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError:
> > 
> > Java heap space.
> 
> This is due to some JVM classes (Swing related) generating a memory leak every time a display property is changed. These JVM classes are those responsible for displaying Matlab views (Command history, Current folder etc...).
>   
> 
> **A solution**
> 
> I wrote a Java piece of code that re-initializes these Java classes together with a Matlab command (jheapcl.m) acting as a wrapper. Calling this command will "really" clean up your heap memory, avoiding the infamous crash.
> 
>   
> 
> You can find all the files on the Mathworks community [here](http://www.mathworks.it/matlabcentral/fileexchange/36757-java-heap-cleaner)&nbsp; ([http://www.mathworks.it/matlabcentral/fileexchange/36757-java-heap-cleaner).](http://www.mathworks.it/matlabcentral/fileexchange/36757-java-heap-cleaner)
> 
>   
> 
> Currently it has been tested on Matlab 2011b and 2010b. Please comment/rate the file, letting me know if it really helps and if there are problems.
> 
>   
> 
> Thank you very much in advance.
> 
>   
> 
> D.
