Skip to content

Memory fragmentation #132634

Closed as not planned
Closed as not planned
@zqlpaopao

Description

@zqlpaopao

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());
}

Memory at startup
Image

Run output

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

long
Image
Image

Are these caused by dynamic memory allocation? If so, how should we deal with this situation? We can't let the memory grow indefinitely, can we? Or, the OS has its own recycling strategy, and when it reaches a certain size, it will be recycled. Can we set this value ourselves? Otherwise, we can't control the size of the program. Will there be memory leakage problems if the running time is long

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-allocatorsArea: Custom and system allocatorsC-discussionCategory: Discussion or questions that doesn't represent real issues.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions