Does free() free the memory immediately

雨燕双飞 提交于 2020-02-08 03:51:04

问题


In my program, I am using malloc to allocate large amounts of memory (several hundred mbs, in chunks of say 25mb to 75mb at a time), I am subsequently freeing some of the chunks, then again reallocating some more. My question is when I use free() to free memory, does it immediately free the concerned block of memory, or it merely marks it for freeing. If it is merely marking for freeing later, is there some standard C library function to force it to be freed immediately.

I am actually required to develop my program to be portable between linux and vxworks. In Vxworks, in one library I am using(vsipl) , I find 'free' is not freeing up, immediately on the call.


回答1:


The answer depends upon malloc and free implementation. However, I can safely say that in nearly any implementation the memory does not get deallocated. Instead it goes back into a pool where it can be reused by the process.

If you are using large blocks of memory, it is usually better to use operating system memory functions and do your own memory management. However, it is not a good idea to map pages in and out of memory.



来源:https://stackoverflow.com/questions/30860188/does-free-free-the-memory-immediately

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!