java.lang.OutOfMemoryError: unable to create new native thread
這個異常一般由于兩個原因?qū)е碌模?/strong>
1)內(nèi)存空間不足以滿足創(chuàng)建線程所需的stack size
??virtual memory < stack size*the number of threads
2)線程數(shù)已達到操作系統(tǒng)的上限
??線程 vs 進程
查看jvm stack size 相關(guān)參數(shù):
java -XX:+PrintFlagsFinal -version | grep ThreadStackSize
intx CompilerThreadStackSize = 0 {pd product}
intx ThreadStackSize = 1024 {pd product}
intx VMThreadStackSize = 1024 {pd product}
操作系統(tǒng)進程相關(guān)參數(shù):
進程可用最大虛擬內(nèi)存
ulimit -v
unlimited
最大棧大小
ulimit -s
8192
每個用戶可創(chuàng)建最大進程數(shù)
ulimit -u
510226
系統(tǒng)支持的最大進程數(shù)
cat /proc/sys/kernel/pid_max
32768
共享內(nèi)存最大線程數(shù)
cat /proc/sys/kernel/threads-max
1020452
獲取進程id
ps -ef | grep <key word>
查詢當前某程序的線程或進程數(shù)
pstree -p <pid> | wc -l
查詢當前系統(tǒng)已用的線程或進程數(shù)
pstree -p | wc -l
打印thread dump
由于已經(jīng)不能創(chuàng)建新的線程何吝,所以使用jstack打印thread dump時會報錯,可以使用kill -3 <pid>
# There is insufficient memory for the Java Runtime Environment to continue.
# Cannot create GC thread. Out of system resources.
# An error report file with more information is saved as:
# ******.log
分析thread dump
可以直接看thread dump烂斋,也可以借助圖形界面工具盗蟆,更清晰方便些,比如:
http://fastthread.io/
解決問題
下面鏈接給出了幾種不同原因?qū)е庐惓?yīng)的解決方法半开,但方法二中只講了ulimit -u,實際上面提到的幾個進程相關(guān)的限制參數(shù)哆致,都會影響進程的數(shù)量涩维。
https://blog.fastthread.io/2016/07/06/troubleshoot-outofmemoryerror-unable-to-create-new-native-thread/