Home > Java > javaTutorial > body text

How to use JVM commands in java to troubleshoot servers

青灯夜游
Release: 2018-10-17 15:28:30
forward
2429 people have browsed it

This article will introduce to you how to use JVM commands in java to implement server troubleshooting. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

1. top (Linux command)

Execute the top command: (View the details of process 15477, used below)

 

System information (first five lines):

    • Line 1: Top task queue information (system running status and average load), the same as the uptime command result .

      • Average system load: In the case of a single-core CPU, 0.00 means no load, 1.00 means just full load, more than 1 means overload, the ideal value is 0.7;

      • Multi-core CPU load: number of CPU cores * ideal value 0.7 = ideal load, for example: 4-core CPU load does not exceed 2.8, which means there is no high load.

      • Format: up xx days, HH:MM

      • For example: 241 days, 20:11, means continuous Running for 241 days, 20 hours and 11 minutes

      • Section 1: System current time, for example: 16:07:37

      • Paragraph 2: System running time, the time before restarting. The longer the time, the more stable the system will be.

      • Paragraph 3: The number of currently logged in users, for example: 1 user, indicating that there is currently only 1 user logged in

      • Paragraph 4 Segment: System load, that is, the average length of the task queue. The three values ​​​​respectively count the average system load in the last 1, 5, and 15 minutes

    • Line 2: Tasks process related information

      • Paragraph 1: Total number of processes, for example: Tasks: 231 total, indicating a total of 231 processes running

      • Paragraph 2: Number of running processes, for example: 1 running,

      • Paragraph 3: Number of sleeping processes, for example: 230 sleeping,

      • Paragraph 4: Number of stopped processes, for example: 0 stopped,

      • Paragraph 5: Number of zombie processes, for example: 0 zombie

    • Line 3: Cpus CPU related information. If it is a multi-core CPU, press the number 1 to display the CPU information of each core. At this time, line 1 will be converted to the Cpu core number line, and the number 1 can be switched back and forth. .

      • Paragraph 1: us Percentage of CPU occupied by user space, for example: Cpu (s): 12.7%us,

      • Paragraph 2: sy The percentage of CPU occupied by the kernel space, for example: 8.4%sy,

      • Paragraph 3: ni The percentage of CPU occupied by processes that have changed priorities in the user process space, for example: 0.0%ni,

      • 4th Section: id Idle CPU percentage, for example: 77.1%id,

      • Section 5: wa Percentage of CPU time waiting for input and output, For example: 0.0%wa,

      • Paragraph 6: hi The total time spent by the CPU serving hardware interrupts, for example: 0.0%hi,

      • Paragraph 7: si The total time spent on CPU service soft interrupt, for example: 1.8%si,

      • ##Paragraph 8 :

        st Steal time The CPU time stolen by the virtual machine by the hypervisor (if the vm is currently under a hypervisor, the hypervisor actually consumes part of the CPU processing time)

    • Line 4:

      Mem memory related information (Mem: 12196436k total, 12056552k used, 139884k free, 64564k buffers)

      • Paragraph 1: Total amount of physical memory, for example: Mem: 12196436k total,

      • Paragraph 2: Total amount of physical memory used, for example: 12056552k used,

      • Section 3: Total free memory, for example: Mem: 139884k free,

      • Section 4: Used as kernel The amount of cached memory, for example: 64564k buffers

    • Line 5: Swap swap partition related information (Swap: 2097144k total, 151016k used, 1946128k free, 3120236k cached)


      • Paragraph 1: Total amount of swap area, for example: Swap: 2097144k total,

      • Paragraph 2: Used Total amount of swap area, for example: 151016k used,

      • Section 3: Total amount of free swap area, for example: 1946128k free,

      • Segment 4: Total buffered swap area, 3120236k cached

##Process information:

Press in the top command Press f to view the displayed column information, and press the corresponding letter to turn on/off the column. An uppercase letter means on, and a lowercase letter means off. The columns marked with * are the default columns.

    • ##A:

      PID = (Process Id) Process Id;

      E:
    • USER
    • = (User Name) The user name of the process owner;

    • H: PR = (Priority) Priority

    • I: NI = (Nice value) nice value. Negative values ​​represent high priority, positive values ​​represent low priority

    • O: VIRT = (Virtual Image (kb)) Process usage The total amount of virtual memory, unit kb. VIRT=SWAP RES

    • Q: RES = (Resident size (kb)) The physical memory used by the process and not swapped out Size, unit kb. RES=CODE DATA

    • T: SHR = (Shared Mem size (kb)) Shared memory size, unit kb

    • W: S = (Process Status) Process status. D=uninterruptible sleep state, R=running, S=sleep, T=track/stop, Z=zombie process

    • K: % CPU = (CPU usage) The percentage of CPU time usage since the last update

    • N: %MEM = (Memory usage (RES)) The percentage of physical memory used by the process

    • ##M:

      TIME = (CPU Time, hundredths ) Total CPU time used by the process, unit 1/100 second b:
      PPID = (Parent Process Pid) Parent Process Id c:
      RUSER = (Real user name) d:
      UID = (User Id) The user ID of the process owner f:
      GROUP = (Group Name) The group name of the process owner g:
      TTY = (Controlling Tty) The terminal name of the startup process. Processes that are not started from the terminal are displayed as? j:
      P = (Last used cpu (SMP)) The last used CPU is only meaningful in a multi-CPU environment p:
      SWAP = (Swapped size (kb)) The size of the virtual memory used by the process that is swapped out, in kb l:
      TIME = (CPU Time) The CPU used by the process Total time, in seconds r:
      CODE = (Code size (kb)) The physical memory size occupied by the executable code, in kb s:
      DATA = ( Data Stack size (kb)) The size of physical memory occupied by parts other than executable code (data segment stack), unit kb u:
      nFLT = (Page Fault count) Number of page faults v:
      nDRT = (Dirty Pages count) The number of pages that have been modified since the last time it was written y:
      WCHAN = (Sleeping in Function) If the process is in Sleep, then display the system function name in sleep z:
      Flags = (Task Flags <sched.h>) Task flags, refer to sched.h

    • X:

      COMMAND = (Command name/line) command name/command line

Reference Linux Performance Analysis Tool top command detailed explanation

Execute top -Hp PID, such as top -Hp 15477

View the threads in a process

Note: At this time, the PID is the thread id

If there is an exception in thread 15571 and you need to check it, use jstack to print the stack and check the status of thread 15571 (15571 hexadecimal = 3cd3)

## 2. jstack

Reference java command--jstack tool

Java command learning series (2)-Jstack

Need to go to the JDK installation directory to use it (you can view the java process through ps x , get the jdk installation directory)

./jstack PID (process id) ./jstack 15477

The red box is thread 15571 (hexadecimal = 3cd3) status

Analyze jstack log:

Monitor Monitor:

Monitor is the main means in Java to achieve mutual exclusion and collaboration between threads. It can be regarded as an object or Class lock.

Every object has only one monitor.

The following figure describes the relationship between the thread and the Monitor, as well as the state transition of the thread:

Enter Set: Indicates that the thread requests to acquire the object's lock through synchronized. If the object is locked, enter the owner; otherwise wait in the entry area. Once the object lock is released by other threads, it immediately participates in competition.

Owner (The Owner): Indicates that a thread successfully competed for the object lock.

Wait Set: Indicates that the thread releases the object's lock through the object's object.wait() method and waits in the waiting area to be awakened.

As can be seen from the figure, a Monitor can only be owned by one thread at a certain moment, and that thread is <span style='font-family: "Microsoft YaHei"'>Active Thread</span> , while other threads are <span style='font-family: "Microsoft YaHei"'>Waiting</span> <span style='font-family: "Microsoft YaHei"'>Thread</span>, respectively in two queues <span style='font-family: "Microsoft YaHei"'>Entry</span> ## Wait inside #Set<span style='font-family: "Microsoft YaHei"'></span> and Wait<span style='font-family: "Microsoft YaHei"'> </span>Set<span style='font-family: "Microsoft YaHei"'></span>.

The thread action waiting in Entry<span style='font-family: "Microsoft YaHei"'> </span>Set<span style='font-family: "Microsoft YaHei"'></span> is Waiting for monitor entry<span style='font-family: "Microsoft YaHei"'>. </span>

The thread action waiting in Wait<span style='font-family: "Microsoft YaHei"'> </span>Set<span style='font-family: "Microsoft YaHei"'></span> is in Object.wait()<span style='font-family: "Microsoft YaHei"'></span>. When a thread applies to enter the critical section, it enters the Entry Set queue.

(We call the code segment protected by synchronized as the critical section. When a thread applies to enter the critical section, it enters the "Entry Set" queue)

Thread status:

NEW: Not started. Will not appear in Dump.

RUNNABLE: Executed in the virtual machine, running state. The Owner Area

BLOCKED: Blocked and waiting for the monitor lock. Waiting for the lock in the Entry Set area.

WATING: Wait indefinitely for another thread to perform a specific operation. In the Wait Set areaWait for a certain condition or monitor to occur, usually staying in statements such as wait().

TIMED_WATING: Time-limited waiting for a specific operation from another thread. In the Wait Set area, the difference between and WAITING is that wait() and other statements add a time limit of wait(timeout).

TERMINATED: Exited.

Call modification

Indicates additional important operations when the thread calls the method. Modify the method call above.

locked

Target: Use synchronized to apply for object lock successfully, the owner of the monitor. The Owner area.

waiting to lock

Target: Application for object lock using synchronized failed, Waiting for the lock in the Entry Set area. The thread status is Blocked

waiting on

Goal: After successfully applying for the object lock using synchronized, release the lock, Wait for the lock in the Wait Set area. The thread status is WAITING or TIMED_WATING

parking to wait for

Target: park() is called, is in the Wait Set area, waiting for permission.

(park is a basic thread blocking primitive that does not block on objects through the monitor.

park: Enter the WAITING state, compare wait can make the thread WAITING without acquiring the lock and wake it up through unpark)

Thread action

The reason why the thread status is generated.

runnable: The Owner area, status RUNNABLE

in Object.wait(): Call wait(), Wait Set area, status is WAITING or TIMED_WAITING, modify waiting on

waiting for monitor entry: waiting for lock, Entry Set area, status BLOCKED, modify waiting to lock

waiting on condition : Parked due to certain conditions, Wait Set area, status is parking to wait for

sleeping: The sleeping thread called Thread.sleep()

Summary

1. Check the thread dump. First look at the thread status/thread action (more intuitive) to determine which stage the thread is currently in. Then look at the call modification and lock status, and you can basically determine whether there is a problem with the secondary thread;

2. You can print thread snapshots multiple times in a short period of time (the time period when there may be problems), and then check whether there may be problems This situation of a certain thread can effectively find the problem.

3. jps

Similar to Linux command ps

Reference Java command learning series (1) - Jps

./jps

./jps -q

./jps -m

./jps -l

./jps -v

## 4. jmap

Reference : Java command learning series (3) - Jmap

java command--jmap command usage

jmap -heap PID: heap usage

jmap -histo PID: object Situation

(jmap -histo:live When this command is executed, the JVM will trigger gc first, and then collect statistical information

Focus on the classes on the project: [C is a string array, and String is used; [B is a byte array, used by the network layer. It doesn’t matter if the two are larger.

 [C is a char[]
 [S is a short[ ]
 [I is a int[]
 [B is a byte[]
 [[I is a int[] []

Summary: The above is the entire content of this article, I hope it will be helpful to everyone's learning. For more related tutorials, please visit

Java Video Tutorial, javaDevelopment Graphic Tutorial, bootstrap Video Tutorial!

The above is the detailed content of How to use JVM commands in java to troubleshoot servers. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:cnblogs.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!