问题: javaagent打入到应用后,发现采集到的数据发不出去了,进一步调试,发现数据处理线程停止了,什么原因导致线程停止了呢?
原因是: 由于测试demo每次访问都递归1000多次,处理的时候导致堆栈溢出了。
示例如下:
import java.util.HashMap;
public class StackTest implements Runnable {
private int i = 0;
private int end = 100000;
public static void main(String[] args) {
new Thread(new StackTest()).start();
}
@Override
public void run() {
while(true) {
try {
i =0;
digui();
Thread.sleep(2);
} catch (Exception e) {
e.printStackTrace();
}
}
}
public void digui() throws InterruptedException {
System.out.println(i);
i++;
if(i>end) {
Thread.sleep(3000);
return;
}
digui();
}
}
Exception in thread "Thread-0" java.lang.*Error
at java.lang.String.<init>(String.java:599)
at java.lang.Integer.toString(Integer.java:333)
at java.lang.String.valueOf(String.java:2954)
at java.io.PrintStream.print(PrintStream.java:597)
at java.io.PrintStream.println(PrintStream.java:736)
at com.cloudwise.agent.core.test.StackTest.digui(StackTest.java:27)
at com.cloudwise.agent.core.test.StackTest.digui(StackTest.java:33)
at com.cloudwise.agent.core.test.StackTest.digui(StackTest.java:33)
如何解决上面的问题呢: 为jvm添加-Xss参数:
注意: 结合如下demo查看,是由于递归引起的问题:
public class StackTest2 implements Runnable {
private int i = 0;
private int end = 100000;
public static void main(String[] args) {
new Thread(new StackTest2()).start();
}
@Override
public void run() {
while(true) {
try {
i =0;
for(int s=0;s<=end;s++) {
forEach();
// Thread.sleep(2);
}
Thread.sleep(3000);
} catch (Exception e) {
e.printStackTrace();
}
}
}
public void forEach() throws InterruptedException {
System.out.println(i);
i++;
}
}