Matlab leaking memory?

8 views (last 30 days)
Alaster Meehan
Alaster Meehan on 26 Sep 2019
Below is an attempt at cleaning some memory from Matlab, it seems that "clear all" is giving me less memory.
I am using a custom GUI using the the old figure, not the uifigure, not sure if this would help. Also using some .NET structures, but I am careful about clearing these in my code, and most of those structures are small.
I suspect that some java variables are not being cleaned, most likely when Images are being relpaced on an images axis as this is the biggest memory use.
32GB of memory is a lot to use without any variables in the work space!
Is there a way to clear free RAM without restarting Matlab? Ideally I would like to be able to run a clean up without having to close my GUI or clear my workspace.
>> memory
Maximum possible array: 16540 MB (1.734e+10 bytes) *
Memory available for all arrays: 16540 MB (1.734e+10 bytes) *
Memory used by MATLAB: 33625 MB (3.526e+10 bytes)
Physical Memory (RAM): 32684 MB (3.427e+10 bytes)
* Limited by System Memory (physical + swap file) available.
>> clear all
>> memory
Maximum possible array: 11476 MB (1.203e+10 bytes) *
Memory available for all arrays: 11476 MB (1.203e+10 bytes) *
Memory used by MATLAB: 32313 MB (3.388e+10 bytes)
Physical Memory (RAM): 32684 MB (3.427e+10 bytes)
* Limited by System Memory (physical + swap file) available.
>> clearvars -global
>> memory
Maximum possible array: 10238 MB (1.074e+10 bytes) *
Memory available for all arrays: 10238 MB (1.074e+10 bytes) *
Memory used by MATLAB: 32313 MB (3.388e+10 bytes)
Physical Memory (RAM): 32684 MB (3.427e+10 bytes)
* Limited by System Memory (physical + swap file) available.
>>

Answers (1)

Sai Bhargav Avula
Sai Bhargav Avula on 4 Dec 2019
Hi,
I believe this has to do more with the OS and its memory management routines. clear statements can mark the memory being unused, it’s up to the OS to reclaim it. Even if there is sufficient free memory, it is free contiguous memory that is limiting when creating arrays. Once it becomes insufficient it throws an error.
Following the guidelines given in the below link can help you to avoid them.
The above link will guide you through different issues that can cause memory issues and detailed steps to avoid them.
Hope this helps! ,

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!