quit doesn't quit the matlab when called from a loop.

1 view (last 30 days)
Hello,
I am running a m-script that makes a lot of memory leak, and it reaches 2GB in about 3 hours. Since I am calling a lot of functions that somebody else created, I figured it is easier to just restart the function whenever it reaches memory maximum, and resume the loop. So, I implemented [!matlab -r myMscript] in the loop, but it won't quit so it won't free up the memory.
For example, the following script doesn't quit the matlab.
for i = 1:100,
if mod(i,20)==0,
!matlab -r test=1
quit
end
end
My matlab version is 7.7.0.471, and I have 64bit computer (Windows 7). The matlab is running with 32bit-mode.
Could anybody help me to dissovle this problem?
Best,
Ji Hyun

Accepted Answer

Titus Edelhofer
Titus Edelhofer on 7 Mar 2012
Hi,
never thought about this way of doing this. You could try to do the following:
!matlab -r test=1 &
Note the & at the end of the line to not wait for the new instance to close.
Titus
  4 Comments
Titus Edelhofer
Titus Edelhofer on 8 Mar 2012
Hi Sean,
it's here:
http://www.mathworks.com/help/techdoc/matlab_env/f0-12994.html#f0-38522
Titus
Daniel Shub
Daniel Shub on 8 Mar 2012
I am not sure that I like the fact that MATLAB spawns processes that it doesn't kill when you quit. Part of me thinks this is really bad behavior. The background running of a task makes sense, but I think MATLAB should clean up after itself.

Sign in to comment.

More Answers (2)

Sean de Wolski
Sean de Wolski on 7 Mar 2012
Interesting idea. when you run:
!matlab
it halts execution in the current MATLAB instance until the new instance closes.

Daniel Shub
Daniel Shub on 7 Mar 2012
I think Sean de gives the reason for your problem. As for a work around, I can think of a bunch of ways and it is really what you are most comfortable with. I think the key is to modify your script/function so that it only does a few loops and then exits. The "hard" part is letting the script figure out what loops to do. You could use the script name, check for an environment variable, pass an argument, etc).
Then you need to call the script/function, wait for it to end, and call the next one. If you have the parallel computing toolbox and/or a cluster, you could use a scheduler. I would probably write a shell script to do it. You could use MATLAB to do it.

Community Treasure Hunt

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

Start Hunting!