How do jps, jinfo, jstat, jmap and jstack get info

2020-02-16 05:18发布

问题:

Closed. This question needs to be more focused. It is not currently accepting answers.

Want to improve this question? Update the question so it focuses on one problem only by editing this post.

Closed 10 months ago.

How does jps get information about all the local java processes? Does it connect to some local server process to fetch the information?

How do jinfo, jstat, jmap, and jstack get information about a local java process? Do they connect to some local server process(es) to fetch the information?

Is jstatd only used for providing remote access to local java processes, but not for providing local access to local java processes?

I am running Ubuntu. My question comes from https://stackoverflow.com/a/55669949/156458.

回答1:

jps finds running Java processes by scanning through /tmp/hsperfdata_<username> directory. Each HotSpot-based Java process creates a file in this directory with the name equal to the process ID.

The file /tmp/hsperfdata_<username>/<pid> contains various counters exported by the JVM. These counters can be read by an external process. This is exactly how jstat works. I described jvmstat performance counters in the JavaMagazine article.

So, jstat can always read counters of a local Java process, but in order to be able to monitor a remote machine, jstatd needs to be running.

jmap, jstack and jinfo use Dynamic Attach mechanism. These utilities connect to the target JVM via UNIX-domain socket and send the corresponding command to the JVM. The command is executed by the remote JVM itself. Find more about Dynamic Attach in this answer and in this presentation.