Skip to content

vec or hashmap seem to still hold heap memory after lifetime #73307

@Right2Left

Description

@Right2Left

I tried this code:

use std::thread;
use std::time::Duration;
use std::collections::HashMap;

 
fn main() {
    //let value = read_file("./1k.conf");
    let value = "test content".to_string();

    println!("begin");
    thread::sleep(Duration::from_secs(20));

    test_vec_not_clear(&value);
    wait_for_free(10);

    test_map_not_clear(&value);
    wait_for_free(10);

    wait_for_free(40);

    test_map_not_clear(&value);
    wait_for_free(10);

    println!("end");
    loop{
        thread::sleep(Duration::from_secs(20));
    }

}

fn wait_for_free(count: i32){
    for i in 0..count  {
        println!("wait count: {}", i);
        thread::sleep(Duration::from_secs(1));
    }
}

fn test_vec_not_clear(value: &str){
    println!("to test vec not clear");
    thread::sleep(Duration::from_secs(5));
    let mut vec = Vec::new();
    for _i in 0..1000000{
        vec.push(value.to_string());
    }
}

fn test_map_not_clear(value: &str){
    println!("to test map not clear");
    thread::sleep(Duration::from_secs(5));
    let mut map = HashMap::new();
    for i in 0..1000000{
        map.insert(i.to_string(), value.to_string());
    }
}

then use valgrind to start:
valgrind --tool=massif --detailed-freq=1 --massif-out-file=massif.out.test.%p ./test_mem

I expected to see this happen:
memory will down to slow usage after line:24
28M -> 300M -> 28M -> 300M -> 28M -> 300M -> 28M

Instead, this happened:
the memory usage is :
28M -> 300M -> 85M -> 300M -> 137M -> 300M -> 137M
the massif report:

Meta

rustc --version --verbose:

test@test-Standard-PC-i440FX-PIIX-1996:~/hzx/test_mem/target/debug$ rustc --version --verbose
rustc 1.41.1 (f3e1a954d 2020-02-24)
binary: rustc
commit-hash: f3e1a954d2ead4e2fc197c7da7d71e6c61bad196
commit-date: 2020-02-24
host: x86_64-unknown-linux-gnu
release: 1.41.1
LLVM version: 9.0
test@test-Standard-PC-i440FX-PIIX-1996:~/hzx/test_mem/target/debug$ 
rustc 1.46.0-nightly (feb3536eb 2020-06-09)
binary: rustc
commit-hash: feb3536eba10c2e4585d066629598f03d5ddc7c6
commit-date: 2020-06-09
host: x86_64-unknown-linux-gnu
release: 1.46.0-nightly
LLVM version: 10.0

in lastest version is same.

Backtrace

<backtrace>

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions