Skip to main content

print_demo/
print_demo.rs

1// SPDX-License-Identifier: MIT OR Apache-2.0
2
3//! `MemoryReport` printing demo — exercises `print_delta` and
4//! `print_before_after` against live measurements.
5//!
6//! Run with:
7//! ```sh
8//! cargo run --features report --example print_demo
9//! ```
10//!
11//! Or to also see raw NVML / DXGI / nvidia-smi diagnostic output:
12//! ```sh
13//! cargo run --features "report debug-output" --example print_demo
14//! ```
15
16use hypomnesis::{MemoryReport, Snapshot};
17
18#[allow(clippy::expect_used)]
19fn main() {
20    println!("--- hypomnesis print_demo ---");
21
22    let before = Snapshot::now(0).expect("Snapshot::now failed");
23
24    // Allocate ~50 MiB on the heap to produce a visible RAM delta.
25    // Use vec![0_u8; ...] (zeroed allocation) so the OS commits pages.
26    let hold: Vec<u8> = vec![0_u8; 50 * 1024 * 1024];
27
28    let after = Snapshot::now(0).expect("Snapshot::now failed");
29    let report = MemoryReport::new(before, after);
30
31    println!("--- print_delta ---");
32    report.print_delta("alloc 50 MiB");
33
34    println!("--- print_before_after ---");
35    report.print_before_after("alloc 50 MiB");
36
37    println!("--- format_delta (returned as String, no newline added by us) ---");
38    print!("{}", report.format_delta("alloc 50 MiB"));
39
40    println!("--- format_before_after (returned as String, no newline added by us) ---");
41    print!("{}", report.format_before_after("alloc 50 MiB"));
42
43    // Keep the allocation alive until here so the after-snapshot still
44    // reflects it (otherwise the optimizer could elide `hold`).
45    drop(hold);
46}