Processing of Java Web Server CPU consumption too high

Source: Internet
Author: User

In peacetime projects sometimes encountered high CPU situation, based on their own limited experience to write a share, here the server is based on the Linux platform.

The CPU's occupancy thread type is generally divided into two types:
US: User space consumes CPU percentage
SY: CPU percent of kernel space occupied
Under Linux You can view the details through the top command, as shown in the following example:

In general, the CPU US high resolution method:
The main reason for CPU US high is that the execution thread does not need any pending actions and is executed all the time, causing the CPU to have no chance to schedule other threads to execute.
Solution for High CPU sy:
The main reason for high CPU Sy is that the running state of the thread should be switched frequently, and in this case, a common optimization method is to reduce the number of threads .

My usual specific steps are as follows:

1. Get the highest number of thread IDs (ps-process State)

Ps-elo PID,LWP,PCPU | grep 15285|sort-nk 3

2. Exporting Java thread stack information

Command: kill-3 [PID] or jstack

3. From the stack information to find a few of the number of threads

Command:sort File name | uniq-c | Sort-nk 1

4. The top 10 threads and threads that consume the highest number of threads, respectively, take up the top 10 threads on the CPU

This step is the most critical, to find out these anomalies, such as a business function point of high CPU or a certain type of thread, which is closely related to the business and specific procedures, there is not much to say.

5. Analyze the top 10 threads that occupy the highest CPU, and the results are as follows:

0X1A5:GC thread "Concurrent mark-sweep GC THread#0 "Prio=1 tid=0x0000002b29df1400 nid=0x1a5runnable 0x35f:memcache Thread "Memcache5-cachethread" Prio=1 tid=0x0000002b3d277de0 nid=0x35frunnable [0x0000000042755000. 0x0000000042755c30]0x35e:memcache thread "Memcache1-cachethread" prio=1 tid=0x0000002b371212f0 nid=0x35e runnable [ 0x0000000042654000. 0x0000000042654cb0]0x14e9:rmi thread "RMI TCPConnection (102) -10.23.241.59 "daemon prio=1 tid=0x0000002b37e72790 nid=0x14e9 runnable [0x000000005103c000. 0x000000005103deb0]0x1be:memcache thread "Memcache5-cachethread" prio=1 tid=0x0000002b3dba4790 nid=0x1be runnable [ 0x0000000041a48000. 0x0000000041a48eb0]0x1bd:memcache thread "memcache1-cachethread "Prio=1 tid=0x0000002b3fb70340 nid=0x1bd runnable [0x0000000041947000]. 0X0000000041947B30]0X1AF:JDK compilation thread "CompilerThread1" daemon prio=1 tid=0x0000002b30e11620 nid=0x1af waiting on condition [0x0000000000000000. 0X000000004123F7C0]0X1AE:JDK compilation thread "CompilerThread0" daemon prio=1 tid=0x0000002b30e10280 nid=0x1ae waiting on condition [0x0000000000000000. The 0x000000004113e440]0x1684:http request thread, through which the thread stack can see the hibernate query called "http-8080-processor123"Daemon prio=1 tid=0x0000002b37dfd9d0 nid=0x1684 runnable [0x000000005afd7000]. 0X000000005AFDCD30]-------------------------------------------------------------------------------------------- ------------------------------------

6. System optimization

If you find out which function points occupy high CPU, the next need to optimize, can be from the business and technical means to enter the line, usually work more commonly used technical means:

Flex time: Requests for high usage are dispersed to different times , such as queuing or asynchrony , to reduce requests that are processed at the same time .

batch or timed tasks : Combine requests into batches, which can make time really useful for processing, rather than preparing for network transmissions . (Reduce network transport, database connections, socket connections)

Caching: caches the results , and space-time is changed .

--------------------------------------------------------------------------------------------------------------- -----------------

7. If the GC thread is more time-consuming, further positioning is required:

First look at the GC policy is reasonable, and then use the command jmap-f-dump:live,file=jmap.hprof [PID] to export the memory dump file , I generally every 5s, exported 3 times. Analyze the files from Eclipse Memory Analyzer to analyze which class takes up more memory and analyze where there may be memory leaks.

Note that the JVM allocates memory when the allocation of a large object is less efficient than the allocation of multiple small objects, if the object is relatively large, split can be efficient , the specific reasons are as follows:

The memory occupied by Java objects is primarily allocated from the heap, and the heap is shared by all threads, so the allocation of memory on the heap requires locking, which results in a large cost of creating objects. When there is insufficient space on the heap, the GC is triggered, and if the space behind the GC is still insufficient, a outofmemory error message is thrown. In order to increase the efficiency of memory allocation, Sun JDK allocates a separate space for each newly created thread on the new generation of Eden Space, called Tlab (thread Local Allocation Buffer). The size is calculated by the JVM based on the operating conditions, and the-xx:tlabwastetargetpercent can be used to set the percentage of Eden space that the Tlab can occupy, with a default value of 1%. The JVM assigns the appropriate size of tlab space to each thread based on the ratio, the number of threads, and whether the thread allocates objects frequently. There is no need to lock when allocating memory on Tlab, so the JVM allocates memory to objects in the thread as much as possible on Tlab, and if the object is too large or tlab space is exhausted, it is still allocated on the heap, so when writing a Java program, it is usually Multiple small objects are more efficient to allocate than larger objects .

Http://hi.baidu.com/i1see1you/blog/item/6438aecac4dd959bc917683d.html

Processing of Java Web Server CPU consumption too high

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.