javajava-native-interfacejnaoshi

How to get the open file limit in Java?


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?


Solution

  • Linux provides these values in /proc/sys/fs

    file-max & file-nr The value in file-max denotes the maximum number of file-handles that the Linux kernel will allocate. When you get lots of error messages about running out of file handles, you might want to increase this limit.

    nr_open This denotes the maximum number of file-handles a process can allocate. Default value is 1024*1024 (1048576) which should be enough for most machines. Actual limit depends on RLIMIT_NOFILE resource limit.

    MacOS provides these values via sysctl:

    kern.maxfiles: 245760
    kern.maxfilesperproc: 122880
    kern.num_files: 8685
    

    FreeBSD also provides them via sysctl, but with a different name for the open files.

    kern.maxfiles: 64534
    kern.maxfilesperproc: 58077
    kern.openfiles: 1339
    

    OpenBSD also provides them via sysctl, and also with different names.

    kern.maxfiles=7030
    kern.nfiles=143
    

    Solaris provides the values in kstat:/kmem_cache/kmem_default/file_cache for buf_inuse and buf_max.

    On some Unix-based filesystem, you can probably capture the command line output of lsof -nl to count the current files (although this may be slow), and ulimit -n for 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.

    Theoretically in 64-bit Windows the maximum number of handles that a process can open is 232, because handles have 32 significant bits. However in reality it has been limited to 16,777,216 (224) per process. On 32-bit Windows the limit is likely 216

    and also

    However for a single process that uses the default C run-time libraries then the default limit is 512

    and

    It's unclear the maximum number of total file handles for all processes in Windows