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

发布于 2024-11-09 07:02:16 字数 58 浏览 0 评论 0原文

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

半城柳色半声笛 2024-11-16 07:02:16

这应该可以做到。谷歌搜索结构中字段的确切含义,但从这段代码来看它应该是不言自明的。

#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];

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];
我们只是彼此的过ke 2024-11-16 07:02:16

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

free 不是标准的 UNIX 命令,而是 Linux 独有的命令。你在 BSD 和 OS X 上都找不到它。

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

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

你就会明白了。

要在 C 中以编程方式使用此功能,请参阅 ma​​n sysctlbyname

另外,我不明白 GNOME 系统监视器 在 OS X 上有何帮助。

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

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

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

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

希望有帮助。

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.

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

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

and you'll get the idea.

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

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

df is a good hint, though.

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.

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

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

Hope that helps.

倾听心声的旋律 2024-11-16 07:02:16

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

  • df -h 用于硬盘驱动器使用
  • free 用于 RAM 和 swap 使用

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

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

The usual commands to do that on UNIX are

  • df -h for hard drive usage
  • free for RAM and swap usage

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文