Tuning kern.maxfilesperproc for large memory FreeBSD systems

I have FreeBSD 10.1-RELEASE running on a machine with 512GB memory. Overall, FreeBSD runs very well on this hardware, but I was having performance problems where some programs that I expected to run essentially instantly were taking several seconds to complete. Running screen -ls took around 5 seconds, for example, but there were other programs with similar behaviour.

It turns out that some programs try to explicitly close every file descriptor after calling fork(), presumably for security reasons. This shows up very clearly when runing truss on the executable. This is fine except that FreeBSD tunes the maximum number of open files per process based on available memory (the kern.maxfilesperproc sysctl gives the limit). With 512GB memory, kern.maxfilesperproc=15098265 and calling close() on 15098262 non-open file descriptors takes some time. The fix is to set kern.maxfilesperproc to a lower value in /etc/sysctl.conf and reboot.