High CPU in java.util.regex.Matcher
Environment
- JBoss Enterprise Application Platform (EAP)
Issue
- High CPU is seen in threads making calls to java.util.regex.Matcher:
"http-0.0.0.0-8080-1" daemon prio=10 tid=0x00000000499c9000 nid=0x1672 runnable [0x000000006880a000]
java.lang.Thread.State: RUNNABLE
...
at java.util.regex.Pattern$Curly.match0(Pattern.java:3782)
at java.util.regex.Pattern$Curly.match(Pattern.java:3744)
at java.util.regex.Pattern$Ques.match(Pattern.java:3691)
at java.util.regex.Pattern$GroupHead.match(Pattern.java:4168)
at java.util.regex.Pattern$Loop.match(Pattern.java:4295)
at java.util.regex.Pattern$GroupTail.match(Pattern.java:4227)
at java.util.regex.Pattern$Curly.match0(Pattern.java:3782)
at java.util.regex.Pattern$Curly.match(Pattern.java:3744)
at java.util.regex.Pattern$Ques.match(Pattern.java:3691)
at java.util.regex.Pattern$GroupHead.match(Pattern.java:4168)
at java.util.regex.Pattern$Loop.matchInit(Pattern.java:4314)
at java.util.regex.Pattern$Prolog.match(Pattern.java:4251)
at java.util.regex.Pattern$Curly.match0(Pattern.java:3782)
at java.util.regex.Pattern$Curly.match(Pattern.java:3744)
at java.util.regex.Pattern$Begin.match(Pattern.java:3120)
at java.util.regex.Matcher.match(Matcher.java:1127)
at java.util.regex.Matcher.matches(Matcher.java:502)
Resolution
- java.util.regex.Matcher is not a thread safe object so ensure each thread uses its own instance of the Matcher
- If sharing a single Matcher across multiple threads, ensure access to it is synchronized and call Matcher.reset() in between uses
- Improve efficiency of the regex patterns used
Root Cause
- Concurrent access to a single instance of java.util.regex.Matcher
- Weighty, inefficient regex patterns used
Diagnostic Steps
- Troubleshoot with CPU data and other diagnostic steps as described in Java application high CPU
- If unsure of the regex involved in these high CPU calls, capture a heap dump as well. Then after identifying the high CPU thread, inspect the underlying Pattern and Matchers in use by this thread in the heap dump to identify any problematic regex.
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.