Why does Valgrind report `LazyLock>` as `possibly lost`, but `LazyLock>` as `still reachable`?

⚓ Rust    📅 2026-01-02    👤 surdeus    👁️ 1      

surdeus

Greetings,

when analyzed with Valgrind (valgrind --leak-check=full --show-leak-kinds=definite,indirect,possible on the release executable), the code

use std::collections::HashMap;
use std::sync::LazyLock;

static MY_LAZY: LazyLock<HashMap<u32, u32>> = LazyLock::new(|| {
	let mut data = HashMap::new();

	for i in 0..1024 {
		data.insert(i, i * 2);
	}

	data
});

fn main() {
	println!("{}", MY_LAZY.get(&15).unwrap());
}

outputs possibly lost: 18,448 bytes in 1 blocks, on the other hand, when replacing HashMap with BTreeMap, Valgrind reports: still reachable: 20,640 bytes in 172 blocks.


The LazyLock doc already states that Valgrind might report memory leaks since the static data is not freed manually upon shutdown; I'm interested by HashMap is reported differently from BTreeMap -- any ideas?

Thanks a lot in advance!

1 post - 1 participant

Read full topic

🏷️ Rust_feed