Repository: GuillaumeGomez/sysinfo (language: rust)

Issue:
process memory/virtual_memory on windows is only set on second refresh
Tested on sysinfo master (f6c1a25aa22ff323087b5e35c5014d1d81f7fe7d).

Here is a code showing the issue:

```rust
use sysinfo::{Pid, PidExt, ProcessExt, ProcessRefreshKind, System, SystemExt};

fn main() {
    let mut args = std::env::args();
    let pid: u32 = args.nth(1).unwrap().parse().unwrap();

    let mut s = System::new();
    let pid = Pid::from_u32(pid);
    s.refresh_process_specifics(pid, ProcessRefreshKind::new());
    let proc = s.process(pid).unwrap();
    println!(
        "memory: {}, virtual memory: {}",
        proc.memory(),
        proc.virtual_memory()
    );
}
```

On Linux, this displays proper values when giving a valid PID.
On Windows, this displays `memory: 0, virtual memory: 0`.
If a second call to `s.refresh_process_specifics` is done, then it displays proper values.


Discussion / hints:

Fix patch:
diff --git a/src/windows/process.rs b/src/windows/process.rs
index d3e3299eb..c09d60393 100644
--- a/src/windows/process.rs
+++ b/src/windows/process.rs
@@ -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;
     }
diff --git a/src/windows/system.rs b/src/windows/system.rs
index d228f756f..e9f3ac7fc 100644
--- a/src/windows/system.rs
+++ b/src/windows/system.rs
@@ -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};
@@ -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)


Test patch:
diff --git a/tests/process.rs b/tests/process.rs
index 4c8bc23e2..dc263fcbf 100644
--- a/tests/process.rs
+++ b/tests/process.rs
@@ -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);
+}