为什么free()函数不将内存返回给操作系统? [英] Why does the free() function not return memory to the operating system?

查看:92
本文介绍了为什么free()函数不将内存返回给操作系统?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Linux上使用顶级终端程序时,看不到免费的结果.

When I use the top terminal program at Linux, I can't see the result of free.

我的期望是:

  1. 免费地图和列表.

  1. free map and list.

我可以在顶部看到的内存使用情况(Linux函数)或/proc/meminfo 变得比过去小.

The memory usage that I can see at the top(Linux function) or /proc/meminfo get smaller than past.

睡眠开始.

程序退出.

但是 程序结束后,内存使用量才会减少.

But The usage of memory only gets smaller when the program ends.

您能解释一下自由功能的逻辑吗?

Would you explain the logic of free function?

下面是我的代码.

for(mapIter = bufMap->begin(); mapIter != bufMap -> end();mapIter++)
{
    list<buff> *buffList = mapIter->second;
    list<buff>::iterator listIter;
    for(listIter = buffList->begin(); listIter != buffList->end();listIter++)
    {
        free(listIter->argu1);
        free(listIter->argu2);
        free(listIter->argu3);
    }
    delete buffList;
}
delete bufMap;

printf("Free Complete!\n");

sleep(10);
printf("endend\n");

谢谢.

推荐答案

内存已分配到.

当您在程序中请求一些内存时(使用new()或malloc()等),您的程序会从其堆中请求一些内存,而堆又会从操作系统中请求内存{1}.由于这是一项昂贵的操作,因此它会从OS中获取大量内存,而不仅仅是您的要求.内存管理器将所有获取的内容放入堆中,只是将您所需的少量返回给您.当您释放()或删除()该内存时,它只会返回到堆,而不是操作系统.

When you request some memory in your program (with a new() or malloc() etc.) Your program requests some memory from its heap, which in turn requests it from the operating system{1}. Since this is an expensive operation, it gets a chunk of memory from the OS, not just what you ask for. The memory manager puts everything it gets into the heap, just returning to you the perhaps small amount you asked for. When you free() or delete() this memory, it simply gets returned to the heap, not the OS.

在您的程序退出之前,不将该内存返回给操作系统是绝对正常的,因为您稍后可能会请求更多的内存.

It's absolutely normal for that memory to not be returned to the operating system until your program exits, as you may request further memory later on.

如果您的程序设计依赖于此内存,那么可以使用运行和退出的程序的多个副本(通过fork()〜ing)来实现.

If your program design relies on this memory be recycled, it may be achievable using multiple copies of your program (by fork()~ing) which run and exit.

{1}程序启动时堆可能是非空的,但是假设它不能说明我的观点.

{1} The heap is probably non-empty on program start, but assuming it's not illustrates my point.

这篇关于为什么free()函数不将内存返回给操作系统?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆