生產(chǎn)環(huán)境某個服務(wù)報Too many open files問題
1效斑、首先確認允許打開的最大文件數(shù)是多少
[root@101 ~]# ulimit -a
core file size (blocks, -c) unlimited
data seg size (kbytes, -d) unlimited
scheduling priority (-e) 0
file size (blocks, -f) unlimited
pending signals (-i) 15215
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) unlimited
cpu time (seconds, -t) unlimited
max user processes (-u) 65535
virtual memory (kbytes, -v) unlimited
file locks (-x) unlimited
看open files參數(shù)语御,結(jié)果是65535爷光,挺大的了
2垫竞、查看系統(tǒng)現(xiàn)在打開的文件數(shù)
[root@101 ~]# lsof |wc -l
66801
3、查看java進程打開的文件數(shù)
[root@101 ~]# lsof -p 12025|wc -l
65613
12025是進程id蛀序,65613超過了系統(tǒng)設(shè)置的65535了
4件甥、查看進程打開了哪些文件
[root@101 ~]# lsof -p 12025
java 12025 tomcat 92r REG 253,0 9281131 655486 /data/java/qqwry.dat
java 12025 tomcat 93r REG 253,0 9281131 655486 /data/java/qqwry.dat
java 12025 tomcat 94r REG 253,0 9281131 655486 /data/java/qqwry.dat
java 12025 tomcat 95r REG 253,0 9281131 655486 /data/java/qqwry.dat
java 12025 tomcat 96r REG 253,0 9281131 655486 /data/java/qqwry.dat
java 12025 tomcat 97r REG 253,0 9281131 655486 /data/java/qqwry.dat
java 12025 tomcat 98r REG 253,0 9281131 655486 /data/java/qqwry.dat
java 12025 tomcat 99r REG 253,0 9281131 655486 /data/java/qqwry.dat
java 12025 tomcat 100r REG 253,0 9281131 655486 /data/java/qqwry.dat
java 12025 tomcat 101r REG 253,0 9281131 655486 /data/java/qqwry.dat
java 12025 tomcat 102r REG 253,0 9281131 655486 /data/java/qqwry.dat
java 12025 tomcat 103r REG 253,0 9281131 655486 /data/java/qqwry.dat
java 12025 tomcat 104r REG 253,0 9281131 655486 /data/java/qqwry.dat
java 12025 tomcat 105r REG 253,0 9281131 655486 /data/java/qqwry.dat
java 12025 tomcat 106r REG 253,0 9281131 655486 /data/java/qqwry.dat
java 12025 tomcat 107r REG 253,0 9281131 655486 /data/java/qqwry.dat
java 12025 tomcat 108r REG 253,0 9281131 655486 /data/java/qqwry.dat
java 12025 tomcat 109r REG 253,0 9281131 655486 /data/java/qqwry.dat
java 12025 tomcat 110r REG 253,0 9281131 655486 /data/java/qqwry.dat
java 12025 tomcat 111r REG 253,0 9281131 655486 /data/java/qqwry.dat
java 12025 tomcat 112r REG 253,0 9281131 655486 /data/java/qqwry.dat
java 12025 tomcat 113r REG 253,0 9281131 655486 /data/java/qqwry.dat
java 12025 tomcat 114r REG 253,0 9281131 655486 /data/java/qqwry.dat
java 12025 tomcat 115r REG 253,0 9281131 655486 /data/java/qqwry.dat
java 12025 tomcat 116r REG 253,0 9281131 655486 /data/java/qqwry.dat
java 12025 tomcat 117r REG 253,0 9281131 655486 /data/java/qqwry.dat
java 12025 tomcat 118r REG 253,0 9281131 655486 /data/java/qqwry.dat
java 12025 tomcat 119r REG 253,0 9281131 655486 /data/java/qqwry.dat
java 12025 tomcat 120r REG 253,0 9281131 655486 /data/java/qqwry.dat
java 12025 tomcat 121r REG 253,0 9281131 655486 /data/java/qqwry.dat
java 12025 tomcat 122r REG 253,0 9281131 655486 /data/java/qqwry.dat
java 12025 tomcat 123r REG 253,0 9281131 655486 /data/java/qqwry.dat
java 12025 tomcat 124r REG 253,0 9281131 655486 /data/java/qqwry.dat
java 12025 tomcat 125r REG 253,0 9281131 655486 /data/java/qqwry.dat
發(fā)現(xiàn)大量打開qqwry.dat這個文件。
5哼拔、計算一下打開qqwry.dat的次數(shù)
[root@101 ~]# lsof -p 12025|grep "qqwry.dat" |wc -l
65212
打開65212次引有,讓開發(fā)的同學(xué)去查看與這個文件相關(guān)的代碼邏輯,發(fā)現(xiàn)這個類的邏輯里倦逐,只管打開文件譬正,并沒有關(guān)閉文件的代碼邏輯,也就是打開了檬姥,并不關(guān)閉曾我,而且每一次請求過來,就new一個這樣的對象出來健民,很明顯這樣的邏輯不妥抒巢。讓其不要每次都new一個對象了,建議改成單例模式秉犹。
上線后蛉谜,再來看結(jié)果
[root@101 ~]# lsof -u tomcat |grep "qqwry.dat" |wc -l
1
哈哈,這個數(shù)量變成1了崇堵,爽歪歪:)
另外型诚,發(fā)現(xiàn)一個博主寫的關(guān)于qqwry.dat的封裝類,在這里https://blog.51cto.com/binghe001/2924976鸳劳,有需要的小伙伴可以參考