Skip to content

Instantly share code, notes, and snippets.

@matthewdowney
Created September 13, 2020 01:03
Show Gist options
  • Save matthewdowney/bd107177d0db92879da43af156f9b477 to your computer and use it in GitHub Desktop.
Save matthewdowney/bd107177d0db92879da43af156f9b477 to your computer and use it in GitHub Desktop.
Programmatic JVM Thread Dump in Clojure
(import '(java.lang.management ManagementFactory))
(defn thread-dump []
(with-out-str
(let [tmx-bean (ManagementFactory/getThreadMXBean)
tids (.getAllThreadIds tmx-bean)
max-stack-depth 100
tinfo (.getThreadInfo tmx-bean tids max-stack-depth)]
(doseq [tinf tinfo]
(println (str \" (.getThreadName tinf) \"))
(println (str " java.lang.Thread.State: " (.getThreadState tinf)))
(doseq [ste (.getStackTrace tinf)]
(println " at" (str ste)))
(println "\n")))))
(comment
"Usage"
(spit "thread-dump.txt" (thread-dump)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment