close and restart matlab ? or another way out?
283 views (last 30 days)
Show older comments
Hello,
I have a code that is quite computationally and memory-wise intensive and given the RAM(64Gb) I have, I can utilize it for my purpose and get my work done. My code does some kind of optimization, so I need to run my code again, but even after upon performing these operations
clear all;
close all;
pack;
I observe that MATLAB does not release the RAM fully, and only upon restarting, it does. I have some idea that this is a known problem with MATLAB, but I am just curious, has there been any new development that can help? I have R2017a running on Ubuntu16.04
Secondly, I am OK with re-starting and launching a new session, but running these commands
system('matlab &');
exit;
does not help. I see a startup screen, but there is no new window. Also, removing the '&' from above launches a new window but the previous one remains and the RAM is not freed.
Can anyone please help me with this?
3 Comments
Waddah Moghram
on 2 Oct 2021
Having the same problem here. I was wondering when the folks are going to address this problem. So far, I have been shutting down and starting parallel pools, but after a few rounds of doing this, MATLAB becomes so sluggish that you have to close the whole thing and run it again.
Jan
on 2 Oct 2021
@Waddah Moghram: I asked for details in the original quiestion already, because it was not clear. Now you explain, that you have "the same problem". How do you know this?
Please open a new question and explain your problem with details. What does "closing the hole thing" mean?
Answers (2)
Matthew Johnson
on 25 Apr 2022
I think the problem with running the commands as OP stated is that it tries to launch the new instance of MATLAB before the old one has quit. This is slightly also undesirable e.g. on the Mac where the first instance you launch is in some sense the "main" instance (e.g. subsequent instances spawn new entries on the Dock but the first one doesn't, at least if you have MATLAB permanently in the Dock as I do).
My somewhat sub-optimal solution has been to put a shell script somewhere, such as my home directory, that launches MATLAB after a few seconds of sleep. For instance mine is called sleepmatlab.bash and it contains this:
#!/bin/bash
sleep 8;
open /Applications/MATLAB_R2020b.app
You can obviously change this for the appropriate MATLAB-launching command on your system and titrate the sleep time so that it is long enough to ensure the typical MATLAB instance fully shuts down without being too much longer.
Then in MATLAB I have another script, restartmatlab.m , that comprises the following:
! ~/sleepmatlab.bash &
exit
So now when you want to restart, run restartmatlab from within MATLAB. It will call the Bash script, which will sleep for 8 seconds, and then exit MATLAB. Presumably in those 8 seconds MATLAB will finish fully shutting down, and then soon after the Bash script will reawaken and open a fresh instance.
If anyone out there is better at shell scripting than I am (or equally good but less lazy), you could probably pretty easily modify this approach so that the shell script checks the ps command every 1 second or so, and launches MATLAB as soon as it is no longer listed among the running processes, instead of sleeping for a fixed duration.
0 Comments
Jeno Boka
on 12 Apr 2022
Try this handy script: Restart MATLAB - File Exchange - MATLAB Central (mathworks.com)
You can also add it to your favorites
1 Comment
Jan
on 12 Apr 2022
The OP explained already, that system('matlab &'); does not solve the problem. Then suggesting a function, which does exactly the same, will not help.
See Also
Categories
Find more on Performance and Memory in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!