Does free() free the memory immediately

358 views Asked by At

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

There are 1 answers

0
user3344003 On

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.