Java内存错误:无法创建新的本机线程 [英] Java Memory error: unable to create new native thread
问题描述
运行Java服务器时,在UNIX服务器上出现此错误:
I get this error on my UNIX server, when running my java server:
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:640)
at [... where ever I launch a new Thread ...]
我有大约600个线程正在运行。
It happens everytime I have about 600 threads running.
我已经在服务器上设置了此变量:
I have set up this variable on the server:
$> ulimit -s 128
该命令的结果对我来说很奇怪,该命令是在该错误上次发生:
What looks strange to me is the result of this command, which I ran when the bug occured the last time:
$> free -m
total used free shared buffers cached
Mem: 2048 338 1709 0 0 0
-/+ buffers/cache: 338 1709
Swap: 0 0 0
我像这样启动我的Java服务器:
I launch my java server like this:
$> /usr/bin/java -server -Xss128k -Xmx500m -jar /path/to/myJar.jar
我的Debian版本:
My debian version:
$> cat /etc/debian_version
5.0.8
我的Java版本:
$> java -version
java version "1.6.0_26"
Java(TM) SE Runtime Environment (build 1.6.0_26-b03)
Java HotSpot(TM) 64-Bit Server VM (build 20.1-b02, mixed mode)
我的问题:我已经在Internet上阅读了我的程序应该处理大约5000个线程之类的东西。那么发生了什么事,以及如何解决?
编辑:这是 ulimit -a
当我打开外壳程序时:
this is the output of ulimit -a
when I open a shell:
core file size (blocks, -c) unlimited
data seg size (kbytes, -d) unlimited
scheduling priority (-e) 0
file size (blocks, -f) unlimited
pending signals (-i) 794624
max locked memory (kbytes, -l) 32
max memory size (kbytes, -m) unlimited
open files (-n) 100000
pipe size (512 bytes, -p) 8
POSIX message queues (bytes, -q) 819200
real-time priority (-r) 0
stack size (kbytes, -s) 10240
cpu time (seconds, -t) unlimited
max user processes (-u) 794624
virtual memory (kbytes, -v) unlimited
file locks (-x) unlimited
我从init.d将脚本作为守护程序运行,而这就是我的运行方式:
I run the script as a daemon from init.d, and this is what i run:
DAEMON=/usr/bin/java
DAEMON_ARGS="-server -Xss128k -Xmx1024m -jar /path/to/myJar.jar"
ulimit -s 128 && ulimit -n 10240 && start-stop-daemon -b --start --quiet --chuid $USER -m -p $PIDFILE --exec $DAEMON -- $DAEMON_ARGS \
|| return 2
Edit2:我遇到过这个堆栈Java测试线程的问题: how-many-threads-can -a-java-vm-support
public class DieLikeADog {
private static Object s = new Object();
private static int count = 0;
public static void main(String[] argv){
for(;;){
new Thread(new Runnable(){
public void run(){
synchronized(s){
count += 1;
System.err.println("New thread #"+count);
}
for(;;){
try {
Thread.sleep(100);
} catch (Exception e){
System.err.println(e);
}
}
}
}).start();
}
}
}
在我的服务器上,该程序613个线程后崩溃。现在,我确定这是不正常的,仅与我的服务器配置有关。有人可以帮忙吗?
On my server, the program crashes after 613 threads. Now i'm certain this is not normal, and only related to my server configuration. Can anyone help please ?
编辑3:
我遇到过这篇文章,还有很多其他人在解释 Linux无法创建1000个线程,但是您伙计们告诉我,您可以在系统上做到这一点。我不明白。
Edit 3: I have come across this article, and many others, explaining that linux can't create 1000 threads, but you guys are telling me that you can do it on your systems. I don't understand.
我还在服务器上运行了此脚本: threads_limits.c ,该限制大约为620个线程。
I have also ran this script on my server: threads_limits.c and the limit is around 620 threads.
我的网站现在处于离线状态,是我的项目可能发生的最糟糕的事情。
我不知道如何重新编译glibc和这些东西。 imo太辛苦了。
My website is now offline and this is the worst thing that could have happened to my project. I don't know how to recompile glibc and this stuff. It's too much work imo.
我想我应该切换到Windows服务器。因为此页面上建议的所有设置均未做出任何更改:我的系统的限制是600到620个线程,无论所涉及的程序是什么。
I guess I should switch to windows server. Because none of the settings proposed on this page did make any change: The limit on my system is between 600 and 620 threads, no matter the program involved.
推荐答案
仅获得以下信息:这是我的主机提供商强加的限制。这与编程或linux无关。
Just got the following information: This is a limitation imposed by my host provider. This has nothing to do with programming, or linux.
这篇关于Java内存错误:无法创建新的本机线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!