Java Memory error: unable to create new native thread(Java 内存错误:无法创建新的本机线程)
问题描述
运行 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)
我的问题:我在互联网上读到我的程序应该处理大约 5000 个线程.那么这是怎么回事,请问如何解决?
这是我打开 shell 时 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 和这些东西.工作量太大了.
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 内存错误:无法创建新的本机线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Java 内存错误:无法创建新的本机线程
- 转换 ldap 日期 2022-01-01
- 未找到/usr/local/lib 中的库 2022-01-01
- java.lang.IllegalStateException:Bean 名称“类别"的 BindingResult 和普通目标对象都不能用作请求属性 2022-01-01
- Eclipse 的最佳 XML 编辑器 2022-01-01
- GC_FOR_ALLOC 是否更“严重"?在调查内存使用情况时? 2022-01-01
- 如何指定 CORS 的响应标头? 2022-01-01
- 如何使 JFrame 背景和 JPanel 透明且仅显示图像 2022-01-01
- 将 Java Swing 桌面应用程序国际化的最佳实践是什么? 2022-01-01
- 获取数字的最后一位 2022-01-01
- 在 Java 中,如何将 String 转换为 char 或将 char 转换 2022-01-01