在我簡單的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
咦?爲什麼記憶不在這裏釋放?
您不應該將帶有副作用的表達式放入'assert'中。 –
確實夠了。忽略它。但結果卻令人困惑。 –
在「strace」下運行您的程序,以便您可以監視實際上會對RSS值產生影響的mmap/munmap調用。您可以在代碼中放置一些printfs,以便您可以將mmap/munmap與輸出中代碼的特定點進行匹配。 – vanza