我在 AIX 上运行我的应用程序时看到异常的内存使用模式...
我创建了一个简单的程序来 malloc 和 free 复制相同的问题。
int main()
{
int *ptr_one;
// enter value as 0.
// I wanted few secs fetch the PID of this statndlone process
// and run 'ps -p <PID> -o "vsz rssize"'
long a;
scanf("%ld", &a);
for(;;)
{
if(a < 10000000) a = a + 100;
ptr_one = (int *)malloc(sizeof(int)*a);
if (ptr_one == 0){
printf("ERROR: Out of memory\n");
return 1;
}
*ptr_one = 25;
printf("%d\n", *ptr_one);
free(ptr_one);
}
return 0;
}
我使用下面的命令捕获了这个程序的内存使用情况,
ps -p $1 -o "vsz rssize" | tail -1 >> out.txt
该图告诉我们内存一直在增长并且没有释放。 这是泄漏的迹象还是 AIX 上的正常内存行为?
最佳答案
进程的内存使用大小没有减少是完全正确的:虽然 malloc
可以为进程请求额外的内存,但 free
永远不会将其返回给系统。相反,释放的内存会在以后的 malloc
调用中重复使用。
关于c++ - VSZ 和 RSS 在 AIX 上不断增加,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31649992/