This approach doesn't follow references.
struct Container<T> {
items: [T; 3],
}
fn main() {
use std::mem::size_of_val;
let cu8 = Container {
items: [1u8, 2u8, 3u8],
};
println!("size of cu8 = {} bytes", size_of_val(&cu8)); // returns 3
let cu32 = Container {
items: [1u32, 2u32, 3u32],
};
println!("size of cu32 = {} bytes", size_of_val(&cu32)); // returns 12
}
This will compile and run on Linux.
cargo add jemallocator
cargo add jemalloc-ctl
In your main.rs file add the following global allocator declaration.
#[cfg(not(target_env = "msvc"))]
#[global_allocator]
static ALLOC: jemallocator::Jemalloc = jemallocator::Jemalloc;
In the rust file where you want to measure the memory consumption.
use jemalloc_ctl::{stats, epoch};
// ...
let epoch = epoch::mib().unwrap();
let allocated_mib = stats::allocated::mib().unwrap();
let allocated_before = allocated_mib.read().unwrap();
let _buf = vec![0i64; 16];
epoch.advance().unwrap();
let allocated_after = allocated_mib.read().unwrap();
println!("{} bytes allocated", allocated_after - allocated_before);
These days one of the best memory allocator is mimalloc (Microsoft) followed by glibc 2.31 and jemalloc (Facebook) according to this test and this one.
An Alpine preloaded with mimalloc can be found here.
docker pull emerzon/alpine-mimalloc
ToDo: Find more recent comparison tests.
The default Musl memory allocator is super slow according to this test. Better to use mimalloc (Microsoft) or jemalloc (Facebook).
cargo add mimalloc
Declare the mimalloc allocator in your main.rs file.
use mimalloc::MiMalloc;
#[global_allocator]
static GLOBAL: MiMalloc = MiMalloc;