Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Use <code>unix('vm_stat');</code> in MATLAB on a Mac. This gives, for example:</p> <pre><code>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) </code></pre> <p>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 <code>unix('vm_stat | grep free');</code>. If you want a number you could use something like:</p> <pre><code>[s,m]=unix('vm_stat | grep free'); spaces=strfind(m,' '); str2num(m(spaces(end):end))*4096 </code></pre> <p>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. </p> <p>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. </p> <p>To see the total memory, you can add up 'free'+'active'+inactive'+'speculative'+'wired' from <code>vm_stat</code> (and multiply by 4096). Or, if you just want the total memory, you can use <code>unix('sysctl hw.memsize | cut -d: -f2')</code> (in bytes). </p> <p>To get the memory used by MATLAB, is slightly more involved. The memory is used by the controlling process. If you just use <code>unix('ps')</code>, you'll get the memory used by <code>matlab_helper</code>. So I use:</p> <pre><code>% 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&gt;1 {print$2}'' ']); % rss is in kB, convert to bytes thisused = str2double(thisused)*1024 </code></pre> <p>Above I've used a little <code>awk</code> function which picks off a named column:</p> <pre><code>function theStr = awkCol(colname) theStr = ['awk ''{ if(NR==1) for(i=1;i&lt;=NF;i++) { if($i~/' colname '/) { colnum=i;break} } else print $colnum }'' ']; </code></pre> <p>A little tutorial of the <code>unix</code> command to explain the above, in case it helps anyone. <code>unix('command')</code> on its own shows you the output and returns the status. If you want to process the output, use <code>[s,w] = unix('command')</code> and deal with the string output in <code>w</code>. If you want to ignore the <code>s</code> output, in later versions of MATLAB, you can use <code>[~,w] = unix('command')</code>, but I avoid that since I inevitably have different versions on different computers.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload