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

Memory Leak Fix: Implementing jemalloc Allocator #205

Merged
merged 2 commits into from
Jan 24, 2025
Merged

Conversation

heemankv
Copy link
Contributor

@heemankv heemankv commented Jan 24, 2025

Memory Leak Fix: Implementing jemalloc Allocator

Issue

Memory leak identified during multiple SNOS job runs, causing cumulative memory growth over time. Memory usage increases with each job completion, even after tasks finish.

Root Cause Analysis

  • Multiple tokio spawned tasks lead to memory fragmentation
  • System allocator (malloc) requests new memory segments instead of reusing fragmented spaces
  • Memory not being efficiently released back to OS

Solution

( See attached screenshots for reference )
Implemented jemalloc as global allocator with aggressive memory release configuration:

#[global_allocator]
static A: jemallocator::Jemalloc = jemallocator::Jemalloc;

Environment Configuration

JEMALLOC_SYS_WITH_MALLOC_CONF="background_thread:true,narenas:1,tcache:false,dirty_decay_ms:0,muzzy_decay_ms:0,abort_conf:true"

Configuration breakdown:

  • background_thread:true: Enables background memory management
  • narenas:1: Single arena to reduce fragmentation
  • tcache:false: Disables thread caching for immediate memory release
  • dirty_decay_ms:0: Immediate release of unused dirty pages
  • muzzy_decay_ms:0: Immediate release of muzzy pages
  • abort_conf:true: Fails fast on configuration errors

Testing

  • Memory usage monitored across multiple job runs
  • No cumulative memory growth observed with jemalloc implementation
  • System stability maintained with aggressive memory release settings

Dependencies Added

[dependencies]
jemallocator = "0.5.4"

Empirical Analysis

Current allocation, leading to memory leaks.

Screenshot 2025-01-24 at 9 06 02 AM

After implementing Jemallocator

Screenshot 2025-01-24 at 9 07 00 AM

Note

While the exact mechanism of the fix requires further investigation, the implementation has effectively resolved the memory leak issue in production environment.
This PR description is enhanced using AI.

@heemankv heemankv self-assigned this Jan 24, 2025
@heemankv heemankv changed the title Fix: Memory Leak while running SNOS Fix: Memory leak while running SNOS Jan 24, 2025
@coveralls
Copy link

coveralls commented Jan 24, 2025

Coverage Status

coverage: 67.086%. remained the same
when pulling 4629784 on fix/memory-leak
into 9000464 on main.

@heemankv heemankv changed the title Fix: Memory leak while running SNOS Memory Leak Fix: Implementing jemalloc Allocator Jan 24, 2025
Copy link
Contributor Author

@heemankv heemankv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Self review done, lgtm!

@heemankv heemankv requested review from Mohiiit, ocdbytes and apoorvsadana and removed request for ocdbytes January 24, 2025 04:27
@heemankv heemankv merged commit afb9afe into main Jan 24, 2025
9 checks passed
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.

4 participants