日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

使用ThreadPoolExecutor产生的 OutOfMemoryError: unable to create new native thread 错误

發(fā)布時(shí)間:2024/1/23 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 使用ThreadPoolExecutor产生的 OutOfMemoryError: unable to create new native thread 错误 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

轉(zhuǎn)載請(qǐng)注明出處:http://www.codelast.com/

最近,在使用Java的ThreadPoolExecutor來(lái)實(shí)現(xiàn)一個(gè)并發(fā)功能的時(shí)候,發(fā)現(xiàn)程序剛執(zhí)行起來(lái)不久,就提示了錯(cuò)誤:

1

OutOfMemoryError: unable to create new native thread

并且服務(wù)器立即陷入類(lèi)似于“無(wú)響應(yīng)”的狀態(tài),無(wú)法用Ctrl+C結(jié)束掉我的Java程序,按Ctrl+C的時(shí)候,命令行只是不斷地打印出類(lèi)似于下面的消息:

1

Exception java.lang.OutOfMemoryError occurred dispatching signal SIGINT to handler- the VM may need to be forcibly terminated


在其他已經(jīng)連上的terminal下,我想嘗試用 ps -ef | grep xxx 來(lái)找出進(jìn)程的pid并kill掉它,也無(wú)果,因?yàn)橹灰斖阷s命令,再一回車(chē),馬上就提示錯(cuò)誤(大概意思就是資源不足),于是在不重啟系統(tǒng)的情況下,只能靜靜等待,直到Linux系統(tǒng)恢復(fù)了響應(yīng),就可以用ps -ef查出其pid并kill掉它了。
文章來(lái)源:http://www.codelast.com/
但是為嘛會(huì)出現(xiàn)這樣的問(wèn)題?
首先,服務(wù)器內(nèi)存是非常充足的,不應(yīng)該是內(nèi)存真的不夠用;其次,我仔細(xì)檢查了一遍我的代碼,也并無(wú)異常之處;再次,我的程序也不會(huì)占用非常大的內(nèi)存。
于是我問(wèn)了一下Google,找到了原因所在:我程序中設(shè)置的ThreadPoolExecutor并發(fā)數(shù)超出了系統(tǒng)限制。這個(gè)限制,就是所謂的“max user processes”限制,可以用如下命令查看Linux系統(tǒng)的設(shè)置值:

1

ulimit -a

其輸出類(lèi)似于:

01

02

03

04

05

06

07

08

09

10

11

12

13

14

15

16

core file size????????? (blocks, -c) 0

data seg size?????????? (kbytes, -d) unlimited

scheduling priority???????????? (-e) 0

file size?????????????? (blocks, -f) unlimited

pending signals???????????????? (-i) 385903

max locked memory?????? (kbytes, -l) 64

max memory size???????? (kbytes, -m) unlimited

open files????????????????????? (-n) 65535

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) 1024

virtual memory????????? (kbytes, -v) unlimited

file locks????????????????????? (-x) unlimited

看到“max user processes”那一行了嗎?1024就是我當(dāng)前用戶(hù)的最大允許值,當(dāng)超過(guò)這個(gè)值的時(shí)候,就無(wú)法創(chuàng)建新的process了。
文章來(lái)源:http://www.codelast.com/
對(duì)應(yīng)到使用ThreadPoolExecutor的代碼,當(dāng)我們創(chuàng)建一個(gè)對(duì)象的時(shí)候,會(huì)使用如下的構(gòu)造函數(shù):

1

2

3

4

5

public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize,

??????????????????????????????long keepAliveTime, TimeUnit unit,

??????????????????????????????BlockingQueue<Runnable> workQueue) {

????//...

}

其中,第1和第2個(gè)參數(shù),就是線程池里線程數(shù)的大小:

@param corePoolSize the number of threads to keep in the pool, even if they are idle.

@param maximumPoolSize the maximum number of threads to allow in the pool.

雖然我只把這兩個(gè)數(shù)值分別設(shè)置成了400和500,但是由于我的用戶(hù)下還運(yùn)行有其他很多程序,因此,它們加在一起占用的線程數(shù)超過(guò)了1024的時(shí)候,就會(huì)出現(xiàn)前面的錯(cuò)誤。
文章來(lái)源:http://www.codelast.com/
要修改Linux系統(tǒng)中的這個(gè)限制,可以修改?/etc/security/limits.d/90-nproc.conf?文件,其內(nèi)容類(lèi)似于:

1

2

3

4

5

6

# Default limit for number of user's processes to prevent

# accidental fork bombs.

# See rhbz #432903 for reasoning.

?

*????????? soft??? nproc???? 1024

root?????? soft??? nproc???? unlimited

把里面的數(shù)值改掉即可。

由于不方便修改Linux系統(tǒng)配置,因此,我把ThreadPoolExecutor的線程數(shù)調(diào)小到100,發(fā)現(xiàn)程序運(yùn)行起來(lái)再也沒(méi)有出現(xiàn)過(guò)上面的問(wèn)題,非常穩(wěn)定。

總結(jié)

以上是生活随笔為你收集整理的使用ThreadPoolExecutor产生的 OutOfMemoryError: unable to create new native thread 错误的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。