mac修改进程能打开的最大文件数

时间:2021-09-09 09:32:25

mac进程能打开的最大文件数,默认只有256
$ulimit -n 
256

这会导致go的服务端运行的时候,如果同时连接的客户端太多,会报错:


2015/08/31 11:47:57 http: Accept error: accept tcp [::]:12621: too many open files; retrying in 10ms
2015/08/31 11:47:58 http: Accept error: accept tcp [::]:12621: too many open files; retrying in 20ms
2015/08/31 11:47:58 http: Accept error: accept tcp [::]:12621: too many open files; retrying in 40ms
2015/08/31 11:47:58 http: Accept error: accept tcp [::]:12621: too many open files; retrying in 80ms
2015/08/31 11:47:58 http: Accept error: accept tcp [::]:12621: too many open files; retrying in 160ms
2015/08/31 11:47:58 http: Accept error: accept tcp [::]:12621: too many open files; retrying in 320ms
2015/08/31 11:47:58 http: Accept error: accept tcp [::]:12621: too many open files; retrying in 640ms
2015/08/31 11:47:59 http: Accept error: accept tcp [::]:12621: too many open files; retrying in 1s
2015/08/31 11:48:00 http: Accept error: accept tcp [::]:12621: too many open files; retrying in 1s
2015/08/31 11:48:01 http: Accept error: accept tcp [::]:12621: too many open files; retrying in 1s
2015/08/31 11:48:02 http: Accept error: accept tcp [::]:12621: too many open files; retrying in 1s
2015/08/31 11:48:03 http: Accept error: accept tcp [::]:12621: too many open files; retrying in 1s
2015/08/31 11:48:04 http: Accept error: accept tcp [::]:12621: too many open files; retrying in 1s


解决办法:

mac修改进程能打开的最大文件数:
1.
$su 切换到root账户
#launchctl limit maxfiles 99999 99999

#ulimit -n 99999

#exit


2.确认修改生效

$ulimit -n

99999



3.每个窗口的服务都必须在重设ulimit -n之后,重启才生效!


4.举例

cd $GOPATH/src/car_server/bentchmark 

go run main.go 2>2.log &

tail -f 2.log

grep "connect ok" 2.log | wc -l