由于这个错误,我一直在阅读和测试并在墙上撞了一天多一天.
我在类中有一些Java代码Listener,看起来像这样
ExecutorService executor = Executors.newFixedThreadPool(NTHREADS);
boolean listening = true;
int count = 0;
while (listening) {
Runnable worker;
try {
worker = new ServerThread(serverSocket.accept()); // this is line 254
executor.execute(worker);
count++;
logger.info("{} threads started", count);
} catch (Exception e1){
//...
}
}
Run Code Online (Sandbox Code Playgroud)
我一直在调整JVM设置-Xmx(从1到15G -Xss不等)和(从104k到512M).服务器有24 GB的RAM,但还必须运行支持该程序的数据库.
在创建了2-20个线程之后(程序中的其他地方也存在了几十个),我得到了错误
Exception in thread "Thread-0" java.lang.OutOfMemoryError: unable to create new native thread
at java.lang.Thread.start0(Native Method)
at java.lang.Thread.start(Thread.java:657)
at java.util.concurrent.ThreadPoolExecutor.addWorker(ThreadPoolExecutor.java:943)
at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1325)
at xxx.Listener.run(Listener.java:254)
Run Code Online (Sandbox Code Playgroud)
$java -version 收益率:
java version "1.6.0_24" …Run Code Online (Sandbox Code Playgroud)