调用 free 或 delete 是否将内存释放回“系统"? [英] Does calling free or delete ever release memory back to the "system"

查看:26
本文介绍了调用 free 或 delete 是否将内存释放回“系统"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的问题:调用 free 或 delete 是否会将内存释放回系统".系统我的意思是,它会减少进程的数据段吗?

Here's my question: Does calling free or delete ever release memory back to the "system". By system I mean, does it ever reduce the data segment of the process?

让我们考虑 Linux 上的内存分配器,即 ptmalloc.

Let's consider the memory allocator on Linux, i.e ptmalloc.

据我所知(如果我错了,请纠正我),ptmalloc 维护一个内存块的空闲列表,当内存分配请求到来时,它会尝试从这个空闲列表中分配一个内存块(我知道,分配器比这复杂得多,但我只是用简单的词来表达).但是,如果它失败了,它会使用 sbrk 或 brk 系统调用从系统中获取内存.当内存被释放时,该块被放置在空闲列表中.

From what I know (please correct me if I am wrong), ptmalloc maintains a free list of memory blocks and when a request for memory allocation comes, it tries to allocate a memory block from this free list (I know, the allocator is much more complex than that but I am just putting it in simple words). If, however, it fails, it gets the memory from the system using say sbrk or brk system calls. When a memory is free'd, that block is placed in the free list.

现在考虑这个场景,在峰值负载时,大量的对象被分配到堆上.现在,当负载减少时,对象就被释放了.所以我的问题是:一旦对象被释放,分配器会做一些计算来确定它是否应该只将该对象保留在空闲列表中,或者取决于空闲列表的当前大小,它可能决定将该内存返回给系统即使用sbrk或brk减少进程的数据段?

Now consider this scenario, on peak load, a lot of objects have been allocated on heap. Now when the load decreases, the objects are free'd. So my question is: Once the object is free'd will the allocator do some calculations to find whether it should just keep this object in the free list or depending upon the current size of the free list it may decide to give that memory back to the system i.e decrease the data segment of the process using sbrk or brk?

glibc 的文档告诉我,如果分配请求远大于页面大小,它将使用 mmap 进行分配,并在释放后直接释放回系统.凉爽的.但是假设我从不要求分配大于 50 字节的大小,并且在系统的峰值负载时我会要求很多这样的 50 字节对象.然后呢?

Documentation of glibc tells me that if the allocation request is much larger than page size, it will be allocated using mmap and will be directly released back to the system once free'd. Cool. But let's say I never ask for allocation of size greater than say 50 bytes and I ask a lot of such 50 byte objects on peak load on the system. Then what?

据我所知(请纠正我),用 malloc 分配的内存永远不会释放回系统,直到进程结束,即如果我释放它,分配器只会将它保留在空闲列表中.但是困扰我的问题是,如果我使用工具来查看进程的内存使用情况(我在 Linux 上使用 pmap,你们使用什么?),它应该始终显示峰值负载时使用的内存(因为内存永远不会返还给系统,除非使用 mmap 分配时)?那是进程使用的内存永远不应该减少(堆栈内存除外)?是吗?

From what I know (correct me please), a memory allocated with malloc will never be released back to the system ever until the process ends i.e. the allocator will simply keep it in the free list if I free it. But the question that is troubling me is then, if I use a tool to see the memory usage of my process (I am using pmap on Linux, what do you guys use?), it should always show the memory used at peak load (as the memory is never given back to the system, except when allocated using mmap)? That is memory used by the process should never ever decrease(except the stack memory)? Is it?

我知道我遗漏了一些东西,所以请说明这一切.

I know I am missing something, so please shed some light on all this.

专家,请澄清我对此的概念.我会很感激.我希望我能够解释我的问题.

Experts, please clear my concepts regarding this. I will be grateful. I hope I was able to explain my question.

推荐答案

malloc 没有太多开销,因此您不太可能实现任何运行时节省.然而,在 malloc 之上实现分配器有一个很好的理由,那就是能够跟踪内存泄漏.例如,您可以在程序退出时释放程序分配的所有内存,然后检查您的内存分配器是否调用了 balance(即分配/解除分配的调用次数相同).

There isn't much overhead for malloc, so you are unlikely to achieve any run-time savings. There is, however, a good reason to implement an allocator on top of malloc, and that is to be able to trace memory leaks. For example, you can free all memory allocated by the program when it exits, and then check to see if your memory allocator calls balance (i.e. same number of calls to allocate/deallocate).

对于您的具体实现,没有理由 free() 因为 malloc 不会释放到系统内存,因此它只会将内存释放回您自己的分配器.

For your specific implementation, there is no reason to free() since the malloc won't release to system memory and so it will only release memory back to your own allocator.

使用自定义分配器的另一个原因是您可能分配了许多相同大小的对象(即您有一些分配了很多的数据结构).您可能希望为这种类型的对象维护一个单独的空闲列表,并且仅从这个特殊列表中进行空闲/分配.这样做的好处是可以避免内存碎片.

Another reason for using a custom allocator is that you may be allocating many objects of the same size (i.e you have some data structure that you are allocating a lot). You may want to maintain a separate free list for this type of object, and free/allocate only from this special list. The advantage of this is that it will avoid memory fragmentation.

这篇关于调用 free 或 delete 是否将内存释放回“系统"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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