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

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.


I modified anders' answer, now it only displays the opened fd numbers of a specific process:

FCOUNT=`lsof -p $1 | grep -v " txt " | wc -l`;echo "PID: $1 $FCOUNT" | sort -nk3

Example:

$ ./fd-count.sh 5926                                                                                                           
PID: 5926       97

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.

Tags:

Unix

Macos

Lsof