首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

c++内存储器delete了,但是程序的内存并没有变小

2014-01-05 
c++内存delete了,但是程序的内存并没有变小一个int, listchar* 的map,向里面同一个key插入10000条char

c++内存delete了,但是程序的内存并没有变小
一个<int, list<char*>> 的map,向里面同一个key插入10000条char[500*1024]的数据,然后释放,如此循环,发现第一次释放时,内存变小了,第二次以后,内存不在变化,一直保持有数据时的内存大小。请教各位大神,有没有知道原因的!!!
[解决办法]
本来就是:你释放了,系统不一定释放
[解决办法]
你释放了,操作系统还有内存用,没回收你的而已。
[解决办法]
看看《程序员的自我修养》里的加载部分就能明白了。在运行main函数前有内存申请操作,在main函数后有释放操作,你这个释放只是程序里释放了。并未真正还给内存。
[解决办法]
1.如上所说,你释放了内存,系统知道了,这块内存已经留作他用了,究竟留作什么用,还有什么时候留作他用,您就甭操心啦。哈

2.release模式下,可能要马上释放,但我不确定。你试一试。
[解决办法]


????????send_msg_queue*?tmp?=?iter->second;
????????if(?tmp?!=?NULL?)
????????{
????????????queue_node*??tmp_node?=?NULL;
?
????????????while(?!tmp->msg_queue.empty()?)
????????????{
????????????????tmp_node?=?tmp->msg_queue.front();
?
????????????????if(?NULL?==?tmp_node?)
????????????????{
????????????????????continue;
????????????????}
?
????????????????delete?tmp_node;
????????????????tmp->msg_queue.pop_front();
????????????}
????????????delete?tmp; 
????????????m_cache.erase(iter); //这东西绝对不应该放在if里面吧
????????}

就像我上面注释所说,你的程序逻辑可能有问题,如果tmp是空的,你的程序就不会删除相应cache,再插入时可能会认为相应的数据已经存在而不再真的插入,至于你具体怎么插入数据我不知道,所以我说可能。
[解决办法]
You can not assume that just after doing the free the memory will be returned back to OS. Generally the CRT implementation have some optimization because of which they may not return this memory immediately. This allows the CRT to allocate the subsequent memory allocation requests in a faster way.
[解决办法]
for very good reasons, the memory allocator does not return memory to the host OS but keeps it (internally in your program's data space) as a free list of some kind.

Some of the reasons the library keeps the memory are:

Interacting with the kernel is much slower than simply executing library code
The benefit would be small. Most programs have a steady-state or increasing memory footprint, so the time spent analyzing the heap looking for returnable memory would be completely wasted.
Internal fragmentation makes page-aligned blocks (the only thing that could be returned to the kernel) unlikely to exist, another reason not to slow the program down looking for something that won't be there.
Returning a page embedded in a free block would fragment the low and high parts of the block on either side of the page.
The few programs that do return large amounts of memory are likely to bypass malloc() and simply allocate and free pages anyway using mmap(2).

热点排行