xcode 如何像活动监视器那样以编程方式检查 Mac 上的可用系统内存?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6094444/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 21:05:57  来源:igfitidea点击:

How can I programmatically check free system memory on Mac like the Activity Monitor does?

objective-cxcodecocoamacos

提问by backspacer

On Mac OS X, I can see how much memory is free in Activity Monitor. How can I programmatically do this?

在 Mac OS X 上,我可以看到活动监视器中有多少可用内存。我怎样才能以编程方式做到这一点?

回答by Steven Kramer

This should do it. Google around for the exact meaning of the fields in the structures, but it should be pretty self-explanatory working from this code.

这应该做。谷歌搜索结构中字段的确切含义,但从这段代码开始工作应该是不言自明的。

#import <sys/sysctl.h>
#import <mach/host_info.h>
#import <mach/mach_host.h>
#import <mach/task_info.h>
#import <mach/task.h>
int mib[6]; 
mib[0] = CTL_HW;
mib[1] = HW_PAGESIZE;

int pagesize;
size_t length;
length = sizeof (pagesize);
if (sysctl (mib, 2, &pagesize, &length, NULL, 0) < 0)
{
    fprintf (stderr, "getting page size");
}

mach_msg_type_number_t count = HOST_VM_INFO_COUNT;

vm_statistics_data_t vmstat;
if (host_statistics (mach_host_self (), HOST_VM_INFO, (host_info_t) &vmstat, &count) != KERN_SUCCESS)
{
    fprintf (stderr, "Failed to get VM statistics.");
}

double total = vmstat.wire_count + vmstat.active_count + vmstat.inactive_count + vmstat.free_count;
double wired = vmstat.wire_count / total;
double active = vmstat.active_count / total;
double inactive = vmstat.inactive_count / total;
double free = vmstat.free_count / total;

task_basic_info_64_data_t info;
unsigned size = sizeof (info);
task_info (mach_task_self (), TASK_BASIC_INFO_64, (task_info_t) &info, &size);

double unit = 1024 * 1024;
memLabel.text = [NSString stringWithFormat: @"% 3.1f MB\n% 3.1f MB\n% 3.1f MB", vmstat.free_count * pagesize / unit, (vmstat.free_count + vmstat.inactive_count) * pagesize / unit, info.resident_size / unit];

回答by Ionic

Actually, that's only half true.

事实上,这只说对了一半。

free is not standard UNIX but a Linux-only command. You will neither find it on BSD, nor on OS X.

free 不是标准的 UNIX,而是一个仅限 Linux 的命令。您既不会在 BSD 上找到它,也不会在 OS X 上找到它。

For that matter, a better way to get memory information is through sysctl.

就此而言,获取内存信息的更好方法是通过 sysctl。

I.e. run sysctl -a | grep -Ei "(hw|vm)\..*mem"

即运行 sysctl -a | grep -Ei "(hw|vm)\..*mem"

and you'll get the idea.

你会明白的。

To use this programmatically in C, refer to man sysctlbyname.

要在 C 中以编程方式使用它,请参阅man sysctlbyname

Also, I don't see how GNOME System Monitorhelps on OS X.

另外,我看不到GNOME 系统监视器如何在 OS X 上提供帮助。

df is a good hint, though.

不过,df 是一个很好的提示。

If you just plan to use the shell to gather those data and opt for top, read man top. You can invoke top with -l 1 to get one sample only and limit the process table to, say, 20 processes with -n 20. Keep in mind that you won't get CPU values for procs using only sample, the reason is outlined in the man page.

如果您只是打算使用 shell 来收集这些数据并选择 top,请阅读man top。您可以使用 -l 1 调用 top 以仅获取一个样本,并使用 -n 20 将进程表限制为 20 个进程。请记住,您不会仅使用样本获取 procs 的 CPU 值,原因已概述在手册页中。

A simple example to get some information about memory out of top (complete lines only):

从顶部获取有关内存的一些信息的简单示例(仅限完整行):

top -l1 -n 20 | grep -Ei "mem|vm"

top -l1 -n 20 | grep -Ei "mem|vm"

Hope that helps.

希望有帮助。

回答by tiktak

The usual commands to do that on UNIX are

在 UNIX 上执行此操作的常用命令是

  • df -hfor hard drive usage
  • freefor RAM and swapusage
  • df -h硬盘使用
  • 免费用于 RAM 和交换使用

You would then use/chain one or many of those to extract one of the information given: ack, sed, grep, head, cut, ...

然后,您将使用/链接其中一个或多个来提取给定的信息之一:ack、sed、grep、head、cut、...

Note: If you don't plan to "programmatically" check memory, I would advise you to rather use topto know which processes are using your CPU and RAM. Gnome System Monitoris one of its GUI equivalents.

注意:如果您不打算“以编程方式”检查内存,我建议您宁可使用top来了解哪些进程正在使用您的 CPU 和 RAM。 Gnome 系统监视器是其 GUI 等效项之一。