Skip to content

Commit

Permalink
Merge pull request #1085 from JustRustThings/fix-windows-process-memo…
Browse files Browse the repository at this point in the history
…ry-refresh

fix: refresh process memory on Windows on first update
  • Loading branch information
GuillaumeGomez authored Oct 14, 2023
2 parents 3c77aa8 + bc86118 commit ae650f7
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/windows/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,7 @@ impl ProcessInner {
if refresh_kind.disk_usage() {
update_disk_usage(self);
}
update_memory(self);
self.run_time = now.saturating_sub(self.start_time());
self.updated = true;
}
Expand Down
3 changes: 1 addition & 2 deletions src/windows/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use crate::{CpuRefreshKind, LoadAvg, Pid, ProcessRefreshKind};

use crate::sys::cpu::*;
use crate::sys::process::{get_start_time, update_memory};
use crate::sys::process::get_start_time;
use crate::sys::tools::*;
use crate::sys::utils::{get_now, get_reg_string_value, get_reg_value_u32};
use crate::{Process, ProcessInner};
Expand Down Expand Up @@ -473,7 +473,6 @@ fn refresh_existing_process(
} else {
return Some(false);
}
update_memory(proc_);
proc_.update(refresh_kind, nb_cpus, now);
proc_.updated = false;
Some(true)
Expand Down
20 changes: 20 additions & 0 deletions tests/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -659,3 +659,23 @@ fn test_process_creds() {
true
}));
}

// Regression test for <https://github.com/GuillaumeGomez/sysinfo/issues/1084>
#[test]
fn test_process_memory_refresh() {
if !sysinfo::IS_SUPPORTED || cfg!(feature = "apple-sandbox") {
return;
}

// Ensure the process memory is available on the first refresh.
let mut s = sysinfo::System::new();

// Refresh our own process
let pid = Pid::from_u32(std::process::id());
s.refresh_process_specifics(pid, sysinfo::ProcessRefreshKind::new());

let proc = s.process(pid).unwrap();
// Check that the memory values re not empty.
assert!(proc.memory() > 0);
assert!(proc.virtual_memory() > 0);
}

0 comments on commit ae650f7

Please sign in to comment.