Skip to content

vm.max_map_count growing steadily when vm.overcommit_memory is 2 #1328

Open

Description

@jsteemann

In an application that uses jemalloc statically linked, I am seeing an ever-increasing value of the process' vm.max_map_count value. The overcommit_memory setting value is 2, so no overcommitting.
It seems that jemalloc reads the overcommit setting at startup, and later takes this setting's value into account when "returning" memory.
When overcommit_memory is set to 2, it seems to call mmap on the returned range, with a protection of PROT_NONE. It seems that this punches holes into existing mappings, so that the kernel will split them and create more of them. This would not be a problem if it happened only seldomly, but we have several use cases in which it happens so often that even increasing the value of vm.max_map_count to tens of millions does not help much.

I have created some (contrived) standalone test program which shows the behavior. I hope it is somewhat deterministic so others can reproduce it:

#include <iostream>
#include <fstream>
#include <deque>
#include <vector>
#include <thread>
#include <algorithm>
#include <cstdlib>
#include <cstring>

void fun() {
  std::string line;
  size_t allocated = 0;
  size_t allocations = 0;
  std::deque<std::pair<void*, size_t>> all;

  size_t n = 1024 * 1024 * 1024;
  ::srand(2848583);
  for (size_t i = 0; i < n; ++i) {
    if (allocations % 500000 == 0) {
      std::ifstream ifs("/proc/self/maps", std::ios_base::in);
      if (!ifs.is_open()) {
        std::cerr << "unable to open mappings file" << std::endl;
        std::abort();
      }
      size_t mappings = 0;
      while (std::getline(ifs, line)) {
        ++mappings;
      }
      std::cout << "- i: " << i << ", allocations: " << allocations << ", mappings: " << mappings << ", allocated: " << allocated << "\n";
    }
    size_t s = ::rand() % (4096 * 128 / 8);
    if (s > 0) {
      auto* p = ::malloc(s);
      if (p == nullptr) {
        std::cerr << "OOM. s: " << s << std::endl;
        std::abort();
      }
      ++allocations;
      allocated += s;
      all.push_back(std::make_pair(p, s));
    }
    while (!all.empty() && allocated > 1024 * 1024 * 128) {
      auto& f = all.front();
      ::free(f.first);
      allocated -= f.second;
      all.pop_front();
    }
  }
}

int main() {
  std::vector<std::thread> threads;

  size_t n = 8; 
  for (size_t i = 0; i < n; ++i) {
    threads.emplace_back(fun);
  }
  
  for (auto& it : threads) {
    it.join();
  }
}

The test program can be compiled and run as follows:

g++ -std=c++11 -O2 -Wall -Wextra test.cc -o test -lpthread
GLIBCXX_FORCE_NEW=1 LD_PRELOAD=~/jemalloc-5.1.0/lib/libjemalloc.so ./test

The program allocates memory of pseudo-random sizes and returns some of the memory. It does so with a few parallel threads. Each thread will not exceed a certain size of allocated memory, so it should not leak.
Each thread is writing out some values to std::cout. The only interesting figure to look at is the "mappings" value reported, e.g.

- i: 7500131, allocations: 7500000, mappings: 18347, allocated: 134196266

That "mappings" value is calculated as the number of lines in /proc/self/maps, which is not 100% accurate but should be a good-enough approximation.

The problem is that when overcommit_memory is set to 2, the number of mappings will grow crazily, both with jemalloc 5.0.1 and jemalloc 5.1.0.

A "fix" for the problem is to apply the following patch:

diff --git a/3rdParty/jemalloc/v5.1.0/src/pages.c b/3rdParty/jemalloc/v5.1.0/src/pages.c
index 26002692d6..3fbad076ad 100644
--- a/3rdParty/jemalloc/v5.1.0/src/pages.c
+++ b/3rdParty/jemalloc/v5.1.0/src/pages.c
@@ -23,7 +23,7 @@ static size_t os_page;
 
 #ifndef _WIN32
 #  define PAGES_PROT_COMMIT (PROT_READ | PROT_WRITE)
-#  define PAGES_PROT_DECOMMIT (PROT_NONE)
+#  define PAGES_PROT_DECOMMIT (PROT_READ | PROT_WRITE)
 static int     mmap_flags;
 #endif
 static bool    os_overcommits;

This makes the test program run with a very low number of memory mappings. It is obviously not a good fix, because it will leave the memory around with read & write access allowed. So please consider it just a demo.

I think it would be good to make jemalloc more usable with an overcommit_memory setting value of 2. Right now, it is kind of risky to use it, because applications may too quickly hit the default vm.max_map_count value of 65K. And even increasing that setting does not help much, because the number of mappings can increase much over time, which means long-running server processes can hit the threshold easily, even if increased.

I guess the current implementation is as it is for a reason, so I guess you will be pretty reluctant to change it. However, it would be good to suggest how to avoid that behavior on systems that don't use overcommit and where vm settings cannot be adjusted. Can an option be added to jemalloc to adjust the behavior on commit in this case, when explicitly configured as such? I think this would help plenty of users, as I have seen several issues in this repository that may have the same root cause. The last one I checked was #1324.
Thanks!

(btw. although I think it does make any difference: the above was tried on Linux kernels 4.15 both on bare metal and an Azure cloud instance, compilers in use were g++-7.3.0 and g++-5.4.0)

Activity

  1. davidtgoldblatt commented on Sep 20, 2018

    @davidtgoldblatt
    Contributor

    Thank you for this great bug report and diagnosis. I don't think the current behavior was really chosen per se; it's just an emergent property of a combination of things we don't test very well. I think this needs some amount of philosophizing.

    (I think it might have to wait for @interwq before anyone can take thorough look; we're stretched a little thin for now).

    Ironically, one thing that might help is disabling purging settings (i.e. MALLOC_CONF="dirty_decay_ms:-1,muzzy_decay_ms:-1"); this would increase RSS, but not having fiddling around with mappings as much might make it net-better.

  2. jsteemann commented on Sep 20, 2018

    @jsteemann
    ContributorAuthor

    @davidtgoldblatt : thanks for looking into it and the suggestion for disabling purging.
    I tried running the test program with purging disabled as you suggested (MALLOC_CONF="dirty_decay_ms:-1,muzzy_decay_ms:-1").

    With jemalloc 5.1.0 this indeed makes the number of mappings grows much much more slowly, but the overall number still increases over time. It is the hundreds rather than in the tens of thousands now.
    Worse however is that this setting makes the overall virtual memory size used by the process (as reported by top) quickly grow to more than 20 GB. On my local laptop it reaches 20 GB vsz after less than 2 minutes and then dies with OOM. With the default malloc settings the process is at around 2,5 GB vsz usage after the same time and can still allocate then.
    In terms of rss the adjusted MALLOC_CONF value is also a bit worse, at least for this particular test.

  3. egaudry commented on Sep 26, 2018

    @egaudry

    Just a side note regarding kernel if needed, this behavior could also be observed with 3.10.x and 4.4.x.

  4. jsteemann commented on Sep 26, 2018

    @jsteemann
    ContributorAuthor

    For reference, here is a "fix" for 5.1.0:

    diff --git a/3rdParty/jemalloc/v5.1.0/src/pages.c b/3rdParty/jemalloc/v5.1.0/src/pages.c
    index 26002692d6..3fbad076ad 100644
    --- a/3rdParty/jemalloc/v5.1.0/src/pages.c
    +++ b/3rdParty/jemalloc/v5.1.0/src/pages.c
    @@ -23,7 +23,7 @@ static size_t os_page;
     
     #ifndef _WIN32
     #  define PAGES_PROT_COMMIT (PROT_READ | PROT_WRITE)
    -#  define PAGES_PROT_DECOMMIT (PROT_NONE)
    +#  define PAGES_PROT_DECOMMIT (PROT_READ | PROT_WRITE)
     static int     mmap_flags;
     #endif
     static bool    os_overcommits;

    And for 5.0.1:

    diff --git a/3rdParty/jemalloc/v5.0.1/src/pages.c b/3rdParty/jemalloc/v5.0.1/src/pages.c
    index fec64dd01d..733652adf3 100644
    --- a/3rdParty/jemalloc/v5.0.1/src/pages.c
    +++ b/3rdParty/jemalloc/v5.0.1/src/pages.c
    @@ -20,7 +20,7 @@ static size_t os_page;
     
     #ifndef _WIN32
     #  define PAGES_PROT_COMMIT (PROT_READ | PROT_WRITE)
    -#  define PAGES_PROT_DECOMMIT (PROT_NONE)
    +#  define PAGES_PROT_DECOMMIT (PROT_READ | PROT_WRITE)
     static int     mmap_flags;
     #endif
     static bool    os_overcommits;

    These "fixes" prevent the endless growth of mappings.
    However, I don't recommend using these fixes, because they will allow the application to still access memory it has returned to the allocator via free()/delete. The standard jemalloc behavior will lead to the kernel raising a SIGSEGV when accessing a memory page that the application or the allocator has given back after decommitting the memory. The patches above will however allow the application to still access memory it has given back, so they may hide errors in an application's memory handling and may also cause security issues, because memory given back by the application is still readable (only by the application itself - however, this is normally undesired).

  5. egaudry commented on Oct 5, 2018

    @egaudry

    @jasone : do you think that a simple fix can be made to avoid this issue (this is likely related to c2f970c) ?

  6. egaudry commented on Jan 11, 2019

    @egaudry

    @interwq Hello, is there any chance to get this fixed for milestone 5.2.0 ?

  7. interwq commented on Jan 11, 2019

    @interwq
    Contributor

    @egaudry : there doesn't seem to be a straightforward fix I can think of right now. Like David mentioned the current behavior under no overcommit isn't particularly optimized, as the environment we work with usually has overcommit enabled.

    I wasn't able to change the overcommit setting on my dev box somehow. Can you guys help to try one more thing, running with malloc_conf retain:false? The retain option was specifically designed to reduce the number of mappings. I wonder if it doesn't interact properly with no overcommit.

  8. egaudry commented on Jan 14, 2019

    @egaudry

    Do you mean that
    $ /sbin/sysctl vm.overcommit_memory=2
    would not work on your box ?
    You can try to lower vm.max_map_count too, it would allow to reproduce a similar issue too.

    I'm not sure about the retain option : as @jsteemann observed, any option that would reduce the number of mapping would not allow to avoid the issue in the long term (i.e. with numerous allocation and/or long living process).

  9. interwq commented on Jan 18, 2019

    @interwq
    Contributor

    I meant turning off retain is worth trying, since it could affect number of mappings even in the long term. Plus I believe the option was more designed with overcommit in mind; it may affect # of mappings negatively with no overcommit.

  10. egaudry commented on Jan 19, 2019

    @egaudry

    Thanks, I will have a look (I though this was a compile time parameter).

  11. egaudry commented on Jan 21, 2019

    @egaudry

    Do I need to rebuild jemalloc ?
    When I set MALLOC_CONF=retain:false before launching my test, jemalloc complains:

    <jemalloc>: Invalid conf pair: retain:false

    I should have stated that I was using (and need to stick to this) version 4.5.0 here.

  12. jsteemann commented on Jan 21, 2019

    @jsteemann
    ContributorAuthor

    I tried MALLOC_CONF="retain:true" original_program original_args and it works for me.
    Jemalloc prints error messages whenever I used some non-existing conf option or provide non-allowed values for config options.
    As far as I can see the options are always processed and there is no way to disable options parsing at compile-time. "retain" should be present at least in v5.0.1 and v5.1.0.

  13. egaudry commented on Jan 21, 2019

    @egaudry

    Jan, you are right : I relaunched my test with the current master branch and I was able to use retain:false at runtime.

    Unfortunately, it didn't solve the large map_count issue (or not enough) I observed with one of our test-cases.

  14. jsteemann commented on Jan 21, 2019

    @jsteemann
    ContributorAuthor

    @egaudry : that is also what I had observed before.
    The only "working solution" we found is to move away completely from vm.overcommit_memory=2, but that may not be an option for everyone and also makes the allocator less general-purpose than desired.

  15. egaudry commented on Jan 21, 2019

    @egaudry

    My main concern is that our users are reluctant to such a change or are not in a position to request such a change (ex: cluster/centralized computing resources with different software running) and I cannot reasonably expect them to switch to a more permissive mode.

  16. 72 remaining items

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions