What options are available to get the (hard/soft) open file limits in java?
OSHI supports getting the maximum amount of open file descriptors in its FileSystem.getMaxFileDescriptors() method but it mentions that "There may be a lower per-process limit.", which seems a bit too vague to me.
Another possiblity would be to use JNA or JNI to call getrlimit(), which should get the open file limits of the JVM process. But I am a bit concerned if this will work on enough operating systems.
Are there other possiblities other than the mentioned ones above?
Linux provides these values in
/proc/sys/fsMacOS provides these values via sysctl:
FreeBSD also provides them via sysctl, but with a different name for the open files.
OpenBSD also provides them via sysctl, and also with different names.
Solaris provides the values in
kstat:/kmem_cache/kmem_default/file_cacheforbuf_inuseandbuf_max.On some Unix-based filesystem, you can probably capture the command line output of
lsof -nlto count the current files (although this may be slow), andulimit -nfor the max.OSHI returns the values from procfs for Linux, sysctl for macOS, FreeBSD, OpenBSD, kstat for Solaris, and from the command lines above for AIX.
I may be biased as OSHI's author, but that's probably your easiest route to getting these values cross-platform.
For Windows, there are no "file limits" per se. Windows enforces limits on handles. See this Stack Exchange question for more details on those limits. That question and answer are the source of the qualified language I put in OSHI's javadocs.
and also
and