High CPU due to multiple Java threads accessing HashMap simultaneously
Environment
- Java
- JBoss Enterprise Application Platform (EAP)
Issue
- High cpu and threads consuming the CPU have HashMap access at the top of the stack trace. For example:
"RMI TCP Connection(4468)-160.57.110.76" daemon prio=6 tid=0x5a436400 nid=0x9e4 runnable [0x6068d000]
java.lang.Thread.State: RUNNABLE
at java.util.HashMap.get(HashMap.java:303)
at com.example.service.getWidget(Component.java:796)
...
"WorkManager(3)-122" daemon prio=6 tid=0x5b496000 nid=0x18fc runnable [0x70b2e000]
java.lang.Thread.State: RUNNABLE
at java.util.HashMap.get(HashMap.java:303)
at com.example.service.getWidget(Component.java:796)
...
Resolution
- Replace HashMap with ConcurrentHashMap.
Root Cause
- HashMap is not a thread-safe structure.
- Accessing a HashMap from multiple threads simultaneously can result in an infinite loop, causing high cpu. See Content from bugs.sun.com is not included.Content from bugs.sun.com is not included.http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6423457. for more.
- Users of JDK 1.6 should ensure to upgrade to u33 or higher, due to this bug Content from bugs.java.com is not included.JDK-7027300 : Unsynchronized HashMap access causes endless loop and related backports.
- High CPU due to Richfaces HashMap.get() infinite loop
- High CPU in concurrent access to the JSSESupport keySizeCache map
- High cpu in beanutils WeakHashMap
- High CPU due to unsafe HashMap access in TomcatInjectionContainer.getEncInjectionsForClass
- High CPU in org.apache.axis.utils.JavaUtils.isEnumClass
- High CPU due to SunLayoutEngine.getEngine HashMap loops
- High CPU due to unsafe HashMap access in org.apache.felix.framework.util.WeakZipFileFactory$WeakZipFile
- Threads looping in WeakHashMap.get called from com.sun.facelets.tag.MetaRulesetImpl.getMetadataTarget on JBoss
Diagnostic Steps
Components
Category
This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.