Reputation: 125
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?
Upvotes: 2
Views: 104
Reputation: 3424
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 {
System.alloc(layout)
}
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
System.dealloc(ptr, layout)
}
}
#[global_allocator]
static GLOBAL: MyAllocator = MyAllocator;
You could use this to change the behavior of the deallocation to your needs.
Or possibly use an existing crate that implements allocators that logs the allocations/deallocations such as tracing-allocator or logging-allocator.
#[global_allocator]
static GLOBAL: tracing_allocator::Allocator = tracing_allocator::Allocator{};
fn main() {
let f = File::create("trace.txt").unwrap();
tracing_allocator::Allocator::initialize(&f);
tracing_allocator::Allocator::activate();
}
(I have no experience with these crates so I have no idea what they uses for allocations)
Upvotes: 3