小编kaz*_*kaz的帖子

创建<100个线程时的Java`OutOfMemoryError`

由于这个错误,我一直在阅读和测试并在墙上撞了一天多一天.

我在类中有一些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)

java openjdk jvm out-of-memory threadpool

11
推荐指数
1
解决办法
2万
查看次数

标签 统计

java ×1

jvm ×1

openjdk ×1

out-of-memory ×1

threadpool ×1