Unreleased memory

my code


 fn main()  {
    std::thread::sleep(std::time::Duration::from_secs(10));
    loop {
        test();
        std::thread::sleep(std::time::Duration::from_secs(3));
    }
}


struct MyVec {
    vec: Vec<i32>,
}

impl Drop for MyVec {
    fn drop(&mut self) {
        println!("MyVec is being dropped, memory is being released.");
    }
}

fn test() {
    let mut my_vec = MyVec { vec: Vec::with_capacity(101) };

    for i in 0..101 {
        my_vec.vec.push(i);
    }
    println!("{:?}", my_vec.vec.len());
}

print

101
MyVec is being dropped, memory is being released.
101
MyVec is being dropped, memory is being released.
101
MyVec is being dropped, memory is being released.
101
MyVec is being dropped, memory is being released.
101
MyVec is being dropped, memory is being released.
101
MyVec is being dropped, memory is being released.
101
MyVec is being dropped, memory is being released.
101
MyVec is being dropped, memory is being released.
101
MyVec is being dropped, memory is being released.
101
MyVec is being dropped, memory is being released.
101
MyVec is being dropped, memory is being released.
101
MyVec is being dropped, memory is being released.
101
MyVec is being dropped, memory is being released.
101
MyVec is being dropped, memory is being released.
101
MyVec is being dropped, memory is being released.
101
MyVec is being dropped, memory is being released.
101
MyVec is being dropped, memory is being released.
101
MyVec is being dropped, memory is being released.
101
MyVec is being dropped, memory is being released.
101
MyVec is being dropped, memory is being released.
101
MyVec is being dropped, memory is being released.
101
MyVec is being dropped, memory is being released.
101
MyVec is being dropped, memory is being released.
101
MyVec is being dropped, memory is being released.
101
MyVec is being dropped, memory is being released.
101
MyVec is being dropped, memory is being released.
101
MyVec is being dropped, memory is being released.
101
MyVec is being dropped, memory is being released.
101
MyVec is being dropped, memory is being released.
101
MyVec is being dropped, memory is being released.
101
MyVec is being dropped, memory is being released.


But the memory I detected has been continuously increasing

The data is released after the function ends, but why is there growth inside

Memory allocators don't interact with the operating system for every single allocation or deallocation; that would be inefficient. This means that there is going to be some memory that the OS understands as occupied, but for the program is free. How much memory this is depends on the allocator’s algorithm, which may take into account the previous usage.

You should let the program run for many more iterations (make your sleeps much shorter to make this convenient) and you will probably observe that its reported memory usage will increase to some value, and then stay there forever.

If you want to minimize the memory your program occupies at all times, you can choose to replace the global allocator with one which is more inclined to return memory to the OS. This is rarely worth doing.

5 Likes