Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: set M_MMAP_THRESHOLD to prevent memory fragmentation #73

Merged
merged 2 commits into from
Feb 5, 2025
Merged

Conversation

mmstick
Copy link
Member

@mmstick mmstick commented Feb 4, 2025

I read some articles about how a default behavior of glibc malloc causes some applications to consume large amounts of RAM until reaching OOM. There's a tunable parameter called M_MMAP_THRESHOLD (or the MALLOC_MMAP_THRESHOLD_ environment variable) which defines when an allocation will use mmap instead of sbrk.

Small sbrk buffers are stored by malloc in arenas for reuse by the application. mmap buffers are released by the OS instantly on drop. The problem is that the default behavior of malloc is to dynamically increase the threshold if this tunable is not set, which leads to some applications storing lots of randomly-sized gigantic buffers in its arenas, which may never be freed.

This lowered memory usage of cosmic-bg on my system from 96 MB to 25 MB.

@mmstick mmstick requested a review from a team February 4, 2025 15:54
src/main.rs Outdated Show resolved Hide resolved
@mmstick mmstick changed the title fix: set M_MALLOC_THRESHOLD to prevent memory fragmentation fix: set M_MMAP_THRESHOLD to prevent memory fragmentation Feb 5, 2025
@mmstick mmstick merged commit b6adf25 into master Feb 5, 2025
7 checks passed
@mmstick mmstick deleted the malloc branch February 5, 2025 18:11
@kskarthik
Copy link

This lowered memory usage of cosmic-bg on my system from 96 MB to 25 MB.

wow! that's a drastic amount

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants