这篇文章记录一个正则表达是导致 CPU 高的问题排查。由于无法直接使用线上的代码测试,所以我自己把代码整理了下来,具体代码如下:
1 2 3 4 5 6 7 8 9 10 11 12
| public class AppMain { public static void main(String[] args) throws InterruptedException { final String regex="^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$"; final String email="blog.laofu.online.fuweilao@vip.qq.com#"; for (int i = 0; i < 1000000; i++) { Matcher matcher = RegexUtils.matcher(regex, email); matcher.find(); Thread.sleep(10);
} } }
|
当运行程序的时候,我们可以看到 java 的进程占用了 CPU 了 82.1%
,由于我使用的服务器是 1核+2G, 所以 load avg 占用也很高。