简体   繁体   English

如何在 Mac OS 上获得进程的峰值内存?

[英]How I can get peak memory of a process on Mac OS?

In linux, when a process is running, I can check its current memory usage and historically peak memory usage by looking into /proc/self/status .在 linux 中,当一个进程正在运行时,我可以通过查看/proc/self/status来检查其当前的内存使用情况和历史上的峰值内存使用/proc/self/status Are there similar files in mac? mac下有类似的文件吗?

In mac, I found that vmmap pid gives a lot info about memory usage, but it seems peek memory usage of the pid is not monitored.在 mac 中,我发现vmmap pid提供了很多有关内存使用情况的信息,但似乎没有监控 pid 的 peek 内存使用情况。 May I ask if anyone could help me with any command?请问有人可以帮我下命令吗?

A program can use the Mach API to get its own memory statistics.程序可以使用 Mach API 来获取自己的内存统计信息。 For example:例如:

#include <stdio.h>
#include <mach/mach.h>
#include <stdlib.h>

int main(void)
{
    kern_return_t ret;
    mach_task_basic_info_data_t info;
    mach_msg_type_number_t count = MACH_TASK_BASIC_INFO_COUNT;

    ret = task_info(mach_task_self(), MACH_TASK_BASIC_INFO, (task_info_t)&info, &count);
    if (ret != KERN_SUCCESS || count != MACH_TASK_BASIC_INFO_COUNT)
    {
        fprintf(stderr, "task_info failed: %d\n", ret);
        exit(EXIT_FAILURE);
    }

    printf("resident size max: %llu (0x%08llx) bytes\n",
           (unsigned long long)info.resident_size_max,
           (unsigned long long)info.resident_size_max);
    return 0;
}

Alternatively, you can run your program under Instruments, with the Allocations template, to observe its memory usage.或者,您可以在 Instruments 下使用 Allocations 模板运行您的程序,以观察其内存使用情况。 (Xcode itself also has memory gauges, but I don't recall off-hand if it shows peak usage.) (Xcode 本身也有内存计量器,但我不记得它是否显示使用高峰。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM