Re:How OpenBSD Leverages Malloc to Find Bugs
by ajross on Monday November 28, 2011 @12:24PM
(#3286702)
OK, I understand. The symptom you're describing isn't really "fragmentation". Fragmentation is the inability to use smaller blocks of memory because the larger allocations won't fit. That behavior isn't changed by this.
You're talking about a resource reclamation issue. Unmapping a page is a clear signal to the kernel that the memory is unused and can be repurposed immediately. Otherwise, it needs to find and eject a page from memory using the VM system, which is more expensive (though I'd guess not a lot more expensive except in pathologically allocation-bound systems).
You are right, it's not fragmentation but resource reclamation. I've added a note to the initial post.
I'm not sure though that the VM system can reclaim memory in the heap allocated with sbrk(). At least I've never seen that before. Or do you mean "eject" as in swap out?
Re:How OpenBSD Leverages Malloc to Find Bugs
by lflux on Monday November 28, 2011 @01:06PM
(#3286882)
The mmap() implementation is awesome. I used to write code for a network hardware management computer which ran OpenBSD on a memory-constrained Soekris (quasi-embedded environment more or less). The fact that OpenBSD's implementation returns memory when allocations are free()'d saved my bacon more than once. With another malloc I'd be out of memory way sooner.
Re:How OpenBSD Leverages Malloc to Find Bugs
by ajross on Monday November 28, 2011 @02:55PM
(#3287326)
OpenBSD can't swap?
Re:How OpenBSD Leverages Malloc to Find Bugs
by lflux on Monday November 28, 2011 @04:55PM
(#3287803)
It can, but this system was running on a Compact Flash (real small, I think maybe 32M usable space) so swapping was out of the question. This was also before CF got good and started mapping in good sectors when bad got overwritten.
The problem with sbrk() is that if you allocate a few megabytes of memory + a tiny chunk, even after freeing everything apart from the tiny chunk near the "program break", nothing will be given back to the OS. If you use mmap() instead though, all pages can be given back apart from the one where the tiny chunk resides in. This makes for a tremendous difference sometimes.