java 正则泄漏 死循环_循环使用正则导致进程挂起和CPU使用率达到100%居高不下测试Demo...

搜索热词

循环使用正则导致进程挂起和cpu使用率达到100%居高不下测试Demo

以下代码为测试代码

import java.io.BufferedReader;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.io.IOException;

import java.util.HashMap;

import java.util.Map;

import java.util.regex.Matcher;

import java.util.regex.Pattern;

public class ErrorDataStatTest {

public static Map errorMap = new HashMap();

public static void main(String[] args) {

String dest = "/Users/kevin/Documents/shop.do1";

try {

new ErrorDataStatTest().statData(new File(dest));

} catch (IOException e) {

e.printStackTrace();

}

for (Map.Entry entry : errorMap.entrySet()) {

System.out.println(entry.getKey() + "\t" + entry.getValue().val + "\t" + entry.getValue().num);

}

}

public static class EntryBean {

String key;

String val;

int num = 0;

public EntryBean(String key,String val) {

this.key = key;

this.val = val;

}

public void add() {

this.num++;

}

}

public static final String regex_code = ".*?code.*?=.*?\"(-?[\\d]*)\"";

public static final Pattern pattern = Pattern.compile(regex_code);

public static final String regex_code_1 = ".*?Code\\s?=\\s?(-?[\\d]*).*?";

public static final Pattern pattern_1 = Pattern.compile(regex_code_1);

//解析错误Code

private static String parserErrorCode(String error) {

Matcher matcher = pattern.matcher(error);

if (matcher.find()) {

return matcher.group(1);

}

Matcher matcher1 = pattern_1.matcher(error);

if (matcher1.find()) {

return matcher1.group(1);

}

return null;

}

public static final String regex_desc = ".*?description.*?=.*?\"(.*?)\"";

public static final Pattern pattern_desc = Pattern.compile(regex_desc);

private static final String regex_desc_1 = ".*?Code\\s?=\\s?[-]?[\\d]*\\s*\"(.*?)\".*?";

private static final Pattern pattern_desc_1 = Pattern.compile(regex_desc_1);

//解析错误描述

private static String parserErrorDesc(String error) {

Matcher matcher = pattern_desc.matcher(error);

if (matcher.find()) {

return matcher.group(1);

}

Matcher matcher1 = pattern_desc_1.matcher(error);

if (matcher1.find()) {

return matcher1.group(1);

}

return null;

}

private String process(String str) {

if (str != null) {

String key = parserErrorCode(str),val = parserErrorDesc(str);

EntryBean entryBean = errorMap.get(key);

if (entryBean == null) {

entryBean = new EntryBean(key,val);

} else {

entryBean.add();

}

errorMap.put(key,entryBean);

}

return str;

}

/*统计日志文件:按照错误码分类统计汇总*/

protected void statData(File srcFile) throws IOException {

FileReader reader = new FileReader(srcFile);

BufferedReader bReader = new BufferedReader(reader);

try {

String line = process(bReader.readLine());

int num = 0;

while (line != null) {

line = process(bReader.readLine());

System.out.println(num);

num++;

}

} catch (FileNotFoundException ex) {

throw ex;

} finally {

bReader.close();

bReader = null;

reader.close();

reader = null;

}

}

}

本地测试了几次都是在循环处理到第11497次的时候程序卡住不动,导致进程挂起,cpu消耗在100%左右一直下不来。

localhost:~ kevin$ jstack 675

2014-03-22 11:10:16

Full thread dump Java HotSpot(TM) 64-Bit Server VM (24.45-b08 mixed mode):

"Attach Listener" daemon prio=5 tid=0x00007f96638e9000 nid=0x3c0b waiting on condition [0x0000000000000000]

java.lang.Thread.State: RUNNABLE

"Service Thread" daemon prio=5 tid=0x00007f966301e000 nid=0x4b03 runnable [0x0000000000000000]

java.lang.Thread.State: RUNNABLE

"C2 CompilerThread1" daemon prio=5 tid=0x00007f966301d000 nid=0x4903 waiting on condition [0x0000000000000000]

java.lang.Thread.State: RUNNABLE

"C2 CompilerThread0" daemon prio=5 tid=0x00007f966300d000 nid=0x4703 waiting on condition [0x0000000000000000]

java.lang.Thread.State: RUNNABLE

"Signal Dispatcher" daemon prio=5 tid=0x00007f9663014000 nid=0x4503 runnable [0x0000000000000000]

java.lang.Thread.State: RUNNABLE

"Finalizer" daemon prio=5 tid=0x00007f9664827000 nid=0x3103 in Object.wait() [0x000000011108c000]

java.lang.Thread.State: WAITING (on object monitor)

at java.lang.Object.wait(Native Method)

- waiting on <0x00000007eaa85568> (a java.lang.ref.ReferenceQueue$Lock)

at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:135)

- locked <0x00000007eaa85568> (a java.lang.ref.ReferenceQueue$Lock)

at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:151)

at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:189)

"Reference Handler" daemon prio=5 tid=0x00007f9664029000 nid=0x2f03 in Object.wait() [0x0000000110f89000]

java.lang.Thread.State: WAITING (on object monitor)

at java.lang.Object.wait(Native Method)

- waiting on <0x00000007eaa850f0> (a java.lang.ref.Reference$Lock)

at java.lang.Object.wait(Object.java:503)

at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:133)

- locked <0x00000007eaa850f0> (a java.lang.ref.Reference$Lock)

"main" prio=5 tid=0x00007f9664800000 nid=0x1903 runnable [0x0000000109753000]

java.lang.Thread.State: RUNNABLE

at java.util.regex.Pattern$Curly.match1(Pattern.java:4198)

at java.util.regex.Pattern$Curly.match(Pattern.java:4134)

at java.util.regex.Pattern$Start.match(Pattern.java:3408)

at java.util.regex.Matcher.search(Matcher.java:1199)

at java.util.regex.Matcher.find(Matcher.java:592)

at com.ishopping.sign.controller.ErrorDataStatTest.parserErrorDesc(ErrorDataStatTest.java:72)

at com.ishopping.sign.controller.ErrorDataStatTest.process(ErrorDataStatTest.java:84)

at com.ishopping.sign.controller.ErrorDataStatTest.statData(ErrorDataStatTest.java:140)

at com.ishopping.sign.controller.ErrorDataStatTest.main(ErrorDataStatTest.java:24)

"VM Thread" prio=5 tid=0x00007f9664028800 nid=0x2d03 runnable

"GC task thread#0 (ParallelGC)" prio=5 tid=0x00007f966300f800 nid=0x2503 runnable

"GC task thread#1 (ParallelGC)" prio=5 tid=0x00007f9663010800 nid=0x2703 runnable

"GC task thread#2 (ParallelGC)" prio=5 tid=0x00007f9663011000 nid=0x2903 runnable

"GC task thread#3 (ParallelGC)" prio=5 tid=0x00007f9663011800 nid=0x2b03 runnable

"VM Periodic Task Thread" prio=5 tid=0x00007f9663019800 nid=0x4d03 waiting on condition

JNI global references: 181

ORACLE:

#总结

建议在高频度执行的代码或高并发的系统中尽量少的使用正则。

相关文章

总结

如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。

如您喜欢交流学习经验,点击链接加入交流1群:1065694478(已满)交流2群:163560250

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值