Skip to content

Instantly share code, notes, and snippets.

@lucko
Created February 19, 2021 13:53
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lucko/81033b72927eee0246dc8e8c284664c4 to your computer and use it in GitHub Desktop.
Save lucko/81033b72927eee0246dc8e8c284664c4 to your computer and use it in GitHub Desktop.
Generate a stack trace / thread dump for a Java program

Step 1: Find the process id for the application

Before you can create a stack dump, you need to know (or find out) the process id of the application.

You can use the jps command (or any other approach) to find the process for all running Java programs on the system.

e.g.

➜  jps
27566 Jps
27550 server.jar

In this case, 27550 is the process id of the app we're interested in.

Step 2: Use the jstack command to generate a stack dump for the process

The jstack command "prints stack traces of all threads for a specified Java process".

It needs only one argument - the process id from step 1.

e.g.

➜  jstack 27550
2021-02-19 13:51:15
Full thread dump OpenJDK 64-Bit Server VM (11.0.9.1+1-LTS mixed mode):

Threads class SMR info:
_java_thread_list=0x000000016113e990, length=63, elements={
0x000000011f866000, 0x000000011f86b000, 0x000000011f886000, 0x000000012f905000,
0x0000000128808800, 0x0000000128809000, 0x000000012f80d000, 0x0000000129027000,
0x000000011f9d8000, 0x000000012f9c8800, 0x00000001284c5800, 0x00000001284c4000,
0x00000001284d4800, 0x00000001284d0800, 0x00000001284d1800, 0x000000014796d000,
0x000000012876a800, 0x0000000161851800, 0x000000016188b800, 0x00000001619ec000,
0x0000000147bc0800, 0x0000000147bbd000, 0x0000000161a60000, 0x000000011fb8a800,
0x0000000147a1f000, 0x000000011fb89000, 0x0000000147a2e000, 0x000000011fbd0800,
0x000000011fd0d000, 0x00000001280d7800, 0x0000000128791000, 0x000000011fd09000,
0x000000011fd0a000, 0x000000011f985800, 0x000000011f986800, 0x000000011f987000,
0x000000012d022000, 0x0000000161c56000, 0x00000001286d1800, 0x00000001280d9000,
0x0000000147e31800, 0x00000001286d4800, 0x00000001291e1800, 0x00000001286d5000,
0x0000000147e36000, 0x0000000147e36800, 0x000000012d00b800, 0x000000012d00c000,
0x000000014701b800, 0x000000011f98c800, 0x0000000147e91800, 0x00000001288bc000,
0x000000012d00d000, 0x0000000129141800, 0x0000000129142800, 0x000000012874a800,
0x000000012909c000, 0x00000001282dc000, 0x000000012e095000, 0x0000000129143800,
0x0000000128a42000, 0x00000001470c2800, 0x00000002d2393000
}

"Reference Handler" #2 daemon prio=10 os_prio=31 cpu=4.28ms elapsed=245.15s tid=0x000000011f866000 nid=0x4303 waiting on condition  [0x000000016c97e000]
   java.lang.Thread.State: RUNNABLE
	at java.lang.ref.Reference.waitForReferencePendingList(java.base@11.0.9.1/Native Method)
	at java.lang.ref.Reference.processPendingReferences(java.base@11.0.9.1/Reference.java:241)
	at java.lang.ref.Reference$ReferenceHandler.run(java.base@11.0.9.1/Reference.java:213)

...

:)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment