This is the first day of my participation in the August More text Challenge. For details, see: August More Text Challenge

In a production environment, there are sometimes scenarios where CPU usage is too high and you can’t go down. In this case, external services may be interrupted and the service life of hardware may be affected due to server overload. This article takes a hands-on, step-by-step look at how to quickly locate problem code using the top and jStack commands.

1. Top command

top (table of processes) is a task manager program, found in many Unix-like operating systems, that displays information about CPU and memory utilization.

Wikipedia explains that top (process table) is a task manager program found on many UniX-like operating systems that displays information about CPU and memory usage.

In addition, the top command is a common performance analysis tool in Linux. It displays the resource usage of each process in the system in real time, similar to the Task Manager in Windows. More precisely, the top command provides real-time status monitoring of the system processor. It displays a list of the most CPU-sensitive tasks on the system. This command sorts tasks by CPU usage, memory usage, and execution time.

The preceding figure shows the information displayed after the top command is executed on a Centos 7 system.

  • The first line describes information about the system, including startup time, user logins, and overall CPU load
  • The second line describes the overall performance of the task, that is, the proportion of each process
  • The third line describes the overall CPU information, such as idle percentage, etc
  • The fourth line describes the physical memory ratio
  • The fifth line describes the swap ratio
  • The dynamic list describes detailed process information, such as PID process ID, %CPU CPU usage of the process, TIME+ Total CPU usage since the process started, that is, the sum of CPU usage.

There is another command line tool, htop, which sometimes needs to be installed by yourself. It has the same functions as the traditional top command, but it has more powerful functions and can display more information.

2. Jstack command

Prints Java stack traces of Java threads for a given Java process or core file or a remote debug server.

Jstack prints the Java stack trace of a given Java process or core file or Java thread on a remote debugging server.

For each Java framework, using the JStack directive will print the full class name, method name, “BCI” (bytecode index), and line number (if available). Using the -m option, jStack prints Java and native frames for all threads, as well as “PCS” (program counters).

The usage is very simple, as shown below.

In general, we would first use the JPS command to display the pid commands for all current Java processes.

$jps
Copy the code

Then execute jSTACK on the executing process ID.

$ jstack 15673
Copy the code

If you know your Java process ID, you can use the JStack directive directly. Fast Location service CPU is too high (actual)