[Java] High CPU usage due to HASHMAP

Source: Internet
Author: User

Today, a problem in the production environment, the CPU usage of Java applications is very high, resulting in CPU utilization of the entire machine up to 90%, under normal circumstances is about 20%.

Export the thread dump and use the IBM Thread Analyzer for Java tool for analysis. There are more than 60 online threads, of which 15 threads are executing the same code in the same file, and the topmost call is Hashmap.get ().

The underlying data structure of HASHMAP is the storage of array + linked list, which is used to deal with the situation of hash collisions. Under normal circumstances the link is a linear list, when the data structure is contaminated in the concurrency situation, resulting in a ring linked list, it will lead to the infinite loop of the program.

It is unusual for 15 threads to execute the same line of code at the same time. When a thread calls Hashmap.get, when it searches on a linked list, it happens to encounter a ring link that can explain the anomaly.

About HASHMAP non-thread-safe, in the case of multi-threaded concurrent access, the introduction of ring linked list reference:

A Beautiful Race Condition

Explain the timing causing hashmap.put () to execute an infinite loop

Attached: About HashMap source Reading notes

Map/hashmap-Source Code Learning notes

[Java] High CPU usage due to HASHMAP

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.