Created
March 15, 2018 18:31
-
-
Save diegolovison/6f918cc08c4c0fd8fb7af70c651cff8d to your computer and use it in GitHub Desktop.
get rss (resident set size) memory using java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.github.diegolovison; | |
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
import java.lang.management.ManagementFactory; | |
public class PrintRssMemory { | |
public static void main(String[] args) throws IOException { | |
System.out.println(getRss()); | |
} | |
private static Long getRss() throws IOException { | |
String command = "ps --no-headers -o rss " + getCurrentPid(); | |
ProcessBuilder builder = new ProcessBuilder().command("bash", "-c", command); | |
Process process = builder.start(); | |
return Long.valueOf(new BufferedReader(new InputStreamReader(process.getInputStream())).readLine()); | |
} | |
// java9: long pid = ProcessHandle.current().pid(); | |
public static synchronized long getCurrentPid() { | |
return Long.valueOf(ManagementFactory.getRuntimeMXBean().getName().split("@")[0]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment