Optimizing Memory Usage in Matlab: Tips and Techniques

In summary, after running the ODE for many iterations, Matlab will not release memory it has taken until the program is paused. A third-party program can be used to detect this and then Matlab will be restarted.
  • #1
Haorong Wu
413
89
TL;DR Summary
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
  • #3
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 jedishrfu
  • #4
clear out intermediate variables in your code when they are not needed any longer to continue the calculation.
 
  • #5
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.
 
  • #8
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.
 
  • #9
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.
 

1. How do I clear all variables in Matlab to free up memory?

To clear all variables in Matlab, you can use the "clear" command. This will remove all variables from the workspace and free up memory for new calculations. You can also use the "clear all" command to clear both variables and functions, or use the "clearvars" command to specify which variables you want to clear.

2. Can I manually free up memory in Matlab?

Yes, you can manually free up memory in Matlab by using the "clear" command to remove unnecessary variables or by deleting large data arrays that are no longer needed. You can also use the "pack" command to reorganize the workspace and free up any fragmented memory.

3. What is the difference between "clear" and "clear all" in Matlab?

The "clear" command removes all variables from the workspace, while the "clear all" command also removes any functions that have been defined. It is recommended to use "clear" for general memory management, and "clear all" only when you want to reset the entire workspace.

4. How can I check the amount of free memory in Matlab?

You can use the "memory" command in Matlab to check the amount of free memory available. This will display the total amount of available memory, as well as how much is currently in use and how much is free.

5. Is there a way to automatically free up memory in Matlab?

Yes, you can use the "clear" command with the "-regexp" option to clear all variables that match a specific pattern. This can be useful for automatically freeing up memory when certain variables are no longer needed. You can also use the "pack" command to automatically reorganize the workspace and free up any fragmented memory.

Similar threads

  • Programming and Computer Science
Replies
17
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
11K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
Back
Top