1

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:

Memory leak demonstraited picture

Chayim Friedman
  • 47,971
  • 5
  • 48
  • 77
Ug-Projekt
  • 19
  • 2
  • Maybe I found the problem, The main problem is that Vec, HashMap are using GlobalAllocator by default if My understanding is correct, BUT How can I make free that HashMap at the end of it's scope? – Ug-Projekt Jul 23 '22 at 14:23
  • 3
    [clear](https://doc.rust-lang.org/stable/std/collections/struct.HashMap.html#method.clear) _Keeps the allocated memory for reuse._ – Ömer Erden Jul 23 '22 at 14:39
  • 2
    Note that most allocators don't return all the memory to the OS when it is freed, but instead keep it for later re-use. This also true in other languages (e.g. C `malloc`/`free` or C++ `new`/`delete`). The real question is: if you call `make_data` a second time, does the memory use double? – Jmb Jul 23 '22 at 15:09
  • 1
    See this [playground](https://play.rust-lang.org/?version=stable&mode=release&edition=2021&gist=93f1f629984674d846eaa0681247f03f): memory usage is much higher after the first call to `make_data`, but is almost the same after the second call. – Jmb Jul 23 '22 at 15:20
  • 1
    Yes, it is seems to be linux kernel or low level memory allocation library related issue, I can't repreduce this problem when testing this application on several of windows and mac machine, also I call the 'make_data' several times but memory consumption is the same as single 'make_data' call, Thanks to all. – Ug-Projekt Jul 23 '22 at 15:44
  • I testing this many times on different platform and this problem is only can be produced on linux platform (includes WSL) – Ug-Projekt Jul 23 '22 at 16:30
  • It's worth mentioning, Rust considers memory leaks "safe". `std::mem::forget` used to be unsafe, but it was changed once people realised you could implement it in safe code using `Rc` – cameron1024 Jul 23 '22 at 22:32
  • @cameron1024 "Safe" and "acceptable for simple data types when they go out of scope" are two different things. Rust doesn't just randomly leak memory whenever it feels like it. That would be horrible. – Finomnis Jul 24 '22 at 08:16
  • @ÖmerErden Yes, but that is only correct until the `HashMap` gets dropped. It doesn't just randomly leak all of its memory forever. – Finomnis Jul 24 '22 at 08:21
  • @Finomnis Post has more than one problem: _I think HashMap is cause memory leak even clear all contents of HashMap_ , I wrote the comment as a reminder to let people not focus on `clear` – Ömer Erden Jul 24 '22 at 09:41
  • @ÖmerErden Yes, but the way you wrote it sounded like that's the reason why there is still memory allocations left when it is stuck in `read_line()`, which is not the case. Everything from `HashMap` was dropped by then. – Finomnis Jul 24 '22 at 09:50
  • @Ug-Projekt Also related: https://www.reddit.com/r/rust/comments/szza43/memory_freed_but_not_immediately/ – Finomnis Jul 24 '22 at 10:14

0 Answers0