If fate is a lonely river, who is your soul ferryman? “– Claire McFaul, Ferryman

Problem a.

  • Does an infinite loop of while cause CPU usage to skyrocket?
  • Do frequent Young GCS cause CPU usage to spike?
  • Does an application with a large number of threads have high CPU utilization?
  • Does a high CPU utilization application have a large number of threads?
  • Do threads that are BLOCKED cause CPU usage to spike?
  • CPU consumption in time-sharing operating systemsUs (User mode)orSy (kernel state)?

2. Think about

1. How do we calculate CPU usage?

CPU% = (1 - idleTime / sysTime ) * 100

  • IdleTime: indicates the idleTime of the CPU

  • SysTime: total time when the CPU is in user mode and kernel mode

2. What are common CPU intensive operations?

It is often said that computationally intensive programs are CPU intensive, so what operations are computationally intensive in Java applications?

Common CPU-intensive operations are listed below:

  • Frequent GC; If the traffic is high, it can lead to frequent GC or even Full GC. When the amount of calls is high, memory allocation is so fast that the GC thread is constantly executing, causing the CPU to spike.
  • Serialization and deserialization.
  • Encryption and decoding.
  • Regular expression. The reason may be that the engine implementation used by Java regular expressions is the NFA automaton, which backtracks when characters are matched.
  • Thread context switch. There are many started threads whose states vary between Blocked (lock wait, I/O wait, etc.) and Running, which can easily happen when lock contention is intense.
  • Some threads continually perform non-blocking operations, such as while (true) statements. If the computation time in the program is long, you can sleep the thread.

3. Is CPU related to processes and threads?

Today, time-sharing operating systems use polling to allocate time slices for process scheduling. If a process is waiting or blocked, it does not use CPU resources. Threads are called light processes and share process resources, so thread scheduling is also time-sharing in the CPU. But in Java, we use the JVM for thread scheduling, so generally there are two modes of thread scheduling: time-sharing and preemptive scheduling. Threads and processes do not use CPU resources while blocking or waiting.

3. The answer

1. Does the infinite loop of while cause CPU usage to spike?

Answer: Yes

Analysis: First, the infinite loop calls the CPU register to count, which consumes CPU resources. Does the CPU switch threads if they remain in an infinite loop? An infinite loop does not give up CPU resources unless the operating system timeslice expires, and it continues to request timeslice from the system until the system has no free time to do anything else.

2. Do frequent Young GCS cause CPU usage spikes?

Answer: Yes

The Young GC itself is a garbage collection operation performed by the JVM, which requires memory calculation and register calls. Therefore, frequent Young GC is bound to consume CPU resources.

Let’s look at a real-world example: a for loop queries a data set from a database and then encapsulates the new data set again. If the memory runs out, the JVM reclaims data that is no longer used. Therefore, if you need a large amount of storage, you may receive CPU usage alerts.

3. Do applications with many threads have high CPU usage?

Answer: Not necessarily

Analysis: If the number of threads is large and the number of threads in the Runnable and Running states is small, CPU utilization is not necessarily high. But in most cases, if the number of threads is large, the common reason is that a large number of threads are BLOCKED and WAITING.

4. Does an application with high CPU usage have a large number of threads?

Answer: Not necessarily

Analysis: The key to high CPU utilization is computation-intensive operations. If a thread has a lot of computation, CPU utilization can also be high, which is why a data scripting task needs to run on a large cluster.

5. Do threads that are BLOCKED cause CPU usage to spike?

Answer: Not necessarily

Analysis: Spikes in CPU usage are more due to context switches or too many runnable threads. A blocked thread does not necessarily lead to increased CPU utilization.

6. The CPUusandsyWhat does high mean in a time-sharing operating system?

We can use the top command to view the VALUES of US and SY for the CPU, as shown below:

  • us: Indicates the percentage of the CPU occupied by the user space. In short, ifusIs high, is caused by our program, and analysis of the thread stack is easy to locate the problem thread.
  • sy: Indicates the percentage of CPU occupied by the kernel space.syWhen it’s high, if it’s programmatically caused, it’s basically thread context switch.

Experience in four.

How can I locate the cause of high CPU usage? The analysis process is briefly described below.

If you find high CPU utilization on an application server, first check parameters such as thread count, JVM, and system load, and then use those parameters to prove the cause of the problem. Second, use JStack to print stack information and use tools to analyze thread usage (fastThread, an online thread analysis tool is recommended).

Online thread analysis tool fastThread address: fastThread. IO /

The next article describes how to use online thread analysis toolsfastThreadAnalyze thread stack information.

Reference Documents:

www.tutorialdocs.com/article/jav…