java 如何创建100个线程_创建<100个线程时的Java`OutOfMemoryError`

由于这个错误,我一直在阅读和测试,并将我的头撞在墙上一整天。

我在一个名为Listener的类中有一些Java代码,看起来像这样

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){

//...

}

}我一直在调整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)$java -version产生:

java version "1.6.0_24"

OpenJDK Runtime Environment (IcedTea6 1.11.1) (fedora-65.1.11.1.fc16-x86_64)

OpenJDK 64-Bit Server VM (build 20.0-b12, mixed mode)发生这种情况时,系统上总是会有大量的可用内存,而其他程序仍能继续正常运行。什么导致Java认为它没有更多的新线程内存?

更新:

也许这比我想象的要大 - 当我使用^C时,我设法得到这个错误(只有一次):

OpenJDK 64-Bit Server VM warning: Exception java.lang.OutOfMemoryError occurred dispatching signal SIGINT to handler- the VM may need to be forcibly terminated当我试图杀死客户端时也发生过同样的事情(同样用Java编写,运行在同一台服务器上,它是一个读取文件并通过套接字发送给服务器的单个线程),所以肯定有一个限制JVM导致其中一个与另一个干扰,但我无法想象如果我仍然有空闲内存并且根本不使用swap?服务器-Xmx1G -Xss104k客户端-Xmx10M

UPDATE2:

放弃perl Forks::Super库并从bash运行客户端,让我在服务器与OOME崩溃之前得到多达34个线程,因此运行多个客户端肯定会对服务器产生影响,但同时我仍应该能够运行更多比34(68如果一个计数客户端)一次java线程。哪些系统资源阻止创建更多线程(即,我应该在哪里寻找猪)?当所有内容(客户端,服务器,GC ...)同时耗尽内存时,top会说明我的CPU和内存使用情况:

Cpu(s): 0.0%us, 0.0%sy, 0.0%ni,100.0%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st

Mem: 24681040k total, 1029420k used, 23651620k free, 30648k buffers

Swap: 26836988k total, 0k used, 26836988k free, 453620k cachedUPDATE3:下面的hs_error日志是否表明我的java不是64位?

# There is insufficient memory for the Java Runtime Environment to continue.

# Cannot create GC thread. Out of system resources.

# Possible reasons:

# The system is out of physical RAM or swap space

# In 32 bit mode, the process size limit was hit

# Possible solutions:

# Reduce memory load on the system

# Increase physical memory or swap space

# Check if swap backing store is full

# Use 64 bit Java on a 64 bit OS

# Decrease Java heap size (-Xmx/-Xms)

# Decrease number of Java threads

# Decrease Java thread stack sizes (-Xss)

# Set larger code cache with -XX:ReservedCodeCacheSize=

# This output file may be truncated or incomplete.

#

# JRE version: 6.0_24-b24

# Java VM: OpenJDK 64-Bit Server VM (20.0-b12 mixed mode linux-amd64 compressed oops)

# Derivative: IcedTea6 1.11.1

# Distribution: Fedora release 16 (Verne), package fedora-65.1.11.1.fc16-x86_64

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值