3 ms·
Without compaction this is often very difficult to do because you don't have large chunks of completely empty memory pages that are convenient to return to the
by xenadu02 9y ago
Without compaction this is often very difficult to do because you don't have large chunks of completely empty memory pages that are convenient to return to the OS; often you end up with a single object on an otherwise empty page, or a free block header on the otherwise empty page. If your malloc library coalesces free regions (or doesn't store its metadata in the heap at all) then this may be a bit easier.
In any case, the GC or malloc lib can use madvise() or posix_madvise() with MADV_DONTNEED. This tells the OS that one or more pages are now junk and can be forgotten, but it leaves the pages mapped. If the process touches them again the kernel will page fault and re-allocate a new page of physical memory but otherwise the physical memory is available for the OS to re-use without swapping to storage.
Modern well-designed VMs will defer acting on this advice unless it actually needs the memory. If the process touches the pages before they are reclaimed very little actual work is performed.
Compacting GCs are another story; they can always wait some time period after a new highwater mark is reached then madvise() on the empty heap areas with little penalty.
I'll also caution people to remember that free memory is wasted memory. If a program isn't using it then the file cache should be.