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_'

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

Related Articles

A.M. Rinas

Written by A.M. Rinas

Full-Stack Software Engineer working on web applications, DevOps automation, and databases. More about me on the about page, or say hi through the contact page and LinkedIn.

← Back to all articles