How to check the process start time and duration

Ps - eo pid, lstart etime, CMD | grep -v grep | grep process nameCopy the code

Pid Process ID lstart Start time etime Duration CMD start command

2. Check the CPU 100%

1. Identify the process of the problem.

Top-c Displays the list of running processes. Shift + P Sorts the list by CPU usage. The Java process whose PID is 28555 occupies 200% of the CPU, indicating that the process is faulty. Through ps aux | grep command PID, can further determine whether tomcat process problems.

2. Locate the thread or code

Thread 28802: thread 28802: thread 28802: thread 28802

ps -mp pid -o THREAD,tid,time
Copy the code

or

Use the command top-hp {pid}, also shift + p to sort the thread list by CPU usage

3. Secondly, convert the required thread ID (TID) to hexadecimal format:

For example, the hexadecimal value of TID =28802 is 7082

printf "%x\n" tid
Copy the code

4. Finally print the stack information of the thread:

jstack pid | grep tid -A 30
Copy the code

Such as: jstack 28555 | grep 7082-30 A

Jstack: command provided by Java. You can view the current thread stack status of a process. -a 10 Indicates the 10 lines after the search. Based on the output of the command, you can locate the current running status, running code, and deadlock status of all threads of A process.

The thread snapshot format is uniform. The following is a simple illustration of a thread snapshot:

"main" #1 prio=5 os_prio=0 tid=0x0000000002792800 nid=0x3e1c runnable [0x00000000025cf000] 
Copy the code

3. Check for excessive memory

1. Review

First let’s review the Java process memory allocation, so that we can explore the following ideas.

Take JDK1.8, the version we use online. There are many summaries on the JVM memory allocation web, so I won’t write again.

The JVM memory region is divided into two parts: heap and non-heap.

  • Heap area: this is what we know as the Cenozoic old age.
  • Non-heap area: The non-heap area is shown in the figure, with metadata area and direct memory.

An additional note here is that the permanent generation (native to JDK8) holds classes used by the JVM runtime, and objects of the permanent generation are garbage collected during full GC.

2. Set JVM memory parameters

  • Heap memory Settings

Heap memory (total) The minimum and maximum heap memory are set by -xms and -xmx respectively

New Generation is set by -xmn, -xx :SurvivorRatio=m Sets the size ratio between Eden and two Survivor zones. -xx :NewRatio=n Sets the size ratio of New Generation to Old Generation.

The stack size for each thread is set by · -xss ·, 1M for each thread after JDK5.0 and 256K for each thread before. Reducing this value generates more threads for the same physical memory. However, the operating system has a limit on the number of threads in a process, which cannot be generated indefinitely. The experience value is about 3000~5000.

  • Non-heap memory Settings

Non-heap memory The minimum and maximum non-heap memory sizes are set by -xx :PermSize=n and -xx :MaxPermSize=n, respectively

Thinking of the February

1. Locate the process with the highest memory usage

Run the top -c command to display the list of running processes. Shift + m sorts the processes by memory usage

2. Use Jmap to generate heap dump snapshots

The PS command can check the CPU usage of a process, but cannot check the memory usage of a specific thread in a process.

ps -mp 9004 -o THREAD,tid,time,rss,size,%mem
Copy the code

We had to find another way, but luckily Java provides a good memory monitoring tool: the jmap command

The jmap command is used in the following ways:

jmap [pid]
jmap -histo:live [pid] > a.log
jmap -dump:live,format=b,file=xxx.xxx [pid]
Copy the code

The last two are used most often. In the command, jmap-histo :live [pid] displays the number of active objects created by the current Java process and memory usage.

Jmap -dump:live,format=b,file=xxx. XXX [PID] can export the Memory usage of the current Java process, which can be analyzed by a dedicated Memory analysis Tool (such as MAT: Memory Analyzer Tool). This command is helpful in analyzing whether there are memory leaks.

3. Use MAT to analyze dump files

Note: By default, mat’s maximum memory is 1024M, and our dump files tend to be larger than 1024M, so we need to adjust and find memoryAnalyzer.ini in mat’s home directory, To change -xmx1024m to a larger size than dump, I changed it to -xmx6g

Four,

1. Common JVM commands

JPS: lists running VM processes. Jinfo: displays and adjusts VM parameters in real time. Jstat: monitors vm running information, including class loading, memory, garbage collection, and JIT compilation. To generate heap dump snapshots, you can also query finalize execution queue, Java heap and persistent generation details. Jstack: To generate thread snapshots of the CURRENT time of the VM. Jhat: Vm heap dump snapshot analysis tool. Used together with JMAP to analyze heap dump snapshots generated by JMAP, similar to MATCopy the code

Jinfo First, we use jINFO PID to view the heap-related parameters of the current JVM:

As can be seen, the maximum heap capacity is: 4G.

jstat

Next, we use the command jstat -gcutil pid 1s 5 to check the current heap usage within 5 seconds:



As above, the new generation is full (97.33%), the old generation is full (100%), and FGC is up to 967 times! The number of FGC is so large that FGC should normally account for 1%-5% of the total GC (YGC+FGC).

jmap

In addition to the jstat command, we can also use the jmap-heap PID to view the current JVM heap

2. Troubleshooting procedure

1) Find the corresponding process: PID 2) Generate a thread snapshot stack (or heap dump snapshot: hprof) 3) analyze the snapshot (or heap dump snapshot) to locate the problem

3. Memory leaks and memory overflows are 100% related to the CPU

reference

MAT: An online memory leak: a complete out-of-heap MEMORY leak: a complete out-of-heap MEMORY leak: a record of Java memory leak analysis series 3: The use of the Jstat command and VM Thread analysis