【FreeBSD】Too Many Open Files

Posted by 西维蜀黍 on 2021-08-28, Last Modified on 2022-02-19

System Level

$ sysctl -a |grep files
kern.maxfiles: 780418
kern.maxfilesperproc: 702369
kern.openfiles: 627
p1003_1b.mapped_files: 200112

To make the change permanent, use sudo to put your settings in /etc/sysctl.conf (which you may have to create), like this:


Session/Shell Level

$ ulimit -n

$ limits
Resource limits (current):
  cputime              infinity secs
  filesize             infinity kB
  datasize             33554432 kB
  stacksize              524288 kB
  coredumpsize         infinity kB
  memoryuse            infinity kB
  memorylocked         infinity kB
  maxprocesses            26802
  openfiles              702369
  sbsize               infinity bytes
  vmemoryuse           infinity kB
  pseudo-terminals     infinity
  swapuse              infinity kB
  kqueues              infinity
  umtxp                infinity


 $ lsof -n | awk '{print $2 "\t" $1}' | sort | uniq -c | sort
# lsof outputs open file info, awk then gives us the PID and proc name which gets sorted and uniq gives a count of each which we sort to have the largest file count at the bottom of the list. What you end up with is a list of two numbers and a name - count of files open followed by the PID and proc name that has them open.
# The catch is that it also includes network connections (I know how to list only network but not sure how to exclude them)

# show you the full program name if it has been shortened.
$ ps ax | grep PID

# show all the open files for PID
$ lsof -p PID