mimalloc icon indicating copy to clipboard operation
mimalloc copied to clipboard

Use MADV_FREE in FreeBSD

Open davide-digennaro-nozomi opened this issue 2 years ago • 4 comments

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);

davide-digennaro-nozomi avatar Nov 17 '23 08:11 davide-digennaro-nozomi

Your proposal makes sense, would be nice to come with tests before and after the change, what do you think ?

devnexen avatar Nov 17 '23 09:11 devnexen

Anyhow, snmalloc already uses MADV_FREE for FreeBSD.

devnexen avatar Nov 17 '23 09:11 devnexen

  1. 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...)
  2. how precisely can we induce the allocator to call madvise i.e. force a decommit from c++ code? I'm not sure a plain delete is sufficient.

thanks

davide-digennaro-nozomi avatar Nov 20 '23 13:11 davide-digennaro-nozomi

Hello, sorry to revive an old thread, but the issue is still open. I noticed that the code in v2.17 is slightly different.

  1. I think I only need to change the constant in _mi_prim_decommit. can you confirm?
  2. do I also need to change the value returned in *needs_recommit?

thanks Davide

davide-digennaro-nozomi avatar Jun 26 '24 13:06 davide-digennaro-nozomi