如何通过产生线程来击打java.lang.OutOfMemoryError? [英] How to hit java.lang.OutOfMemoryError by spawning threads?
本文介绍了如何通过产生线程来击打java.lang.OutOfMemoryError?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我遇到了这个博客网站,作者在机器抛出java.lang.OutOfMemoryError之前针对最大线程数进行测试.但是,在下面的测试代码中,尽管产生了任意大线程,但我还是无法解决该错误.
I came across this blog site where the author is testing against the maximum number of threads before the machine throws a java.lang.OutOfMemoryError. However, in my below test codes, i am unable to hit the error despite the arbitrary large threads spawned.
for (int i = 0; i < 1000000; i++) {
Thread thread = new Thread(new Car());
thread.setName(Integer.toString(i));
thread.start();
}
推荐答案
Try sleeping inside the thread, otherwise it might end up too quickly and get garbage collected, as shown in the example code:
Thread t = new Thread(new Runnable() {
@Override
public void run() {
try {
while (!Thread.interrupted()) {
Thread.sleep(1000);
}
} catch (InterruptedException ignored) {
//
}
}
});
这篇关于如何通过产生线程来击打java.lang.OutOfMemoryError?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文