91名师指路-头部
91名师指路

OutOfMemoryError系列(五):Java.lang.OutOfMemeoryError:unable to create new native thread

由于某些原因,现在不支持支付宝支付,如需要购买源码请加博主微信进行购买,微信号:13248254750

一:出现 java.lang.OutOfMemoryError: unable to create new native thread的原因:

1 创建了太多的线程,一个应用进程创建多个线程,超过了系统承载极限。

2 你的服务器不允许你的应用程序创建这么多线程,linux系统默认运行单个进程可以创建的线程数是1024个,你的应用创建超过这个数量就会报 java.lang.OutOfMemoryError: unable to create new native thread

二:解决办法:

1 想办法降低你应用程序创建线程的数量,分析应用是否真的需要创建这么多线程,如果不是,改代码将线程数降到最低。

2 对于有的应用,确实需要创建很多线程,远超过linux系统的默认1024个线程的限制,可以通过修改linux服务器配置,扩大默认限制。


三:代码演示。

package com.mszl.oom;

/**
* 功能:
* 备注:更多资料请访问 http://www.91mszl.com
* @author bobo teacher
*/
public class NativeThreadDemo {

public static void main(String[] args) {
for(int i=0; ; i++){
System.out.println(" >>>>>>>>>>>> " + i);

new Thread(() -> {
try {
Thread.sleep(Integer.MAX_VALUE);
} catch (InterruptedException e) {
e.printStackTrace();
}
},""+i).start();
}
}


}

执行结果:

Exception in thread "main" java.lang.OutOfMemoryError: unable to create new native thread
at java.lang.Thread.start0(Native Method)
at java.lang.Thread.start(Thread.java:717)
at com.mszl.oom.NativeThreadDemo.main(NativeThreadDemo.java:20)


2019-12-13 19:21:24     阅读(1165)

名师出品,必属精品    https://www.91mszl.com

联系博主    
用户登录遮罩层
x

账号登录

91名师指路-底部