11

Given a PID, how can I get the memory currently used by a process ? Specifically I am looking for:

  • The private physical memory (RAM) used by the process
  • The swap space used by the process

But I am not interested in mapped files and shared memory. In short, I would like to determine how much memory (RAM and swap) would be freed by terminating the PID.

jscs
  • 63,694
  • 13
  • 151
  • 195
  • Have a look at http://stackoverflow.com/questions/131303/linux-how-to-measure-actual-memory-usage-of-an-application-or-process – Parag Bafna Aug 23 '13 at 08:58
  • Thank you, but this article refers to tools that aren't available on mac (/proc, pmap, smem, htop, gcore) or to things such as valgrind which aren't an option. I am mainly looking for a way of finding the memory (RAM) and SWAP used by a process, programatically. PS could work, but it does not report that. I should have tagged objective-c instead of cocoa. But It would not be better. C/c++ calls are good as well as long as they can be placed on a MAC OS platform. –  Aug 26 '13 at 17:58

2 Answers2

8

Would this be useful? You can use the ps command with various options to get at least some of those things:

ps -o rss -o vsz -o pid

will give you the resident set size, the virtual size, and the process ID. I see from the man page that -o paddr gives the swap address, but I don't see which option gives you the swap size.

user1118321
  • 25,567
  • 4
  • 55
  • 86
  • Thank you for your reply. I believe this command will return the address space size ? Does RSS include the shared / mapped memory as well ? (note: `-p pid`). –  Aug 26 '13 at 18:12
  • 4
    Or even simpler: `ps -o rss,vsz,pid`. – kenorb Aug 25 '15 at 10:35
  • As a note if you're using this to get memory programmatically, shelling out to `ps` is extremely expensive https://github.com/schneems/get_process_mem/issues/31 – Schneems Jul 04 '19 at 18:15
6

You can use Mach's task_info call to find this information. Here is code which works on OS X v10.9, and which gets the virtual process size of the current process:

#include <mach/mach.h>
#include <mach/message.h>  // for mach_msg_type_number_t
#include <mach/kern_return.h>  // for kern_return_t
#include <mach/task_info.h>
#include <stdio.h>

int main(void) {
  kern_return_t error;
  mach_msg_type_number_t outCount;
  mach_task_basic_info_data_t taskinfo;

  taskinfo.virtual_size = 0;
  outCount = MACH_TASK_BASIC_INFO_COUNT;
  error = task_info(mach_task_self(), MACH_TASK_BASIC_INFO, (task_info_t)&taskinfo, &outCount);
  if (error == KERN_SUCCESS) {
    // type is mach_vm_size_t
    printf("vsize = %llu\n", (unsigned long long)taskinfo.virtual_size);
    return 0;
  } else {
    printf("error %d\n", (int)error);
    return 1;
  }
}

I think that this excludes shared memory segments, but I'm not sure.

aecolley
  • 1,973
  • 11
  • 10
  • Thanks a ton for this! The code here returns info for the current process. Do you know how to retrieve the resident set size for a different PID on mac? – Schneems Aug 25 '20 at 17:16