When I am new to Rust and I am using HashMap to store some data but I think HashMap is cause memory leak even clear all contents of HashMap,
Here is the very simple demonstration code:
use std::{io::stdin, collections::HashMap};
fn main() {
make_data(); //***********AFTER THIS FUNCTION, NO WHERE TO ACCESS THE HASHMAP THAT DECLARED IN THE FUNCTION******
let mut line = String::new();
println!("Please press enter key to exit.");
stdin().read_line(&mut line).expect("Cannot read console");
}
fn make_data() {
let mut data = HashMap::new(); //HashMap is declared in there.
for i in 0..10_000_000 {
let (key, value) = (format!("Key{}", i), String::from("Long long long text Long long long text Long long long text Long long long text Long long long text Long long long text"));
data.insert(key, value); //And fill it with some of data, now HashMap is become of 'key' and 'value' variable so the HashMap is responsible to manage these variable memory I think.
}
data.clear(); //clear also Won't work, 1.5GB of memory is not freed, Here I'm guessing the memory leak is occurred the key or value content, But I think HashMap should be manage it's key and value memory because the HashMap is owner of it's key and value.
// Why HashMap is not freed when it is goes out of it's scope?
// and no where is impossible to access the HashMap object right?
}
The final result is the same when I am trying to using Vec instead of HashMap
rustc --version
is rustc 1.62.1 (e092d0b6b 2022-07-16)
there are the some pictures: