Fileland
Fileland

Reputation: 351

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. Are there similar files in 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. May I ask if anyone could help me with any command?

Upvotes: 3

Views: 2336

Answers (1)

Ken Thomases
Ken Thomases

Reputation: 90571

A program can use the Mach API to get its own memory statistics. 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. (Xcode itself also has memory gauges, but I don't recall off-hand if it shows peak usage.)

Upvotes: 3

Related Questions