Source of Hidden Allocations?

When I compile this program with rustc -C opt-level=3:

fn main() {}

valgrind shows that this binary allocates and frees nine times:

==443292== HEAP SUMMARY:
==443292==     in use at exit: 0 bytes in 0 blocks
==443292==   total heap usage: 9 allocs, 9 frees, 2,160 bytes allocated
==443292== 
==443292== All heap blocks were freed -- no leaks are possible

Why and where do these allocations occur?

The allocations are very likely for capturing backtraces. Much of the code that executes before main is here: rust/library/std/src/rt.rs at master · rust-lang/rust (github.com)

If you want to avoid it, there are the #![no_std] and #![no_main] attributes, as well as a lot of annoying but important details that you have to consider for creating bare metal executables. Here's a short list of resources that will help along the way if you are interested in this path:

6 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.