user42037
user42037

Reputation: 39

aborting the MATLAB code when the RAM is FULL

Is there any MATLAB command that let us abort the MATLAB code when the 90% of RAM is full due to the huge amount of data?

I am asking this question because I do not want to restart the computer every time that MATLAB is stuck and computer is hanged?

Upvotes: 2

Views: 129

Answers (2)

GameOfThrows
GameOfThrows

Reputation: 4510

Adding this answer to SO as suggested by @Ander Biguri, the answer if purely based on this link

Using Matlab try (as an option), you can monitor your memory usage as

 tryOptions.watchdog.virtualAddressSpace = 7e9 ; %//7GB Mem
 tryOptions.watchdog.execTime = 1800 ;           %//Execution Time 1800 seconds
 try tryOptions
 ...
 catch  %// use the try and catch combo to monitor your memory usage and kill process if you need to.

Other useful tools which may help:

T = evalc('feature(''memstats'')') ;
str2mat(regexp(T, '(?<=Use:\s*)\d+', 'match'))

The memstats could output the current stats of your memory, you can add break points in your code (at the beginning of a major operation) to monitor your memory usage and decide if you want to continue executing.

Upvotes: 2

Ander Biguri
Ander Biguri

Reputation: 35525

As far as I know, you can't "automatically" do that, if MATLAB hangs, it hangs.

However, in your code, you can always add somewhere (e.g. inside of a memory heavy iterative function) a memory check.

if you do

maxmem=2e10; %about 2GB of RAM

%% //this inside the memory heavy code
mem=memory;
if mem.MemUsedMATLAB>maxmem
   exit;  % // or some other thing you may want to do
end

This will exit MATLAB when the memory is about 2GB of RAM (the value is in bits, so make sure you note that when putting your own value)

Upvotes: 2

Related Questions