init: add MALLOC_ARENA_MAX=1 to systemd #27642

pull sangaman wants to merge 1 commits into bitcoin:master from sangaman:malloc_arena_max_systemd changing 1 files +3 −0
  1. sangaman commented at 3:15 PM on May 12, 2023: contributor

    This adds the MALLOC_ARENA_MAX=1 environment variable as suggested in https://github.com/bitcoin/bitcoin/blob/master/doc/reduce-memory.md#linux-specific to the systemd service file definition.

    Without this env var, memory usage can grow significantly especially when rpcthreads is increased above its default value.

    Closes #24542.

    I have tested this change myself with positive results after dealing with memory consumption issues for a long time using systemd on a 8GB RAM raspi4. I figure a similar change may be desirable for the OpenRC and CentOS init files, but I don't have a way to test them and I'm not even exactly sure how environment variables should be added there (via export statements?).

  2. init: add MALLOC_ARENA_MAX=1 to systemd
    This adds the `MALLOC_ARENA_MAX=1` environment variable as suggested in
    https://github.com/bitcoin/bitcoin/blob/master/doc/reduce-memory.md#linux-specific
    to the systemd service file definition.
    
    Without this env var, memory usage can grow significantly especially when
    `rpcthreads` is increased above its default value.
    
    Closes #24542.
    0cd0eec466
  3. DrahtBot commented at 3:15 PM on May 12, 2023: contributor

    <!--e57a25ab6845829454e8d69fc972939a-->

    The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

    <!--021abf342d371248e50ceaed478a90ca-->

    Reviews

    See the guideline for information on the review process. A summary of reviews will appear here.

  4. willcl-ark commented at 3:19 PM on May 12, 2023: member

    I don't believe this affects the majority of users (where the two pools per core default works fine), so I don't see why we'd update the default service file like this.

    We have documentation that describes what to do to reduce memory in the event that one wants to do that?

  5. sangaman commented at 3:35 PM on May 12, 2023: contributor

    I don't believe this affects the majority of users (where the two pools per core default works fine), so I don't see why we'd update the default service file like this.

    We have documentation that describes what to do to reduce memory in the event that one wants to do that?

    Maybe it's my fault for not digging into the docs more but I went over a year with inflated, unpredictable RAM usage and even occasional OOM crashes on a machine that I wouldn't consider particularly memory-constrained. It looks like the reason I was seeing it was due to a higher rpcthreads setting, but I'm sure I'm not the only one that has that sort of config and they may be dealing with the same issues.

    My thinking is that if in fact the performance impact is "small or absent" in bitcoind then this is a sensible default for linux machines.

  6. Sjors commented at 6:04 PM on May 12, 2023: member

    From the doc:

    However, in Bitcoin Core very little parallel allocation happens, so the impact is expected to be small or absent.

    The "expected to be" could use some benchmarks before making it a default. Does the problem only happen with high rpcthreads or equally with other (potentially more common) settings like a high -dbcache or -maxmempool?

  7. sangaman commented at 6:28 PM on May 12, 2023: contributor

    Does the problem only happen with high rpcthreads or equally with other (potentially more common) settings like a high -dbcache or -maxmempool?

    From my understanding/reading of MALLOC_ARENA_MAX it affects memory usage where multiple threads are concerned. rpcthreads is the only configuration option afaict that impacts the number of threads running. Increasing dbcache or maxmempool wouldn't create new threads, only increase a set limit of RAM usage, so I don't think those alone would trigger the behavior I was seeing.

  8. sangaman commented at 6:34 PM on May 12, 2023: contributor

    More relevant discussion here: #6876 (comment)

  9. Sjors commented at 6:48 PM on May 12, 2023: member

    @sangaman maybe -par as well? It defaults to the number of cores - 1 and is capped at 15.

  10. martinus commented at 1:57 PM on May 13, 2023: contributor

    Now that #25325 has been merged, this might be less of an issue. This pool allocator significantly reduces the number of allocations used for CCoinsMap. (by about a factor of 2000).

  11. sangaman commented at 2:35 PM on May 13, 2023: contributor

    Now that #25325 has been merged, this might be less of an issue. This pool allocator significantly reduces the number of allocations used for CCoinsMap. (by about a factor of 2000).

    Thanks! I'll try that branch out and see if resolves the underlying issue without this env var, in which case I'd agree this PR is probably not necessary.

  12. fanquake commented at 11:01 AM on May 15, 2023: member

    Note that internally we already set M_ARENA_MAX to 1, but that is currently restricted to 32-bit systems. I would assume everyone reporting issues (#24542) is using on 64-bit. We may be at the point where we could just drop that code entirely.

  13. sangaman commented at 2:34 PM on May 16, 2023: contributor

    I'm 64 bit indeed, but wouldn't the same concern be present on 32 bit systems?

    I just restarted my node with the changes from #25325 and without the MALLOC_ARENA_MAX setting, I'll give it a few days and report back on how the memory usage is going.

  14. DrahtBot commented at 9:25 AM on August 8, 2023: contributor

    <!--8ac04cdde196e94527acabf64b896448-->

    There hasn't been much activity lately. What is the status here?

    Finding reviewers may take time. However, if the patch is no longer relevant, please close this pull request. If the author lost interest or time to work on this, please close it and mark it 'Up for grabs' with the label, so that it can be picked up in the future.

  15. sipa commented at 4:46 PM on September 20, 2023: member

    Just because it's desirable for the purpose of running on low-memory systems does not mean it should be a default for all systemd users of bitcoind? If it's useful always, it should probably be recommended outside a doc about reducing memory usage. Alternatively, it sounds like maybe we need to document that increasing rpcthreads may increase memory usage.

  16. real-or-random commented at 4:48 PM on September 20, 2023: contributor

    I just restarted my node with the changes from #25325 and without the MALLOC_ARENA_MAX setting, I'll give it a few days and report back on how the memory usage is going.

    Any insights here? Has the issue been resolved for you?

    Note that https://www.gnu.org/software/libc/manual/html_node/Memory-Allocation-Tunables.html says:

    The default value of this tunable is 0, meaning that the limit on the number of arenas is determined by the number of CPU cores online. For 32-bit systems the limit is twice the number of cores online and on 64-bit systems, it is 8 times the number of cores online.

    This means what we say in https://github.com/bitcoin/bitcoin/blob/master/doc/reduce-memory.md#linux-specific is wrong (i.e., only true for 32 bits):

    By default, glibc will create up to two heap arenas per core.

  17. real-or-random referenced this in commit 12f7257b8f on Sep 20, 2023
  18. achow101 commented at 5:39 PM on September 20, 2023: member

    This PR does not seem to have conceptual support. Please leave a comment if you would like this to be reopened.

  19. achow101 closed this on Sep 20, 2023

  20. fanquake referenced this in commit dcfbf3c210 on Sep 24, 2023
  21. Retropex referenced this in commit 1ee47e838f on Oct 4, 2023
  22. janus referenced this in commit 3ae974e71f on Apr 1, 2024
  23. bitcoin locked this on Sep 19, 2024

github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2026-04-22 18:13 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me