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

lsofmacosunix

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?

Best Solution

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.