3 ms·
i don't know of any malloc implementations that return memory to the system. the only way to do that is to pass a negative value to sbrk, which requires all th
by jeffffff 14y ago
i don't know of any malloc implementations that return memory to the system. the only way to do that is to pass a negative value to sbrk, which requires all the memory being returned to be at the end of the data segment. even if you free 99% of the memory you were using, if one byte is still in use at the end of the data segment no memory can be returned. this is almost always the case in practice, so no malloc implementations bother to return memory in this way. on the other hand, unmapping an anonymous mmap does return the memory to the system. most mallocs handle large allocations by delegating to anonymous mmap for this reason. you probably could've tweaked the mmap threshold for tcmalloc to get the same result.
edit: apparently tcmalloc is using mmap to allocate more of its memory than i realized, not sure why i thought it was using sbrk for everything
- priteau 14y agoOpenBSD's malloc implementation does: "On a call to free, memory is released and unmapped from the process address space using munmap." http://en.wikipedia.org/wiki/C_dynamic_memory_allocation#OpenBSD.27s_malloc http://en.wikipedia.org/wiki/C_dynamic_memory_allocation#Ope...