new了以后,马上delete,但是用prstat, pmap查看,内存不减少,为什么?

cooldong 2005-02-23 04:12:23
new了以后,马上delete,但是用prstat, pmap查看,内存不减少,为什么?

1、
void f()
{

char * p = new char[1024 * 1024 * 4]; //用prstat,pmap查看,内存增加了4M

sleep(5);

delete [] p; //delete完以后,但是用prstat,pmap查看,内存没有减少
}

2、
main()
{
while (1)
{
f (); //这里虽然死循环,但是内存只涨了一次,4M,以后就没有关系了。
}
}

为什么?

3、
同样的程序我在windows下测试,就没有这个现象:new ---- +4M, delete -------- -4M

分不够再加,如果你在深圳,我请你吃饭
...全文
772 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
XtremeRun 2005-02-28
  • 打赏
  • 举报
回复
blueflame 2005-02-25
  • 打赏
  • 举报
回复
gz
gis_2000 2005-02-25
  • 打赏
  • 举报
回复
从这个case看,在没有调用new char的时候系统会分配1112k的地址空间,在调用
new char以后,new char会产生一个heap,地址空间大小是1032k。但是地址空间
不代表实际分配的内存,内存是有系统核心分配的,是动态的。而且在一个程序中
循环调用new char和多个并行进程调用new char是个不同的,也不是一个概念。

现在解释一下pmap -x的输出项的含义,
Address 就是一个进程不同段的地址空间。
Kbytes 就是每个段的地址空间的大小
Resident 表示在内存中驻留的段的空间

shared 表示这些北分配的内存是被系统中其他进程共享的。

private 表示只能被该进程使用的空间大小。你可以发现share的空间不具有
private的属性。

Prstat -LP 的输出的意义是:
size 就是该进程占用的地址空间。
RSS 实际被分配的内存的大小。

你看到的resident和RSS不同,是RSS是进程在内存中的实际的大小,这个数值最大
可以达到Resident显示数值。
cooldong 2005-02-24
  • 打赏
  • 举报
回复
请大家继续顶吧。顶就给分。
下面贴出我的代码,和测试结果。请各位大侠验证一下。谢谢。
(直接 CC filename即可编译,非常简单)


#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

void test1()
{
char *pChar = new char[1024 * 1024]; //这里new 了1M的内存

delete[] pChar; //这里delete
}

int main()
{
sleep(20); //程序一启动的时候,我用pmap -x procid命令查看,查看信息如下A所示

printf("here, test begin:\n");
while(1)
{
test1();
printf("new and delete 1M");//这里再用pmap -x procid命令查看,查看信息如下B所示
sleep(5);
}
}


程序一启动的时候,我用pmap -x procid命令查看
zhoulang/home/zhoulang >pmap -x 28521
28521: a.out
Address Kbytes Resident Shared Private Permissions Mapped File
00010000 8 8 8 - read/exec a.out
00020000 8 8 - 8 read/write/exec a.out
FF200000 688 616 616 - read/exec libc.so.1
FF2BC000 32 32 - 32 read/write/exec libc.so.1
FF300000 16 16 16 - read/exec libc_psr.so.1
FF320000 8 8 8 - read/exec libdl.so.1
FF330000 88 80 80 - read/exec libm.so.1
FF354000 8 8 - 8 read/write/exec libm.so.1
FF360000 40 40 40 - read/exec libCrun.so.1
FF378000 16 16 - 16 read/write/exec libCrun.so.1
FF37C000 16 - - - read/write/exec libCrun.so.1
FF390000 8 8 8 - read/exec libw.so.1
FF3A0000 8 8 - 8 read/write/exec [ anon ]
FF3B0000 152 152 152 - read/exec ld.so.1
FF3E6000 8 8 - 8 read/write/exec ld.so.1
FFBEE000 8 8 - 8 read/write/exec [ stack ]
-------- ------ ------ ------ ------
total Kb 1112 1016 928 88
(可以看到,是1112)


程序执行完test1以后,pmap -x procid命令查看,可以看到变为了2144
(当然以后每次执行完test1以后查看,都是这样了,不再变化)

zhoulang/home/zhoulang >pmap -x 28521
28521: a.out
Address Kbytes Resident Shared Private Permissions Mapped File
00010000 8 8 8 - read/exec a.out
00020000 8 8 - 8 read/write/exec a.out
00022000 1032 16 - 16 read/write/exec [ heap ]
FF200000 688 616 616 - read/exec libc.so.1
FF2BC000 32 32 - 32 read/write/exec libc.so.1
FF300000 16 16 16 - read/exec libc_psr.so.1
FF320000 8 8 8 - read/exec libdl.so.1
FF330000 88 80 80 - read/exec libm.so.1
FF354000 8 8 - 8 read/write/exec libm.so.1
FF360000 40 40 40 - read/exec libCrun.so.1
FF378000 16 16 - 16 read/write/exec libCrun.so.1
FF37C000 16 - - - read/write/exec libCrun.so.1
FF390000 8 8 8 - read/exec libw.so.1
FF3A0000 8 8 - 8 read/write/exec [ anon ]
FF3B0000 152 152 152 - read/exec ld.so.1
FF3E6000 8 8 - 8 read/write/exec ld.so.1
FFBEE000 8 8 - 8 read/write/exec [ stack ]
-------- ------ ------ ------ ------
total Kb 2144 1032 928 104


----------------------------------------------------------
----------------------------------------------------------

我又执行了一遍a.out,这次用prstat -Lp procid

程序一起动的时候:是1112K
PID USERNAME SIZE RSS STATE PRI NICE TIME CPU PROCESS/LWPID
29139 root 1112K 648K sleep 46 4 0:00.00 0.0% a.out/1

程序执行完test1以后:变为2144K(当然以后就不再变化了)

PID USERNAME SIZE RSS STATE PRI NICE TIME CPU PROCESS/LWPID
28928 root 2144K 752K sleep 46 4 0:00.00 0.0% a.out/1
blueflame 2005-02-24
  • 打赏
  • 举报
回复
估计是prstat, pmap有个采样时间间隔导致的
tengulre 2005-02-24
  • 打赏
  • 举报
回复
up
我不懂电脑 2005-02-24
  • 打赏
  • 举报
回复
会有一个滞后的。
city_goal 2005-02-24
  • 打赏
  • 举报
回复
linux 内存管理的机制问题,不是马上回收释放的内存,而是再适当的时候才回收
genesisking 2005-02-24
  • 打赏
  • 举报
回复
unix好像应该是这样的:
你new了,然后delete,这一快并没有真正释放,delete只是告诉编译器可以重新被使用,没有还给操作系统,所以
main()
{
while (1)
{
f (); //内存只涨了一次,4M
}
}
等你整个程序exit出来后,这4M内存才释放。
gdhyj 2005-02-24
  • 打赏
  • 举报
回复
你试试new前加个getchar()
new完再加个getchar()
delete完再来个getchar()

这个你做完一步看过内存情况后再回到执行程序的地方按下回车,再去看内存情况,
看是否人家说的取样时间间隔的问题!
sharkhuang 2005-02-23
  • 打赏
  • 举报
回复
prstat, pmap不准?学习
大熊猫侯佩 2005-02-23
  • 打赏
  • 举报
回复
这个...顶一下...
cooldong 2005-02-23
  • 打赏
  • 举报
回复
谁来顶啊,顶就给分。

23,110

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧