Search code examples
linuxlinux-kernelfile-descriptor

Get memory usage of open file descriptors


I know how to find number of files currently open using - lsof | wc -l or for a particular PID or for an user.

Is it possible to list the corresponding PID's and how many file descriptors and memory are they using ?

I hope I made it clear. Thanks in advance.


Solution

  • From man ps:

    rss   resident set size
    size      memory size in kilobytes
    

    Just call ps ax -o pid,rss,size to get processes with resident size and memory size. As for file descriptors, we can count number of files in /proc/<PID>/fd/* directory for each process:

    ps -A -opid | sudo xargs -n1 -I{} /bin/bash -c 'echo {} $(ls /proc/{}/fd | wc -l);'