malloc在分配内存时会保留更多空间吗? [英] Does malloc reserve more space while allocating memory?

查看:85
本文介绍了malloc在分配内存时会保留更多空间吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在测试程序中观察到以下行为:

I am observing the following behavior in my test program:

我正在将malloc()占用1 MB,然后在sleep(10)之后执行free().我已经做过五次了.程序运行时,我正在观察top中的内存消耗.

I am doing malloc() for 1 MB and then free() it after sleep(10). I am doing this five times. I am observing memory consumption in top while the program is running.

一旦free() -d,我预计程序的虚拟内存(VIRT)消耗将减少1 MB.但实际上并非如此.它保持稳定.这种现象的解释是什么? malloc()在分配内存时会做一些保留吗?

Once free()-d, I am expecting the program's virtual memory (VIRT) consumption to be down by 1 MB. But actually it isn't. It stays stable. What is the explanation for this behavior? Does malloc() do some reserve while allocating memory?

推荐答案

一旦free() -d,我预计程序的虚拟内存(VIRT)消耗将减少1MB.

Once free()-d, I am expecting program's virtual memory (VIRT) consumption to be down by 1MB.

好吧,这不是C标准所保证的.它只是说,一旦您free()内存,就不应该再访问它了.

Well, this is not guaranteed by the C standard. It only says, once you free() the memory, you should not be accessing that any more.

由内存管理器决定是将内存块实际返回到可用的内存池中还是留作以后的分配.

Whether the memory block is actually returned to the available memory pool or kept aside for future allocations is decided by the memory manager.

这篇关于malloc在分配内存时会保留更多空间吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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