Getting VSIZE-RSIZE from process id
Getting VSIZE-RSIZE from process id
- Subject: Getting VSIZE-RSIZE from process id
- From: Ben Haller <email@hidden>
- Date: Sun, 9 Jul 2006 19:55:33 -0700
Hi folks. Back on Feb. 20th someone named Malcolm asked:
How can I retrive VSIZE and RSIZE values of a process id? Anyone have
a short example?
The list didn't come up with much at the time (a link to the
source code for "top", and a link to archives that, as far as I could
tell, didn't answer the question); but, needing to do the same, I
found a snippet recently, after much Googling. It's by one Michael
Knight, on his blog here:
http://miknight.blogspot.com/2005_11_01_miknight_archive.html
It gives the VSIZE and RSIZE for the current process, but it looks
trivial to adapt to other processes. For the record, here's Mike's
code, with his permission:
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/sysctl.h>
#include <mach/task.h>
#include <mach/mach_init.h>
void getres(task_t task, unsigned int *rss, unsigned int *vs)
{
struct task_basic_info t_info;
mach_msg_type_number_t t_info_count = TASK_BASIC_INFO_COUNT;
task_info(task, TASK_BASIC_INFO, (task_info_t)&t_info,
&t_info_count);
*rss = t_info.resident_size;
*vs = t_info.virtual_size;
}
void memcheck()
{
unsigned int rss, vs, psize;
task_t task = MACH_PORT_NULL;
if (task_for_pid(current_task(), getpid(), &task) != KERN_SUCCESS)
abort();
getres(task, &rss, &vs);
psize = getpagesize();
fprintf(stderr, "RSS: %u KiB, VS: %u KiB.\n", rss, vs);
}
It seems to work well for me on 10.4.6. And by the way, for those
who recommended Carbon's Process Manager to answer such questions:
for the memory usage of processes under OS X it simply returns zero.
Ah well. Seems like they could have done a bit better than that, but
I guess there's a case to be made for not having multiple APIs
reporting the same numbers...
Enjoy!
Ben Haller
Stick Software
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden