Skip to content

[flow] Use jemalloc in Cargo builds on Linux - #9520

Closed
panagosg7 wants to merge 1 commit into
mainfrom
export-D128303865
Closed

panagosg7 wants to merge 1 commit into
mainfrom
export-D128303865

Conversation

@panagosg7

@panagosg7 panagosg7 commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

Summary:
Release binaries are built with Cargo, which uses the system allocator: glibc malloc on Linux. glibc keeps most of the memory a recheck frees instead of returning it to the OS, so a long-running server's resident memory ratchets up with every large recheck. On a large codebase it settled at about twice what the same code uses with jemalloc, and malloc_trim(0) gave the difference back, so the growth is retained free memory, not live data.

Install jemalloc as the global allocator in Cargo builds on Linux, with background threads so that memory freed by a recheck goes back to the OS while the server is idle. Buck builds pick their allocator themselves and are unchanged. jemalloc fixes its page size when it is built, so the arm64 release build uses 64 KiB pages, which also work on kernels with 4 or 16 KiB pages.

macOS keeps the system allocator, which already returns memory after a recheck. With jemalloc (which has no background threads on macOS) the server settled about 1 GiB higher and peaked about 4 GiB lower, which is not a clear win.

Changelog: [fix] Linux release builds use jemalloc, which keeps the memory of long-running servers from growing after large rechecks.

Differential Revision: D128303865

@meta-cla meta-cla Bot added the CLA Signed label Oct 29, 2026
@meta-codesync

meta-codesync Bot commented Oct 29, 2026

Copy link
Copy Markdown

@panagosg7 has exported this pull request. If you are a Meta employee, you can view the originating Diff in D128303865.

Summary:
Release binaries are built with Cargo, which uses the system allocator: glibc malloc on Linux. glibc keeps most of the memory a recheck frees instead of returning it to the OS, so a long-running server's resident memory ratchets up with every large recheck. On a large codebase it settled at about twice what the same code uses with jemalloc, and `malloc_trim(0)` gave the difference back, so the growth is retained free memory, not live data.

Install jemalloc as the global allocator in Cargo builds on Linux, with background threads so that memory freed by a recheck goes back to the OS while the server is idle. Buck builds pick their allocator themselves and are unchanged. jemalloc fixes its page size when it is built, so the arm64 release build uses 64 KiB pages, which also work on kernels with 4 or 16 KiB pages.

macOS keeps the system allocator, which already returns memory after a recheck. With jemalloc (which has no background threads on macOS) the server settled about 1 GiB higher and peaked about 4 GiB lower, which is not a clear win.

Changelog: [fix] Linux release builds use jemalloc, which keeps the memory of long-running servers from growing after large rechecks.

Differential Revision: D128303865
@meta-codesync

meta-codesync Bot commented Oct 30, 2026

Copy link
Copy Markdown

This pull request has been merged in 4414276.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant