My OS is MacOs sierra, and I increase the maximum of opened files for each process using following commands and make them permanently affected:
$ sysctl kern.maxfiles
kern.maxfiles: 12288
$ sysctl kern.maxfilesperproc
kern.maxfilesperproc: 10240
$ sudo sysctl -w kern.maxfiles=1048600
kern.maxfiles: 12288 -> 1048600
$ sudo sysctl -w kern.maxfilesperproc=1048576
kern.maxfilesperproc: 10240 -> 1048576
$ ulimit -S -n
256
$ ulimit -S -n 1048576
$ ulimit -S -n
It indeed works when I use ulimit -a
to see the limit in terminal.
However, When I run my Java server program, when then socket connection approach 5110,
"Too many open files Exception is still throw"
So I write following code to see the parameter of kernel when java running:
String [] cmd={"/bin/bash","-c","ulimit -a"};
Process proc = Runtime.getRuntime().exec(cmd);
InputStream stderr = proc.getInputStream();
InputStreamReader isr = new InputStreamReader(stderr);
BufferedReader br = new BufferedReader(isr);
String line;
while ( (line = br.readLine()) != null) {
System.out.println(line);
}
And the result shows
open files (-n) 10240
It's very confused because I try to increase the limits everywhere I know.
* ~/.bash_profile: initialization for login (bash-)shells
* ~/.bashrc: initialization for all interactive (bash-)shells
* ~/.profile
* /etc/bashrc: meant for functions and aliases
* /etc/profile: all types of initialization scripts'
Anyone can give me any hints about this question? Thanks a lot for you.