title: Freeing mmaped memory on linux using Python's mmap module
body: <p>I'm using pythons mmap module to share memory between a set of processes. The data being shared is hundreds of GB and does not all fit into memory at once, that being said I'd like to be able free some regions of memory after I'm done working with them.</p>
  <p>As far as I understand it, I'm able to free memory on OSX with the following code. Which appears to work.</p>
  <pre><code>data_size = 10
  data_start = 0
  self.data = mmap.mmap(-1, 20)
  self.data.seek(data_start)
  self.data.write('0000000000')
  self.data.madvise(mmap.MADV_FREE, data_start, data_size)
  </code></pre>
  <p>However, MMADV_FREE appears to not be available on redhat and the only similar constant that I see being available on redhat is MMADV_REMOVE. But whenever I try to use that constant I get an os error.</p>
  <pre><code># Let the OS know that these pages are no longer in use and can be freed
  &gt;       self.data.madvise(mmap.MADV_REMOVE, data_start, data_size)
  E       OSError: [Errno 22] Invalid argument
  </code></pre>
  <p>Is this the correct way to free up memory from a mmap allocated in Python or am I missing something?</p>
  <p>Redhat Version: Red Hat Enterprise Linux Server release 7.8 (Maipo)</p>
  <p><strong>Kernel Version:</strong></p>
  <pre><code> #uname -r 
   4.14.35-2025.400.9.el7uek.x86_64
  
   #uname -mrs
   Linux 4.14.35-2025.400.9.el7uek.x86_64 x86_64
  </code></pre>
  <p><strong>madvise headers</strong></p>
  <pre><code>/* Advice to `madvise'.  */
  #ifdef __USE_BSD
  # define MADV_NORMAL      0 /* No further special treatment.  */
  # define MADV_RANDOM      1 /* Expect random page references.  */
  # define MADV_SEQUENTIAL  2 /* Expect sequential page references.  */
  # define MADV_WILLNEED    3 /* Will need these pages.  */
  # define MADV_DONTNEED    4 /* Don't need these pages.  */
  # define MADV_REMOVE      9 /* Remove these pages and resources.  */
  # define MADV_DONTFORK    10    /* Do not inherit across fork.  */
  # define MADV_DOFORK      11    /* Do inherit across fork.  */
  # define MADV_MERGEABLE   12    /* KSM may merge identical pages.  */
  # define MADV_UNMERGEABLE 13    /* KSM may not merge identical pages.  */
  # define MADV_HUGEPAGE    14    /* Worth backing with hugepages.  */
  # define MADV_NOHUGEPAGE  15    /* Not worth backing with hugepages.  */
  # define MADV_DONTDUMP    16    /* Explicity exclude from the core dump,
                                     overrides the coredump filter bits.  */
  # define MADV_DODUMP      17    /* Clear the MADV_DONTDUMP flag.  */
  # define MADV_HWPOISON    100   /* Poison a page for testing.  */
  #endif
  </code></pre>
  <p><strong>Glibc version</strong></p>
  <p><code>glibc-headers-2.17-307.0.1.el7.1.x86_64</code></p>
  <p><strong>Solution</strong>
  So as it turns out, on linux any arguments passed to <code>madvise</code> with <code>MADV_REMOVE</code> must be page aligned. That's why it was giving me an error. Aligning the arguments to 4KB pages fixed the issue.</p>