Optimizing Memory Usage in Matlab: Tips and Techniques

  • Context: MATLAB 
  • Thread starter Thread starter Haorong Wu
  • Start date Start date
  • Tags Tags
    Matlab Memory
Click For Summary

Discussion Overview

The discussion revolves around optimizing memory usage in Matlab, particularly when using the ode45 function to solve ordinary differential equations (ODEs). Participants explore various techniques and challenges related to memory management during long-running computations.

Discussion Character

  • Exploratory
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant describes experiencing memory issues when using ode45 in a loop, noting that memory does not seem to be freed until the program is paused.
  • Another participant inquires about the possibility of invoking garbage collection after a certain number of iterations.
  • Some participants suggest clearing intermediate variables when they are no longer needed, although one notes that this does not appear to free memory effectively.
  • There is a mention of external resources discussing Matlab's garbage collection behavior and potential workarounds.
  • One participant proposes using the command "clear ode45" to see if it helps with memory release.
  • A later post claims that using "clear ode45" successfully releases memory occupied by Matlab.
  • Another participant expresses frustration with memory consumption and mentions resorting to writing a C# program to monitor and restart Matlab when memory usage is high.

Areas of Agreement / Disagreement

Participants express a range of views on memory management in Matlab, with no consensus on a definitive solution. Some methods are proposed, but their effectiveness is debated, and experiences vary.

Contextual Notes

Participants highlight limitations in Matlab's memory management, particularly regarding the timing of garbage collection and the behavior of the ode45 function. There are unresolved questions about the best practices for managing memory during extensive computations.

Who May Find This Useful

Users of Matlab, particularly those working with ODEs or long-running computations, may find this discussion relevant for exploring memory optimization techniques.

Haorong Wu
Messages
419
Reaction score
90
TL;DR
How to free memory that is allocated but is not used currently in Matlab?
Hi. I am using Matlab to solve ODEs. I use ode45 in a for loop since solving the ODEs in a single ode45 function yielding an error of "out of memory".

However, after the loop has run many times for several hours, the memory will still be full and the program will become slower and slower. If I pause the program, then after several minutes, the memory will be free automatically. It seems that Matlab will not free the memory allocated before unless I pause the program.

So I am trying to find a way to free that memory in the process.

I used the "pack" function. But it can only be used from the command line, and can not be written in a program. Also, I try to use a memory-optimizing app, but it failed when the program is running.

Now I am thinking of using the command "memory" to acquire current memory information and pause the program if not enough memory is available. I am not sure whether this will work or not. I will wait and see.

Are there other methods?

Thanks!
 
Physics news on Phys.org
Thanks, @jedishrfu . On the page you referred to, they seem to experience the same problem. Maybe Matlab does not have a suitable garbage collection. I have to check my computer from hour to hour to see whether should I restart the program or not. Sad.
 
  • Like
Likes   Reactions: jedishrfu
clear out intermediate variables in your code when they are not needed any longer to continue the calculation.
 
Dr Transport said:
clear out intermediate variables in your code when they are not needed any longer to continue the calculation.
Hi, @Dr Transport . Unfortunately, this is not working. It appears that Matlab only marks those cleared variables without actually free memory. The garbage collection will be done either by Java or by Windows, maybe.

Strangely, the collection will not be conducted properly when the program is running. Only after the program is paused for some time, the memory taken by those variables will be free slowly.
 
Try deleting the variables after they are used
 
Thanks, @Dr Transport , @jim mcnamara .

I have new ideas now. I suspect that the ode45 takes memory and does not release it, so I will try the command "clear ode45" and see what will happen.
 
Problem solved.

The command "clear ode45" keeps releasing memory occupied by Matlab.

Cheers!
 
  • #10
Fine. I give up. The memory was eaten up this morning.

Well, I resort to my final method. I wrote a C# program to detect the memory, terminate, and restart Matlab if most memory is occupied.
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 4 ·
Replies
4
Views
12K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 6 ·
Replies
6
Views
2K