MATLAB Answers

What happens on Ctrl + C?

62 views (last 30 days)
I have a problem that my code becomes slower with each iteration (I used a profiler, it is none of the usual culprits: unclosed figures or non-preallocated arrays). If I click Ctrl+C and continue where I started, it runs quickly again. Can someone please tell me what exactly happens on Ctrl+C? I tried writing my variables into a file, clearing all and re-starting, but it didn't help. I would like to know which function is called on this command so I can reproduce this behavior.

  6 Comments

Show 3 older comments
Tiko
Tiko on 4 Dec 2017
I will try to look at the memory in more detail. What I have done so far was look at the profiler. Unfortunately, it doesn't give memory consumption (is there a profiler for that?). The profiler points to some Matlab-internal functions (see images I uploaded). The "return" does suggest that there is a memory leak. Do you have an idea how to clear the problematic memory?
Tiko
Tiko on 4 Dec 2017
I also observed the memory in the task manager, as well as with the "memory" command. Interestingly enough, the task manager shows a memory increase. The "memory" command, placed every few iterations, shows a slight decrease. I wrote a script that calls the same function twice. Thus, in the second call it should not be using more memory, but the task manager begs to differ and the second simulation is slower as well. In either case, the task manager claims I am using about 15% of the total memory.
Philip Borghesani
Philip Borghesani on 4 Dec 2017
Some additional information would help here: What MATLAB version are you using and are you using any custom MEX files?

Sign in to comment.

Accepted Answer

Philip Borghesani
Philip Borghesani on 4 Dec 2017
Two things happen when MATLAB returns back to the command line:
  1. Memory leaked by any mex files is cleaned up (mxMalloc... or MATLAB arrays). The best practice is to only rely on automatic clean up in the event of an error.
  2. Any pending display/graphics messages are processed by MATLAB. This can be forced by adding a drawnow to your code. In general this should not cause an issue because there should not be a huge number of events pending but there have been bugs in the distant past.

  0 Comments

Sign in to comment.

More Answers (0)

Tags