On Linux, ulimit -n
can be used to change or view the limit on the number of file descriptors for a process, and lsof -p nnn | wc -l
seems to consistently report the actual file descriptor usage.
But on Mac OS X, lsof -p nnn | wc -l
can return a number higher than the limit. I suppose this means lsof
is returning more than just file descriptors, but I can't tell what's what.
Bottom line: How can I get an accurate count of file descriptor usage in Mac OS X?
At least on OSX 10.10 (Yosemite, didn't check on Mavericks), you can get the list of open files by process via the default activity monitor application. Just double click on the relevant process on the list and select "Open Files and Ports" tab on the popup.
PROTIP: On MacOS, the maximum number that can be specified is 12288.
In the /proc pseudo filesystem, we can find the open file descriptors under /proc/<pid>/fd/ where <pid> is the PID of a given process. Thus, we have to determine the process identification number (PID) of a process to look at its open file descriptors.
I came across the need for identifying this recently - the command I used to count up the total entries (so more than just file handles, but its relative so therefore relevant imo) is:
lsof | awk '{print $1}' | uniq -c | sort -rn | head
This gives something like the following output (your highest used applications may be different!):
$lsof | awk '{print $1}' | uniq -c | sort -rn | head 3271 com.apple 2978 Google 914 Atom\x20H 505 Skype 476 Microsoft 375 Screenher 304 Finder 292 Dock 277 Atom\x20H 270 Atom\x20H
I usually only need to see the top 10 entries, but you can manipulate head
to show as many lines as you like.
lsof can show a lot of things beyond just file descriptors, but most of what is likely inflating your count is the loaded frameworks and libraries for an application. You can look at the "FD" column to see if a line is a file descriptor--in which case it's a number, possibly followed by a letter indicating the mode--or something else (see the description of the FD column in the lsof man page for the full list).
If you just need a rough approximation adding a 'grep -v " txt "' before your wc will get you a lot closer to an accurate value. If you need an exact value, you probably need to put together a regex to feed the output through that filers precisely by the FD column.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With