Tracking down memory issues within program

Hello. I am writing a terminal in rust and gtk (Files · dev · Michael Ditto / mt · GitLab) and I'm currently trying to track down a memory issue that I'm having. The program usually opens at around 26mb on my system, but it can spike to around 42 mb even when I'm not doing anything and it won't go down. I thought maybe I had a memory leak, so I ran it with valgrind memcheck and massif. Memcheck does show a leak, but its relatively small (a few kb) and couldn't be the source of the issue that I'm seeing. Massif showed that my heap memory peaking around 30mb, but going down once tabs were closed. Neither of these approaches have helped me narrow down what could possibly be causing this sudden increase in memory. Does anyone know of good tools that track the presence of rust structures in memory that might make this problem easier to solve?

It may simply be that the memory allocator doesn't give up the memory to the OS. The memory allocator often keeps a bit of memory allocated to quickly satisfy new malloc requests. In addition it isn't possible to unmap partial pages, so if a page is partially used, it has to be completely mapped. Many memory allocators also subdivide the memory into bigger chunks that can only be unmapped as a whole. Memory fragmentation can cause the amount of memory as measured by the OS to be like twice as much as the actually allocated amount of memory.

is a great article about this.


To verify this, the cap crate is very useful. You can ask it for byte-accurate amount of Rust memory currently allocated (from Rust's perspective, excluding allocator and OS overhead).


@kornel , @bjorn3 thank you so much for your suggestions :slight_smile: Looks like I've got some more reading to do.