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_MALLOC_THRESHOLD to prevent memory fragmentation #796

Merged
merged 1 commit into from
Feb 4, 2025

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 causes cosmic-applet-minimize's memory usage to reach 200+ MB when restoring a few minimized windows. Memory usage of cosmic-applet-minimize is now in line with all other applets (25 MB), no matter how many windows are minimized and restored.

Fixes #785

@mmstick mmstick requested a review from wash2 February 4, 2025 15:44
@l-const
Copy link
Contributor

l-const commented Feb 4, 2025

Could we test this fix on cosmic-files and cosmic-term? I had raised this one : pop-os/cosmic-files#641 that seems related.

@mmstick
Copy link
Member Author

mmstick commented Feb 4, 2025

@l-const You could launch cosmic files like so to try it out

env MALLOC_MMAP_THRESHOLD_=65536 cosmic-files

You can also try out different values.

@mmstick mmstick merged commit 8b4e257 into master Feb 4, 2025
9 of 10 checks passed
@mmstick mmstick deleted the mmap-threshold branch February 4, 2025 19:41
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.

[Bug] cosmic-applet-minimize is using a GB of RAM
3 participants