Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check available memory in Matlab 2010b or later on Mac OSX?

I've got a copy of Matlab from 2004 (forgot what the version number would be, but it certainly is old), and I could use the "features memstat" command to see how much memory is available to Matlab. The command no longer works for me in Matlab 2010b, what should I do to see available memory? Thanks.

P.S. I tried the "memory" command as suggested by @Rasman but got the following error:

??? Error using ==> memory Function MEMORY is not available on this platform.

I am running Matlab 2010b for Mac OS X 64-bit.

like image 226
hpy Avatar asked Dec 22 '22 14:12

hpy


1 Answers

Use unix('vm_stat'); in MATLAB on a Mac. This gives, for example:

Mach Virtual Memory Statistics: (page size of 4096 bytes)
Pages free:                        1580152.
Pages active:                       184679.
Pages inactive:                      64572.
Pages speculative:                   63389.
Pages wired down:                   203816.
"Translation faults":              3906655.
Pages copy-on-write:                301846.
Pages zero filled:                 1899205.
Pages reactivated:                       0.
Pageins:                            107102.
Pageouts:                                0.
Object cache: 15 hits of 32166 lookups (0% hit rate)

Results are in pages of 4096 bytes, so multiply results by 4096 and you get values consistent with Activity Monitor (you have to add 'speculative' to 'free' to get exact agreement). If you just want the available memory, you can use unix('vm_stat | grep free');. If you want a number you could use something like:

[s,m]=unix('vm_stat | grep free');
spaces=strfind(m,' ');
str2num(m(spaces(end):end))*4096

EDIT: in response to a comment below "It doesn't tell you how much MATLAB used up and how much more MATLAB can use." Here is what I do for that additional question.

From my experience, 64 bit MATLAB can use up all of the free memory (and more but it slows down a lot if you start swapping much). One of my systems has 22Gb and it has no trouble using all of that. If you're using 32 bit MATLAB you're limited to 2Gb.

To see the total memory, you can add up 'free'+'active'+inactive'+'speculative'+'wired' from vm_stat (and multiply by 4096). Or, if you just want the total memory, you can use unix('sysctl hw.memsize | cut -d: -f2') (in bytes).

To get the memory used by MATLAB, is slightly more involved. The memory is used by the controlling process. If you just use unix('ps'), you'll get the memory used by matlab_helper. So I use:

% get the parent process id
[s,ppid] = unix(['ps -p $PPID -l | ' awkCol('PPID') ]); 
% get memory used by the parent process (resident set size)
[s,thisused] = unix(['ps -O rss -p ' strtrim(ppid) ' | awk ''NR>1 {print$2}'' ']); 
% rss is in kB, convert to bytes 
thisused = str2double(thisused)*1024 

Above I've used a little awk function which picks off a named column:

function theStr = awkCol(colname)
theStr  = ['awk ''{ if(NR==1) for(i=1;i<=NF;i++) { if($i~/' colname '/) { colnum=i;break} } else print $colnum }'' '];

A little tutorial of the unix command to explain the above, in case it helps anyone. unix('command') on its own shows you the output and returns the status. If you want to process the output, use [s,w] = unix('command') and deal with the string output in w. If you want to ignore the s output, in later versions of MATLAB, you can use [~,w] = unix('command'), but I avoid that since I inevitably have different versions on different computers.

like image 152
Ramashalanka Avatar answered Jan 18 '23 23:01

Ramashalanka