Where does freed memory go?

free(3), of course, is a library routine so it stands to reason that when we free up memory, previously allocated by one of the dynamic allocation routines, it does not get freed back to the system, but rather to the process heap (which, of course, is virtual memory).

However, there are at least two cases where this may not occur:

  • If the allocation was satisfied internally via mmap rather than via the heap segment, it gets immediately freed back to the system
  • On modern glibc, if the amount of heap memory being freed is very large, this triggers the return of at least some of the memory chunks back to the OS.

Get Hands-On System Programming with Linux now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.