프로세스당 메모리 사용량을 정확하게 알수 있는 방법?

익명 사용자의 이미지

리눅스에서 특정 프로세스당 얼마만큼의 메모리를 사용하고 있는지 주기적으
로 검사하고 싶습니다.
top이나 ps -aux 로 보면 메모리 사용량이 퍼센트(%)단위로 나오는데요.
메모리사용량을 바이트 또는 킬로바이트 단위로 알수 있는 방법이 있습니까?

익명 사용자의 이미지


man 2 getrusage

sun1226의 이미지

...

tiffang의 이미지

특히 메모리 사용량은 아무리 많이 호출해도 0으로 나옵니다.
http://kldp.org/comment/reply/19857/39728

harisoo의 이미지

/proc/pid/status를 파싱하면 메모리 사용량을 알 수 있습니다.

kpserv의 이미지

#include
#include

int main(int argc, char *argv[])
{
struct mallinfo mem_info;
char *temp;
int i;

if ( argc != 2 )
return 0;

temp = (char*)malloc(atoi(argv[1]));
memset(temp, 0, atoi(argv[1]));
mem_info = mallinfo();

printf("(%d) This is the total size of memory allocated with sbrk by malloc, in bytes.\n", mem_info.arena);
printf("(%d) This is the number of chunks not in use.\n", mem_info.ordblks);
printf("(%d) This field is unused.\n", mem_info.smblks);
printf("(%d) This is the total number of chunks allocated with mmap.\n", mem_info.hblks);
printf("(%d) This is the total size of memory allocated with mmap, in bytes.\n", mem_info.hblkhd);
printf("(%d) This field is unused.\n", mem_info.usmblks);
printf("(%d) This field is unused.\n", mem_info.fsmblks);
printf("(%d) This is the total size of memory occupied by chunks handed out by malloc.\n", mem_info.uordblks);
printf("(%d) This is the total size of memory occupied by free (not in use) chunks.\n", mem_info.fordblks);
printf("(%d) This is the size of the top-most releasable chunk that normally borders the end of the heap.\n", mem_info.keepcost);

return 0;
}

정확한 답은 아니지만 동적인 메모리 현황을 아는 방법은 위와 같이 하면 됩니다..
걍.. 참고하시라는 의미로^^

#define DEBUG printf( "%s, %s, %d\n", __FILE__, __FUNCTION__, __LINE__ );

tiffang의 이미지

커널 코드의 linux/fs/proc/array.c 에서 proc_pid_statm 을 참고 하세염
단지 아래 문장 한줄 만 찍어줍니다.
sprintf(buffer,"%d %d %d %d %d %d %d\n", size, resident, shared, text, lib, data, 0);

sscanf 로 파싱하기 딱!