mimalloc
mimalloc copied to clipboard
Use MADV_FREE in FreeBSD
Apparently mimalloc uses MADV_DONTNEED on unix-like platforms in prim.c:
#if defined(MADV_DONTNEED) && MI_DEBUG == 0 && MI_SECURE == 0
// decommit: use MADV_DONTNEED as it decreases rss immediately (unlike MADV_FREE)
// (on the other hand, MADV_FREE would be good enough.. it is just not reflected in the stats :-( )
err = unix_madvise(start, size, MADV_DONTNEED);
however the semantics of MADV_DONTNEED is not identical on every platform. According to man pages:
- MADV_DONTNEED in Linux is equivalent to MADV_FREE in FreeBSD.
- MADV_DONTNEED in FreeBSD does not release the memory back to the OS, it's just a hint that the page may be unloaded from physical memory (so, in particular, if you don't have swap space, it has little impact).
So it would make sense to prefer MADV_FREE (at least) in Freeebsd. Likely, the same may be true also in MacOS, but we didn't test.
What we propose is something like:
#ifndef MADV_DECOMMIT_BEHAVIOR
#if (defined(__FreeBSD__) || defined(__DragonFly__)) && defined(MADV_FREE)
#define MADV_DECOMMIT_BEHAVIOR MADV_FREE
#elif defined(MADV_DONTNEED)
#define MADV_DECOMMIT_BEHAVIOR MADV_DONTNEED
#endif
#endif
#if defined(MADV_DECOMMIT_BEHAVIOR) && MI_DEBUG == 0 && MI_SECURE == 0
err = unix_madvise(start, size, MADV_DECOMMIT_BEHAVIOR);
Your proposal makes sense, would be nice to come with tests before and after the change, what do you think ?
Anyhow, snmalloc already uses MADV_FREE for FreeBSD.
- Is there any other line in the code we should change, in addition to the above
_mi_prim_commit? (I don't think so, but...) - how precisely can we induce the allocator to call
madvisei.e. force a decommit from c++ code? I'm not sure a plaindeleteis sufficient.
thanks
Hello, sorry to revive an old thread, but the issue is still open. I noticed that the code in v2.17 is slightly different.
- I think I only need to change the constant in
_mi_prim_decommit. can you confirm? - do I also need to change the value returned in
*needs_recommit?
thanks Davide