2

I've the following code

use jemalloc_ctl::{stats, epoch};
use jemallocator;
#[global_allocator]
static ALLOC: jemallocator::Jemalloc = jemallocator::Jemalloc;

#[actix_rt::main]
async fn main() -> std::io::Result<()> {
    let my_structs = vec![3];

    let allocated_bytes: usize = current();
    {
        for _i in 0..4782 {
            let _a: Vec<&i32> = my_structs
                .iter()
                .collect();
        }
    }

    let allocated_bytes_after: usize = current();
    println!("before allocated_bytes: {}", allocated_bytes);
    println!("after   allocated_bytes: {}", allocated_bytes_after);
    println!("DIFF allocated_bytes: {}", allocated_bytes_after - allocated_bytes);

    Ok(())
}

fn current() -> usize {
    epoch::advance().unwrap();
    stats::allocated::read().unwrap()
}

The output confirms that the diff is 0 bytes as expected

Anyway if I change the iteration upper limit from 4782 to 4783 the result changes: the program exits with panic because the allocated_bytes_after is less than allocated_bytes.

This is a strange behavior. Why the memory allocation is less after? Why the limit is 4782?

I'm using:

$ cargo version
cargo 1.43.0 (2cbe9048e 2020-05-03)
4

0 回答 0