#Should free() return memory to the OS?

15 messages · Page 1 of 1 (latest)

twilit sparrow
#

When implementing free() should I make it munmap() free bytes to the os? Should I munmap() when a whole page is free or just chunks of memory (which wouldn’t that break the program)? Or should I just allow the user to hoard massive amounts of memory not in use? Ty!!!

stone sinewBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

opaque lily
twilit sparrow
#

If I munmap a chunk of size 32 (chunk is marked as free) and then I want to allocate more mem of size 32, how will that memory be usable if it’s munmapedz Wouldn’t it be an access violation or something?

tranquil cosmos
twilit sparrow
#

Ooh okay

tranquil cosmos
#

and munmap will unmap the entire region originally allocated by mmap

tall sinew
#

If you have a normal allocation setup this will be either semi-expensive to compute or end up fragmenting your arenas

twilit sparrow
#

About the fragments, does free() bother coalescing freed chunks that are separated by unfreed chunks? Like <free(32), unfree(32), free(32), unfree(32)> -> <free(64), unfree(32), unfree(32)> ? It seems exhaustive unless it somehow doesn’t move the unfreed memory over

heavy carbon
#

You can't just move the chunks, all the pointers would get broken

#

I'm not sure what you're implying

twilit sparrow
#

Yea nvm then I figured. I would be like when you realloc and it memcpys it over

#

Ty

#

!solved