If too many files are open on a system, errors may occur, preventing the opening of a database, the call to a URL, etc. It is therefore necessary to ensure that this number remains below the maximum threshold.
The sysctl utility displays operating system settings. The file-max variable indicates the maximum number of files open by all processes.
The ulimit utility displays the limits set by process. The "open files" field contains the maximum number of open files per process.
The lsof utility displays details about open files. By counting the number of lines in the response, we get the number of opened files.
This command line continuously displays, with a 10-second pause, the number of files opened by each Java process. If there are several Java processes, this creates as many corresponding columns:
If the lsof utility is not available, it can be replaced by the following command which returns essentially the same values:
Shows the 10 largest opened files, sorted in descending order of size, excluding deleted files:
Add "-u myuser" after lsof to limit execution to a specific user's files.
This script can be tested by simulating an opened file (it redirects the output to a file then increase its filesize to 200 megabytes):