Skip to content

Commit 7462dca

Browse files
committed
Add some examples that show how to get currernt memory usage
1 parent 5ac8ba6 commit 7462dca

File tree

4 files changed

+116
-2
lines changed

4 files changed

+116
-2
lines changed

README.md

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ Here's a small example that prints out all processes that are running on the sam
1818
process. This is very similar to what "ps" does in its default mode:
1919

2020
```rust
21-
extern crate procfs;
22-
2321
fn main() {
2422
let me = procfs::Process::myself().unwrap();
2523
let tps = procfs::ticks_per_second().unwrap();
@@ -41,6 +39,28 @@ fn main() {
4139
}
4240
```
4341

42+
Here's another example that shows how to get the current memory usage of the current process:
43+
44+
```rust
45+
use procfs::process::Process;
46+
47+
fn main() {
48+
let me = Process::myself().unwrap();
49+
println!("PID: {}", me.pid);
50+
51+
let page_size = procfs::page_size().unwrap() as u64;
52+
println!("Memory page size: {}", page_size);
53+
54+
println!("== Data from /proc/self/stat:");
55+
println!("Total virtual memory used: {} bytes", me.stat.vsize);
56+
println!("Total resident set: {} pages ({} bytes)", me.stat.rss, me.stat.rss as u64 * page_size);
57+
}
58+
```
59+
60+
There are a few ways to get this data, so also checkout the longer
61+
[self_memory](https://github.com/eminence/procfs/blob/master/examples/self_memory.rs) example for more
62+
details.
63+
4464
## Cargo features
4565

4666
The following cargo features are available:

examples/README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,24 @@ Prints out all processes that share the same tty as the current terminal. This
6060
24206 pty/13 0.11 ps
6161
```
6262

63+
## self_memory.rs
64+
65+
Shows several ways to get the current memory usage of the current process
66+
67+
```
68+
PID: 21867
69+
Memory page size: 4096
70+
== Data from /proc/self/stat:
71+
Total virtual memory used: 3436544 bytes
72+
Total resident set: 220 pages (901120 bytes)
73+
74+
== Data from /proc/self/statm:
75+
Total virtual memory used: 839 pages (3436544 bytes)
76+
Total resident set: 220 pages (901120 byte)s
77+
Total shared memory: 191 pages (782336 bytes)
78+
79+
== Data from /proc/self/status:
80+
Total virtual memory used: 3436544 bytes
81+
Total resident set: 901120 bytes
82+
Total shared memory: 782336 bytes
83+
```

examples/self_memory.rs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
use procfs::process::Process;
2+
3+
fn main() {
4+
let me = Process::myself().expect("Unable to load myself!");
5+
println!("PID: {}", me.pid);
6+
7+
let page_size = procfs::page_size().expect("Unable to determinte page size!") as u64;
8+
println!("Memory page size: {}", page_size);
9+
10+
// Note: when comparing the below values to what "top" will display, note that "top" will use
11+
// base-2 units (kibibytes), not base-10 units (kilobytes).
12+
13+
println!("== Data from /proc/self/stat:");
14+
println!("Total virtual memory used: {} bytes", me.stat.vsize);
15+
println!(
16+
"Total resident set: {} pages ({} bytes)",
17+
me.stat.rss,
18+
me.stat.rss as u64 * page_size
19+
);
20+
println!();
21+
22+
if let Ok(statm) = me.statm() {
23+
println!("== Data from /proc/self/statm:");
24+
println!(
25+
"Total virtual memory used: {} pages ({} bytes)",
26+
statm.size,
27+
statm.size * page_size
28+
);
29+
println!(
30+
"Total resident set: {} pages ({} byte)s",
31+
statm.resident,
32+
statm.resident * page_size
33+
);
34+
println!(
35+
"Total shared memory: {} pages ({} bytes)",
36+
statm.shared,
37+
statm.shared * page_size
38+
);
39+
println!();
40+
}
41+
42+
if let Ok(status) = me.status() {
43+
println!("== Data from /proc/self/status:");
44+
println!(
45+
"Total virtual memory used: {} bytes",
46+
status.vmsize.expect("vmsize") * 1024
47+
);
48+
println!(
49+
"Total resident set: {} bytes",
50+
status.vmrss.expect("vmrss") * 1024
51+
);
52+
println!(
53+
"Total shared memory: {} bytes",
54+
status.rssfile.expect("rssfile") * 1024 + status.rssshmem.expect("rssshmem") * 1024
55+
);
56+
}
57+
}

src/process.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,22 @@
3232
//! }
3333
//! }
3434
//! ```
35+
//!
36+
//! Here's a simple example of how you could get the total memory used by the current process.
37+
//! There are several ways to do this. For a longer example, see the `examples/self_memory.rs`
38+
//! file in the git repository. You can run this example with:
39+
//!
40+
//! > cargo run --example=self_memory
41+
//!
42+
//! ```rust
43+
//! # use procfs::process::Process;
44+
//! let me = Process::myself().unwrap();
45+
//! let page_size = procfs::page_size().unwrap() as u64;
46+
//!
47+
//! println!("== Data from /proc/self/stat:");
48+
//! println!("Total virtual memory used: {} bytes", me.stat.vsize);
49+
//! println!("Total resident set: {} pages ({} bytes)", me.stat.rss, me.stat.rss as u64 * page_size);
50+
//! ```
3551
3652
use super::*;
3753
use crate::from_iter;

0 commit comments

Comments
 (0)