3 stable releases

1.2.0 Jun 26, 2024
1.1.0 Dec 26, 2022
1.0.0 Mar 20, 2022

#12 in Profiling

Download history 35951/week @ 2024-10-28 39272/week @ 2024-11-04 35032/week @ 2024-11-11 37914/week @ 2024-11-18 33124/week @ 2024-11-25 37189/week @ 2024-12-02 37493/week @ 2024-12-09 37178/week @ 2024-12-16 13602/week @ 2024-12-23 16306/week @ 2024-12-30 37838/week @ 2025-01-06 45570/week @ 2025-01-13 38418/week @ 2025-01-20 37180/week @ 2025-01-27 45236/week @ 2025-02-03 46163/week @ 2025-02-10

169,365 downloads per month
Used in 122 crates (41 directly)

MIT/Apache

16KB
210 lines

Memory Stats

Crates.io License Build Status Dragon Powered

A cross-platform memory profiler for Rust, supporting Windows, Linux, and MacOS. This crate provides two metrics:

  • "Physical" Memory, which corresponds to the Resident Set Size on Linux and MacOS and the Working Set on Windows.
  • "Virtual" Memory, which corresponds to the Virtual Size on Linux and MacOS and the Pagefile Usage on Windows.

Usage

Add memory-stats as a dependency to your Cargo.toml:

[dependencies]
memory-stats = "1.2.0"

Optional Features

serde: Enables serialization and deserialization of the MemoryStats struct.

Example

Here's an example that prints out the current memory usage:

use memory_stats::memory_stats;

fn main() {
    if let Some(usage) = memory_stats() {
        println!("Current physical memory usage: {}", usage.physical_mem);
        println!("Current virtual memory usage: {}", usage.virtual_mem);
    } else {
        println!("Couldn't get the current memory usage :(");
    }
}

Caveats

Getting accurate memory usage on Linux is fairly expensive and not always possible. This crate always attempts to use the statistics from /proc/self/smaps if avaliable. However, since support for /proc/self/smaps might not be compiled in on all kernels, this crate will also use the faster but less accurate statistics from /proc/self/statm as a fallback.

If speed is needed over accuracy, the always_use_statm feature can be enabled to always use the /proc/self/statm statistics.

License

This crate is dual-licensed under either:

at your option.

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~0–8.5MB
~66K SLoC