cmemorygarbage-collectionboehm-gc

Does Boehm GC release memory?


I've been looking at the Boehm GC (for C/C++) and it seems to me that (on Windows, but probably on linux as well) the GC, by default, never releases the memory it has asked for. Worth to note is that I'm on an older version of Boehm GC (7.6).

It seems that the part that deals with memory allocations is located in os_dep.c, on windows it looks like memory is allocated via VirtualAlloc. I can see that the GC locks memory in GC_win32_get_mem as I create new objects. In order for that memory to be returned there would need to be a call to VirtualFree but that never seems to happen (there is a GC_win32_free_heap function but that seems to only be used by some of the GC libs test suite).

There is a cmake flag - USE_MUNMAP. From the documentation it seems that using this flag, the GC will return some of the memory it has allocated sometimes. Here I can see that VirtualFree is called from some of the code that is inside of the #ifdef in os_dep.c and building with this flag I can debug it and see it now makes calls to VirtualFree sometimes.

Am I correct to understand that by default, the Boehm GC does'nt release memory that is has allocated? Does anyone know more about the "USE_MUNMAP" flag - is the only difference using this flag that the GC will (on certain occasions when some criterias are meet) return memory that is has allocated, or is there more to it? It would also be interesting to know if this design was choosen for performance reasons, I can imagine that calling VirtualFree will induce some performance overhead and perhaps memory fragmentation? Is that the reason why per default it does'nt return memory at all or is there some other reason?

If anyone can shed some light on this I'd be very grateful.

(Edited 2024-07-03, I had some mentioning of OS before in this post which probably caused some confusion. I'm only interested in knowing if Boehm GC makes the appropriate free call on memory it has previously allocated when its no longer in use.)


Solution

  • BDWGC v8.0.0 (and later) returns unused memory to OS by default (on most target platforms). For older BDWGC versions you could pass --enable-munmap flag to configure to turn on the feature.