• Uncategorized

About linux : How-to-set-a-free-strategy

Question Detail

I’ve encountered high memory usage (looks like a memory leak) in production environment (container in k8s), and want to check if it’s because of the “MADV_FREE” behaviour.

Is there a way to change to use MADV_DONTNEED instead of MADV_FREE in rust?

Question Answer

Rust allows overriding the default allocator with the #[global_allocator] attribute

struct MyAllocator;

unsafe impl GlobalAlloc for MyAllocator {
    unsafe fn alloc(&self, layout: Layout) -> *mut u8 {

    unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
        System.dealloc(ptr, layout)

static GLOBAL: MyAllocator = MyAllocator;

You could use this to change the behavior of the deallocation to your needs.

Or possably use an existing crate that impliment allocators tha logs the allocations/deallocations such as tracing-allocator or logging-allocator.

static GLOBAL: tracing_allocator::Allocator = tracing_allocator::Allocator{};

fn main() {
  let f = File::create("trace.txt").unwrap();

(I have no experience with these crates so I have no idea what they uses for allocations)

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.