Java Process Info: Finding PID, Working Directory, and Memory of Java Processes
When several Java applications run on the same server — Tomcat instances, a batch job, a scheduler — the first thing you often need is a map of which PID belongs to which application and where it was started. This article covers the commands I use for that, from the JDK's own process lister to /proc inspection.
jps: The JDK's Own Process Lister
jps ships with every JDK and finds only JVMs, which makes it cleaner than grepping ps output:
jps -l
The -l flag shows the full main class name or the jar path instead of just a short name:
12854 /opt/apps/order-service/order-service.jar
12901 org.apache.catalina.startup.Bootstrap
13012 sun.tools.jps.Jps
The last line is jps itself — it lists every JVM, including the one running the command.
PID and Working Directory
PIDs alone are not very useful until you know where each process was started. The working directory of any process is available through /proc/<pid>/cwd (a symlink), and pwdx prints it in one step:
jps | grep -v Jps | while read pid _; do pwdx "$pid"; done
Example output:
12854: /opt/apps/order-service
12901: /opt/tomcat/bin
The grep -v Jps filters out the jps process itself; quoting "$pid" protects against paths containing spaces (unlikely for PIDs, but harmless).
Command Line, JVM Flags, and Environment
jps -lv # full args, including -Xmx and -D properties
ps -e -o pid,rss,vsz,args --sort=-rss | grep java | grep -v grep
tr '\0' ' ' < /proc/12854/cmdline
cat /proc/12854/environ | tr '\0' '\n' | grep -E 'JAVA_HOME|APP_'
jps -lvshows the exact launch flags, which is the fastest way to see the configured heap (-Xmx) of a running JVM.ps ... --sort=-rsslists Java processes by resident memory, useful for spotting a runaway process./proc/<pid>/cmdlineand/proc/<pid>/environexpose the raw arguments and environment; the entries are null-terminated, hence thetr '\0' ...conversion.
Heap vs RSS: Why They Differ
A JVM's -Xmx value is the maximum heap, not its current usage, and RSS includes heap, metaspace, thread stacks, and native memory. To check the actual heap of a running JVM, use jcmd (preferred over the deprecated jmap -heap):
jcmd 12854 GC.heap_info
This reports current used/committed heap, which is the number that matters when a JVM seems to be using more memory than -Xmx suggests — the gap is usually native memory, thread stacks, or metaspace, not a leak in the heap itself.
When This Pays Off
- Two Tomcat instances on one box: confirm which one is which with
jps -lbefore restarting anything. - A deployment script needs the working directory of the running service to find its log path —
pwdxanswers it without guessing. - An application is using far more RAM than its heap setting — compare
psRSS againstjcmd GC.heap_infoto separate heap from native memory.