在我简单的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