Skip to main content

windows_erg/process/
memory.rs

1//! Memory information and statistics.
2
3use windows::Win32::System::ProcessStatus::{GetProcessMemoryInfo, PROCESS_MEMORY_COUNTERS};
4
5use super::processes::Process;
6use super::types::MemoryInfo;
7use crate::error::{Error, ProcessError, ProcessOpenError, Result};
8
9impl Process {
10    /// Get memory usage information for this process.
11    pub fn memory_info(&self) -> Result<MemoryInfo> {
12        let mut counters = windows::Win32::System::ProcessStatus::PROCESS_MEMORY_COUNTERS {
13            cb: std::mem::size_of::<PROCESS_MEMORY_COUNTERS>() as u32,
14            ..Default::default()
15        };
16
17        unsafe { GetProcessMemoryInfo(self.as_raw_handle(), &mut counters, counters.cb) }.map_err(
18            |e| {
19                Error::Process(ProcessError::OpenFailed(ProcessOpenError::with_code(
20                    self.id().as_u32(),
21                    "Failed to get process memory info",
22                    e.code().0,
23                )))
24            },
25        )?;
26
27        Ok(MemoryInfo {
28            working_set: counters.WorkingSetSize,
29            peak_working_set: counters.PeakWorkingSetSize,
30            page_fault_count: counters.PageFaultCount,
31        })
32    }
33}