Skip to content

Instantly share code, notes, and snippets.

@diegolovison
Created March 15, 2018 18:31
Show Gist options
  • Save diegolovison/6f918cc08c4c0fd8fb7af70c651cff8d to your computer and use it in GitHub Desktop.
Save diegolovison/6f918cc08c4c0fd8fb7af70c651cff8d to your computer and use it in GitHub Desktop.
get rss (resident set size) memory using java
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