Howto get memory usage of a started process

2019-07-11 07:17发布

问题:


I try to get the memory usage of a process started via Java.
Can someone give me a hint how to do it for the example Notepad.exe?

    // Memoryusage of the Java programm
    Runtime runtime=Runtime.getRuntime();
    total = runtime.totalMemory();
    System.out.println("System Memory: " + total);

    ProcessBuilder builder = new ProcessBuilder("notepad.exe");
    Process p = builder.start();

Thanks for your help!

回答1:

this post has your answer Java ProcessBuilder memory

quoting top answer:

The new process runs outside the Java process that started it. Allocation of memory to the new process is managed by the operating system, as part of process management. The Java class ProcessBuilder, which provides an interface for starting and communicating with the new process, runs inside the Java process.



回答2:

You could run a system command and dump it's output in a file. Then, parse this file to find the memory usage.



回答3:

This question here including

   ProcessBuilder pb = new ProcessBuilder("cmd.exe",  "/C", "tasklist /fi \"IMAGENAME eq notepad.exe\" /NH");

solved my problem.