내 간단한 C 프로그램 (gnu linux)에서 proc/stat에서 rss 값을 얻고 있습니다.
int GetRSS()
은 내 프로세스에 대한 proc/stat에서 RSS 값을 반환합니다. 이 경우 광기는 무료입니다.
:
printf("A RSS=%i\n", GetRSS());
char *cStr = null;
cStr = malloc(999999);
if (cStr != NULL)
{
printf("B RSS=%i\n", GetRSS());
free(cStr);
printf("C RSS=%i\n", GetRSS());
}
내가 얻을 :
A RSS=980
B RSS=984
C RSS=980
C
이 984
를 반환하지 않은 이유를 설명 할 수 없다. 내가 두 번 같은 절차를 실행하면
내가 얻을 :
A RSS=980
B RSS=984
C RSS=980
B RSS=984
C RSS=980
잘 보인다. 이 경우에
하지만, :
struct _test
{
char *pChar;
}
struct _test **test_ptr;
int i = 0;
printf("D RSS=%i\n",GetRSS());
assert(test_ptr = (struct _test **)malloc((10000) * sizeof(struct _test *)));
for (i = 0; i < 1000; i++)
{
assert(test_ptr[i] = (struct _test *)malloc(sizeof(struct _test)));
test_ptr[i]->pChar=strdup("Some garbage");
}
printf("E RSS=%i\n", GetRSS());
for (i=0; i<1000; i++)
{
free(test_ptr[i]->pChar);
free(test_ptr[i]);
}
free(test_ptr);
printf("F RSS=%i\n", GetRSS());
내가 얻을 :
D RSS=980
E RSS=1024
F RSS=1024
D RSS=1024
E RSS=1024
F RSS=1024
응? 기억이 왜 여기에서 자유롭지 않습니까?
부작용이있는 표현식을 절대로 '단정'해서는 안됩니다. –
사실 충분합니다. 무시해. 여전히 결과는 당황 스럽습니다. –
"strace"아래에서 프로그램을 실행하면 실제로 RSS 값에 영향을 미치는 mmap/munmap 호출을 모니터링 할 수 있습니다.mmap/munmap을 출력에서 코드의 특정 지점과 일치시킬 수 있도록 코드 주위에 printfs를 넣을 수 있습니다. – vanza