Skip to main content

basic/
basic.rs

1//! Install `ModAlloc` as the global allocator and report stats.
2//!
3//! Run with: `cargo run --release --example basic`
4
5use mod_alloc::{ModAlloc, Profiler};
6
7#[global_allocator]
8static GLOBAL: ModAlloc = ModAlloc::new();
9
10fn main() {
11    let p = Profiler::start();
12
13    let v: Vec<u64> = (0..1_000).collect();
14    let sum: u64 = v.iter().sum();
15    drop(v);
16
17    let mut owned: Vec<String> = Vec::with_capacity(100);
18    for i in 0..100 {
19        owned.push(format!("item-{i}"));
20    }
21    drop(owned);
22
23    let delta = p.stop();
24    println!("Profiler delta (alloc/total/current = delta; peak = absolute):");
25    println!("  alloc_count:   {}", delta.alloc_count);
26    println!("  total_bytes:   {}", delta.total_bytes);
27    println!("  current_bytes: {}", delta.current_bytes);
28    println!("  peak_bytes:    {}", delta.peak_bytes);
29
30    let snap = GLOBAL.snapshot();
31    println!();
32    println!("Process-wide snapshot:");
33    println!("  alloc_count:   {}", snap.alloc_count);
34    println!("  total_bytes:   {}", snap.total_bytes);
35    println!("  current_bytes: {}", snap.current_bytes);
36    println!("  peak_bytes:    {}", snap.peak_bytes);
37
38    println!();
39    println!("(workload checksum: {sum})");
40}