malloc 申请得到的内存后,再 free 释放它的时候,操作系统会立即收回那块内存吗?
stackoverflow上的回答:
In many malloc/free implementations, free does normally not return the memory to the operating system (or at least only in rare cases). The reason is, that you will get gaps in your heap and thus it can happen, that you just finish off your 2 or 4 GB of virtual memory with gaps. This should be avoided of course, since as soon as the virtual memory is finished, you will be in really big trouble. The other reason of course is, that the OS can only handle memory chunks that are of a specific size and alignment. To be specific: Normally the OS can only handle blocks that the virtual memory manager can handle (most often multiples of 512 Bytes eg. 4KB).
So returning 40 Bytes to the OS will just not work. So what does free do?
Free will put the memory block in its own free block list. Normally it also tries to meld together adjacent blocks in the address space. The free block list is just a circular list of memory chunks which have of course some administrative data in the beginning. This is also the reason, why managing very small memory elements with the standard malloc/free is not efficient. Every memory chunk needs additional data and with smaller sizes more fragmentation happens.
The free-list is also the first place that malloc looks at when a new chunk of memory is needed. It is scanned before it calls for new memory from the OS. When a chunk is found that is bigger then the needed memory, it is just divided into two parts. One is returned to caller, the other is put back into the free list.
There are many different optimizations to this standard behaviour (for example for small chunks of memory). But since malloc and free must be so universal, the standard behaviour is always the fallback when alternatives are not usable. There are also optimizations in handling the free-list -- for example storing the chunks in lists sorted by sizes. But all optimizations also have their own limitations.
首先malloc()/free()的操作取决于操作系统和编译器的实现。一般来说当你调用malloc(),系统会从堆中给你分配一块足够大的空闲内存,并返回指向它的指针,并且标记它不再空闲。当调用free(),系统检查这块内存的大小,并把它加入到free列表中,而不是立即回收它的内存,因为操作系统只能处理特定大小且连续的内存块:一般来说是512Bytes的倍数。Free内存块链表的另一个作用是,当调用malloc()时,系统会首先从这个表中查找符合要求的内存块,如果找不到适合大小的内存块再向操作系统申请新的内存空间。
malloc 申请得到的内存后,再 free 释放它的时候,操作系统会立即收回那块内存吗?的更多相关文章
- 《C语言中分配了动态内存后一定要释放吗?》
问:比如main函数里有一句 malloc(),后面没有free()1.那么当main结束后,动态分配的内存不会随之释放吗?2.如果程序结束能自动释放,那么还加上free(),是出于什么考虑? 答: ...
- C++ - free()函数释放内存后的指针行为
一个指针释放后不置空的后果: free(p)之后原本那块内存的数据已经被释放了,内存重新收回.但此时的指针变量依然指向那块内存,在以后的代码中若不小心继续调用指针变量,会出现不可预料的错误. 不置空的 ...
- 有关于malloc申请内存和free内存释放
malloc工作机制: malloc函数的实质体现在,它有一个将可用的内存块连接为一个长长的列表的所谓空闲链表(堆内存).调用malloc函数时,它沿连接表寻找一个大到足以满足用户请求所需要的内存块. ...
- 【VS开发】malloc申请内存错误分析
每个进程会有4G的虚拟地址空间, malloc得到的的地址都是虚拟地址, 并且当malloc的时候, 操作系统并不会将实际的内存分配给进程的, 所以malloc只会占用进程自身的虚拟地址空间.我以前也 ...
- 关于malloc申请的动态内存的问题
http://bbs.bccn.net/thread-331344-1-1.html #include<stdio.h>#include<stdlib.h>int main(v ...
- PE文件从文件加载到内存,再从内存读取,然后存盘到文件
// mem.cpp : 定义控制台应用程序的入口点. //PE文件从文件加载到内存,再从内存读取,然后存盘到文件 #include "stdafx.h" #include < ...
- ndk学习之C语言基础复习----虚拟内存布局与malloc申请
在这一次中来学习一下C语言的内存布局,了解它之后就可以解释为啥在用malloc()申请的内存之后需要用memset()来对内存进行一下初始化了,首先来了解一下物理内存与虚拟内存: 物理内存:通过物理内 ...
- Android ADT中增大AVD内存后无法启动:emulator failed to allocate memory 8 (转)
Android ADT中增大AVD内存后无法启动:emulator failed to allocate memory 8http://www.crifan.com/android_emulator_ ...
- 【已解决】Android ADT中增大AVD内存后无法启动:emulator failed to allocate memory 8
[问题] 折腾: [已解决]Android ADT中增大AVD内存后无法启动:emulator failed to allocate memory 8 过程中,增大对应AVD的内存为2G后,结果无法启 ...
随机推荐
- xshell使用命令总结
这个工具主要是链接linux 并且可以从linux上面下载文件到本地 还有上传本地文件到linux上面 下载首先需要压缩打包命令为: tar -cf am_mailer.tar * sz am_mai ...
- Ibatis代码自动生成工具——Abator安装与应用实例(图解)
Abator 能自动生成DAO,DTO和sqlMap,大大提高开发效率.Abator 的官方网站:http://ibatis.apache.org/ibator.html 使用也比较简单,以下做个实例 ...
- 常用的sql server规范
常见的字段类型选择1.字符类型建议采用varchar/nvarchar数据类型2.金额货币建议采用money数据类型3.科学计数建议采用numeric数据类型4. 自增长标识建议采用bigint数据类 ...
- 【整理】SQLServer查询各种数据库对象(表,索引,视图,图表,存储过程等)
首先明确数据库对象的定义:数据库对象定义数据库内容的结构.它们包含在数据库项目中,数据库项目还可以包含数据生成计划和脚本. 常见的数据库对象包括:表,索引,视图,图表,缺省值,规则,触发器,存储过程, ...
- sql server存储过程分页
Create PROCEDURE [dbo].[Table_GetList] ) = '', -- 查询条件(注意: 不要加 WHERE) ) = '', -- 设置排序 , -- 页尺寸 , -- ...
- Mantis的附件图片实现预览/弹出层动画效果预览图片(LightBox2)的完美解决方案[Z]
方法1: 在Mantis的配置文件中,加入此句,将这个值设的很大,就可以直接看到图片 1 $g_preview_attachments_inline_max_size=1000000; 效果如图 这个 ...
- C++ Primer 读书笔记: 第9章 顺序容器
第9章 顺序容器 引: 顺序容器: vector 支持快速随机访问 list 支持快速插入/删除 deque 双端队列 顺序容器适配器: stack 后进先出栈 queue 先进先出队列 priori ...
- 如何在MyEclipse中部署struts2的环境
总记不住一些部署struts2框架的细节,下面就做一个总结:其实很简单,只要几步:1.下载的strutsXXX(版本号)-zip文件中解压app目录中有一个struts2-blank.war文件,解压 ...
- 幻世(OurDream)2D图形引擎大更新——炫丽粒子特效强势回归!
本次更新终于让各位期待已久的绚丽粒子系统特效强势回归到幻世当中了.凭借新引擎强大而又高效的绘图,新的粒子系统将比旧有版本(原Ycnd 2D)在性能上有极大幅度的增强,增幅超过十倍! 更强的性能!更好的 ...
- nodeJs入门笔记(一)
node将"HTTP服务器"这一层抽离,直接面向浏览器用户 如PHP运行之前先要配置一个功能强大而复杂的HTTP 服务器,譬如Apache.IIS 或Nginx,还需要将PHP 配 ...