Howto get memory usage of a started process

2019-07-11 06:39发布


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!

3条回答
聊天终结者
2楼-- · 2019-07-11 06:58

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.

查看更多
放荡不羁爱自由
3楼-- · 2019-07-11 07:20

This question here including

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

solved my problem.

查看更多
Bombasti
4楼-- · 2019-07-11 07:22

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

查看更多
登录 后发表回答