Overview
Opening and underlying principles of Java remote debugging
Turn on Java remote debugging
Java Remote Debugging allows developers to debug Java applications running on remote servers in local IDEs (such as Eclipse, IntelliJ IDEA, etc.). The following are the basic steps to enable Java remote debugging:
-
Configure Java applications on remote servers:
- When starting a Java application, enable remote debugging by adding JVM parameters. These parameters usually include
、
(Specify the debug port),
(usually set to
y
orn
, depending on whether it is server mode), etc., but in actual use, what we use more often is-agentlib:jdwp
Parameters and their subparameters. - For example, suppose we want to enable remote debugging on port 5005, we can add the following parameters to the startup command:
java -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005 -jar
-
transport=dt_socket
: Specify the use of socket transfer. -
server=y
: Indicates that the current JVM is used as a debugging server. -
suspend=n
: means that the JVM does not pause when started and executes immediately. If set toy
, the JVM will pause while waiting for the debugger to connect. -
address=5005
: Specify the debug port.
-
- When starting a Java application, enable remote debugging by adding JVM parameters. These parameters usually include
-
Configure local IDE:
- Create a remote debug configuration in the IDE.
- Specifies the IP address and debug port of the remote host (same as configured on the server).
- Connect and start debugging.
The underlying principle
The underlying principle of Java remote debugging mainly relies on the debugging interfaces of Java Debug Wire Protocol (JDWP) and Java virtual machine (JVM).
-
JDWP:
- JDWP is a communication protocol for Java application debugging. It defines the communication specification between the debugger (debug tool in the IDE) and the debugged Java virtual machine (JVM).
- JDWP supports a variety of debugging functions, such as setting breakpoints, stepping, viewing variable values, monitoring threads, etc.
- JDWP uses sockets (Sockets) as the communication transport layer, so network connections need to be established between the debugger and the JVM.
-
JVM debugging interface:
- The JVM provides a debugging interface (commonly called Java Virtual Machine Tool Interface, JVMTI or Java Debug Interface, JDI) that allows the debugger to interact with the JVM.
- When the JVM starts, if
-agentlib:jdwp
Parameters, the JVM will load the JDWP proxy library. This proxy library implements the JDWP protocol and serves as a bridge between the JVM and the debugger. - The debugger sends debug commands to the JVM through the JDWP protocol, and the JVM receives these commands through the JDWP proxy library and performs corresponding debugging operations according to the commands.
-
Communication process:
- When the debugger connects to a remote JVM, a JDWP session is established between them.
- The debugger can send various JDWP commands to query the status of the JVM, control the execution of the JVM (such as pausing and restoring threads), read and modify variable values, etc.
- The JVM receives these commands through the JDWP proxy library and calls the corresponding JVM debugging interface to implement the debugging function.
- Data during debugging (such as variable values, thread states, etc.) will also be transferred between the debugger and the JVM through the JDWP protocol.
In short, Java remote debugging realizes communication and interaction between the debugger and the remote JVM through the JDWP protocol and the JVM debugging interface. Developers can easily debug Java applications running on remote servers in the local IDE.
JVM parameters
The following are some common JVM parameters, displayed in table form, and are attached with examples of use:
Parameter Category | Parameter name | illustrate | Examples of use |
---|---|---|---|
Standard parameters | -version | Display Java version information | java -version |
-help | Show Java commands help | java -help | |
-server | Start the JVM in Server mode | java -server -jar | |
-cp or -classpath | Specify the class search path | java -cp .;lib/ MyApp | |
Non-standard parameters (-X) | -Xint | Explain execution, not perform JIT compilation | java -Xint -jar |
-Xcomp | Compile local code for the first time | java -Xcomp -jar | |
-Xmixed | Mixed mode, the JVM decides whether to explain or compile the execution | java -Xmixed -jar | |
Non-standardized parameters (-XX) | Boolean Type | -XX:[±]name | java -XX:+UseG1GC -jar (enable G1 garbage collector) |
java -XX:-UseConcMarkSweepGC -jar (disable CMS garbage collector) | |||
Non-Boolean type | -XX:name=value | java -XX:MaxGCPauseMillis=500 -jar (set the maximum GC pause time to 500 milliseconds) | |
java -XX:HeapDumpPath=/path/to/dump -jar (set the heap dump file path) | |||
Memory management | -Xms | Set the initial heap memory size of JVM | java -Xms512m -jar (512MB) |
-Xmx | Set the maximum heap memory size of JVM | java -Xmx2g -jar (2GB) | |
-Xmn | Set the new generation size | java -Xmn256m -jar (256MB) | |
-XX:NewRatio | Set the ratio of the new generation to the old generation | java -XX:NewRatio=3 -jar (New Generation: Old Generation=1:3) | |
-XX:SurvivorRatio | Set the ratio of Eden area to Survivor area | java -XX:SurvivorRatio=4 -jar (Eden:Survivor=4:1) | |
Garbage recycling | -XX:+UseG1GC | Enable G1 garbage collector | As shown above |
-XX:+UseConcMarkSweepGC | Enable CMS garbage collector (before Java 9) | As shown in the disable example above | |
-XX:+HeapDumpOnOutOfMemoryError | Generate heap dump file when memory overflows | As shown in the example setting heap dump path above, you can use it in combination with this parameter | |
Performance optimization | -XX:+TieredCompilation | Enable multi-level compiler | java -XX:+TieredCompilation -jar |
-XX:+UseCompressedOops | Enable pointer compression | java -XX:+UseCompressedOops -jar | |
Debugging and monitoring | -XX:+PrintGCDetails | Print detailed GC logs | java -XX:+PrintGCDetails -jar |
-XX:+PrintGCDateStamps | Print the time stamp that occurs when GC | java -XX:+PrintGCDateStamps -jar | |
-Xloggc: | Specify the path to the GC log file | java -Xloggc: -jar |
Notice:
- The parameters in the above table are examples only and not all JVM parameters. There are many JVM parameters, and as Java versions are updated, new parameters may be introduced or old parameters may be discarded.
- In actual use, parameters should be configured and tuned according to the specific needs of the application and the hardware environment.
- Some parameters may need to be valid in a specific JVM version or mode.
- When using non-standard parameters (-X and -XX), be cautious, as these parameters may vary in different versions of the JVM and some parameters may cause unstable JVM behavior or performance degradation.
Summarize
This is the article about how to enable remote modulation in Java Advanced Learning. For more related content on Java to enable remote modulation, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!