17

When gdb is used for debugging purposes in Java:

  • What's its practical use?
  • What are its limitations?
  • How is it compared to other debuggers?
Javier
  • 343
  • 1
  • 3
  • 8

3 Answers3

16

I would say gdb is used for debugging Java when the programmer is coming from a different language and is already familiar with gdb. Otherwise, it seems like a strange choice given that there are more popular alternatives for Java: jdb, JSwat, eclipse, netbeans, etc.

Here is a tutorial for debugging Java with gdb.

toogley
  • 551
  • 1
  • 4
  • 11
dbyrne
  • 59,111
  • 13
  • 86
  • 103
  • Only more popular or better? Could you be more specific? From what I've read it has limitations (or maybe other uses). See [Debugging with gdb](http://publib.boulder.ibm.com/infocenter/realtime/v1r0/index.jsp?topic=/com.ibm.rt.doc.10/diag/problem_determination/linux_gdb.html). – Javier Feb 23 '11 at 21:04
  • I tend to agree with the article you've linked. gdb is probably more limited for debugging pure java code than other popular options. It might be more useful for debugging the actual JVM though. I could see it being helpful for debugging alternative JVM languages like JRuby, Clojure, or Scala as well. My gdb experience is limited to C and C++ though, so I don't feel qualified to comment any further. – dbyrne Feb 23 '11 at 21:24
4

GDB is 99% of the time is not useful for debugging Java, but it can help you find native memory leaks which the java debuggers can't.

Tom Larkworthy
  • 2,104
  • 1
  • 20
  • 29
1

gdb can be useful to take heap dumps fast.

Normally one would dump the heap by using jmap or similar tools. But when using this tools, the JVM performs garbage collection before dumping, which can take hours on a hanging process. With the steps below you can dump fast, restart the process and convert the dump afterwards.

gdb --pid <PID>
gcore /tmp/gdbdump.core
detach
quit

Afterwards you need to convert the core file to a hprof file to analyze it with Java tools:

jmap -dump:format=b,file=/tmp/javadump.hprof /path/tojdk/java /tmp/gdbdump.core

Be aware:

  1. You need to use the same version of the JDK like the JRE/JDK version the process was running with.
  2. The conversion can take some hours.
  3. For Windows: There seems to be similar process: https://stackoverflow.com/a/35822193/4248273
Yeti
  • 1,108
  • 19
  • 28