如何在Mac OSX上的Matlab 2010b或更高版本中检查可用内存? [英] How to check available memory in Matlab 2010b or later on Mac OSX?

查看:198
本文介绍了如何在Mac OSX上的Matlab 2010b或更高版本中检查可用内存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2004年的Matlab副本(忘记了版本号,但是肯定是旧的),我可以使用功能memstat"命令来查看Matlab可用的内存量.该命令在Matlab 2010b中不再对我有用,该怎么办才能查看可用内存?谢谢.

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.我按照@Rasman的建议尝试了内存"命令,但收到以下错误:

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

???使用==>内存功能时出错 无法使用此功能的MEMORY 平台.

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

我正在运行适用于64位Mac OS X的Matlab 2010b.

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

推荐答案

在Mac上的MATLAB中使用unix('vm_stat');.例如,这给出了:

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)

结果以4096字节为页面,因此将结果乘以4096,您将获得与活动监视器一致的值(必须将投机性"添加到免费"中以获得确切的一致).如果只需要可用内存,则可以使用unix('vm_stat | grep free');.如果您想要一个电话号码,可以使用类似以下内容的话:

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

针对下面的评论它并没有告诉您MATLAB消耗了多少,MATLAB可以使用多少."这是我针对另一个问题的处理方法.

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.

根据我的经验,64位MATLAB可以用尽所有可用内存(甚至更多,但是如果您开始大量交换,它会减慢很多速度).我的系统之一具有22Gb,使用所有这些都没有问题.如果您使用的是32位MATLAB,则限于2Gb.

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.

要查看总内存,可以将vm_stat中的'free'+'active'+ inactive'+'speculative'+'wired'相加(乘以4096).或者,如果只需要总内存,则可以使用unix('sysctl hw.memsize | cut -d: -f2')(以字节为单位).

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).

要获取MATLAB所使用的内存,要花些时间.该存储器由控制过程使用.如果仅使用unix('ps'),则会获得matlab_helper使用的内存.所以我用:

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 

上面,我使用了一个小的awk函数,该函数会选择一个命名列:

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 }'' '];

unix命令的小教程,以解释上述内容,以防它对任何人有帮助. unix('command')单独显示输出并返回状态.如果要处理输出,请使用[s,w] = unix('command')并处理w中的字符串输出.如果要忽略s输出,则在更高版本的MATLAB中,可以使用[~,w] = unix('command'),但我避免这样做,因为我不可避免地在不同的计算机上使用不同的版本.

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.

这篇关于如何在Mac OSX上的Matlab 2010b或更高版本中检查可用内存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆