情況:
linux環境在沒動過任何code情況下,只有資料量變多,在執行下面程式時會噴
java.net.SocketException: Too many open files
原code:
StringBuffer sb = new StringBuffer();
String templatePath = PropertiesUtil.getProperty("MAIL_NOTICE_TEMPLATE_DONATE_PATH_IN_CLASSPATH");
InputStreamReader isr = new InputStreamReader(NoticeUtil.class.getResourceAsStream(templatePath));
BufferedReader br = new BufferedReader(isr);
String tmp = null;
try {
while((tmp = br.readLine()) != null) {
sb.append(tmp);
}
} catch (IOException e) {
log.error("讀取Notice html發生錯誤 for Award Invoic");
e.printStackTrace();
return "";
}
Map<String, String> map = new HashMap<String, String>();
for (Iterator<AwardInvoice> it = aiList.iterator(); it.hasNext();) {
AwardInvoice ai = it.next();
map.put(ai.getPrizeType(), getPrizeInChinese(ai.getPrizeType()));
}
後來查一下之後發現可用 ulimit -a這指令查看目前系統資訊
ulimit -a
core file size (blocks, -c) 0
data seg size (kbytes, -d) unlimited
scheduling priority (-e) 0
file size (blocks, -f) unlimited
pending signals (-i) 131072
max locked memory (kbytes, -l) 32
max memory size (kbytes, -m) unlimited
open files (-n) 4096
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) 131072
virtual memory (kbytes, -v) unlimited
file locks (-x) unlimited
可重新設定開啟檔案數
ulimit -n 8192
core file size (blocks, -c) 0
data seg size (kbytes, -d) unlimited
file size (blocks, -f) unlimited
pending signals (-i) 1024
max locked memory (kbytes, -l) 32
max memory size (kbytes, -m) unlimited
open files (-n) 8192
pipe size (512 bytes, -p) 8
POSIX message queues (bytes, -q) 819200
stack size (kbytes, -s) 10240
cpu time (seconds, -t) unlimited
max user processes (-u) 16382
virtual memory (kbytes, -v) unlimited
file locks (-x) unlimited
data seg size (kbytes, -d) unlimited
file size (blocks, -f) unlimited
pending signals (-i) 1024
max locked memory (kbytes, -l) 32
max memory size (kbytes, -m) unlimited
open files (-n) 8192
pipe size (512 bytes, -p) 8
POSIX message queues (bytes, -q) 819200
stack size (kbytes, -s) 10240
cpu time (seconds, -t) unlimited
max user processes (-u) 16382
virtual memory (kbytes, -v) unlimited
file locks (-x) unlimited
但想一想這或許不是解決辦法,後來又仔細看了一下code,發現下面兩行,使用完後沒有close掉,所以就試看看改成
InputStreamReader isr = new InputStreamReader(NoticeUtil.class.getResourceAsStream(templatePath));
BufferedReader br = new BufferedReader(isr);
修改後的code加了close:
StringBuffer sb = new StringBuffer();
String templatePath = PropertiesUtil.getProperty("MAIL_NOTICE_TEMPLATE_DONATE_PATH_IN_CLASSPATH");
InputStreamReader isr = new InputStreamReader(NoticeUtil.class.getResourceAsStream(templatePath));
BufferedReader br = new BufferedReader(isr);
String tmp = null;
try {
while((tmp = br.readLine()) != null) {
sb.append(tmp);
}
} catch (IOException e) {
log.error("讀取Notice html發生錯誤 for Award Invoic");
e.printStackTrace();
return "";
} finally {
try {
br.close();
isr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
再重新執行一次就沒噴了,以前學習時雖然都說要close覺得好像沒關程式也都可以run,就想
說有沒有關可能都沒差,結果在資料量大的時候沒關,果然還是有差!
所以以後還是要記得close