English 中文(简体)
创建 < 100 线索时 Java'out of memoryError'的 Java'out of memoryError '
原标题:Java `OutOfMemoryError` when creating < 100 threads

由于这个错误,我一直在阅读和测试 和敲我的头 在墙上超过一天的时间。

我在一个叫做 lististener 的类中有 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) 和 -Xs (从104k到512M) 。 服务器有24GB 内存, 但也必须运行支持程序的数据库 。

创建 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)

当这种情况发生时,系统中总是有大量的免费记忆,而其他程序继续顺利执行。 是什么使得爪哇认为它不再有新线索的记忆?

UPDATE: Perhaps this is bigger than I thought- I managed to get this error (only one time) when I used ^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

当我试图杀死客户端(也写在爪哇,运行在同一服务器上,它是一个单线,读出一个文件,然后通过套接头将其发送到服务器)时,也发生了同样的情况,因此,绝对有一个超出JVM的限度,使一个人干扰另一个,但我无法想象,如果我仍然拥有自由的内存,而根本不使用交换机呢?服务器 - Xmx1G - Xss104k 客户端 - Xmx10M

UPDATE2: Abandoning the perl Forks::Super library and running the clients from bash let me get up to 34 threads before the server crashed with OOME, so running multiple clients definitely had an impact on the server, but at the same time I should still be able to run more than 34 (68 if one counts the clients) java threads at a time. Which system resources are blocking the creation of more threads (i.e. where should I look to find the hog)? When everything (clients, server, GC...) runs out of memory at the same time, top says this about my CPU and memory usage:

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 cached

<强度> UPDATE3: 下面的 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
最佳回答

您可以通过 max 用户进程 来限制您的使用限制 :

ulimit -u

要更改限制 :

/etc/security/rmits.conf 集: /etc/security/rmits.conf 集:

user soft nproc [your_val] 
user hard nproc [your_val]

您可能需要添加一些其他配置, 如果它还不够的话, 您可能要看到此 < a href=" http:// directory. fedoraproject. org/ wiki/ Performance_ Tuning" rel= " noreferrer" >link 。

注:OP在 fedora 和 centos 中发现了 < a href=> "https://buggzilla.redhat.com/show_bug.cgi?id=823030" rel= "noreferr" > bug报告 ,解释了编辑 /etc/security/remits.conf 的局限性。

问题回答

您的问题可能与 JVM 无法分配新线条的堆叠内存有关。 具有讽刺意味的是, 这个问题可以通过减少堆积空间(- Xmx) 和堆积空间(- Xss) 来解决。 例如, 请在这里查询一个好的解释 : < a href=" http://www.blogsoncloud. com/jsp/techSols/java- lang- Out- memoryError- Out- Out- memoryError- unable- to- intern- new- native- thread.jsp" rel=“ no follow” > http://www. blogsoncloud. com/jsp/techSols/java- outof- MemoryError- unable-to- crevenation- new- nat-thread. sp/ a >

It s not missing memory for your new threads, it s missing actual threads. The system is probably stopping you: there s a limit to the number of thread a user can create. You can query it that way:

cat /proc/sys/kernel/threads-max

Note that you might be impacted by other processes on the same machine, you they create many thread too. You might find this question useful: Maximum number of threads per process in Linux?

澄清一下:

您向 < code> Thread 提供 < code> ServerSocket 。 您是否向该 Socket 发送数据? 您是否存储数据到该 Socket 中的大部分数据? Tak a look for a standle, 您在其中将流数据存储到 < code> byte [] 中 。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签