Crates.io | memory-stats |
lib.rs | memory-stats |
version | 1.2.0 |
source | src |
created_at | 2022-03-20 20:34:42.685603 |
updated_at | 2024-06-26 19:33:36.254428 |
description | A cross-platform memory profiler for Rust. |
homepage | |
repository | https://github.com/Arc-blroth/memory-stats |
max_upload_size | |
id | 553716 |
size | 32,370 |
A cross-platform memory profiler for Rust, supporting Windows, Linux, and MacOS. This crate provides two metrics:
Add memory-stats
as a dependency to your Cargo.toml
:
[dependencies]
memory-stats = "1.2.0"
serde
: Enables serialization and deserialization of the MemoryStats
struct.
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 :(");
}
}
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.
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.