macosunixlsof

In Mac OS X, how can I get an accurate count of file descriptor usage?


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?


Solution

  • 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.