0

I have a problem where my Java application opens too many files. Debugging this issue, I am dependent on using lsof. However running lsof this way takes too much time (more than one minutt):

lsof |grep "java" 

I should be able to run it using the -p option, however it "lies". It shows too few lines.

lsof -p <PID of the java process>

This is my proof :

lsof |grep java  | wc -l 
1510146

lsof -p 802 | wc -l
4735

The same happens if I use the -u option limiting to username (process owner).

My system is : Linux 3.16.0-4-amd64 #1 SMP Debian 3.16.39-1+deb8u2 (2017-03-07) x86_64 GNU/Linux

Am I missing something ? Is there an alternative to using lsof ?

Zamra
  • 29
  • 2

3 Answers3

1

lsof is not lying. The output of the command:

lsof |grep java  | wc -l 

may contain results of files or processes opened by other programs.

The result you are searching for is the result of the command:

lsof -p <PID> | wc -l

You can increase the limit of opened files for the user running your java application adding this line in /etc/security/limits.conf:

<USER>   hard   nofile  65536

you can check the current user's limits by typing:

su - <USER>
ulimit -a 
  • 1
    Thanks for your respond. However I do not think that is why. My java process has pid 41866 `lsof |grep "^java 41866" |wc -l > 857349 ` while : `lsof -p 41866 |wc -l > 1634` See the HUGE difference ? So when I run 'lsof' with no options, it displays many many more lines which start with java and its pid, than when I run 'lsof -p ' I really think this must be a bug. – Zamra Jun 28 '17 at 14:03
  • lsof |grep "^java 41866" |wc -l > 857349 show you also the threads of a process. One process may have more than one thread. [link] http://www.thegeekstuff.com/2013/11/linux-process-and-threads – Lorenzo Garuti Jun 28 '17 at 15:32
0

lsof without parameter lists all open files, including files which are not using file descriptors – such as current working directories, memory mapped library files, and executable text files.

lsof -p <PID> lists open file descriptors. A file descriptor is a data structure used by a program to get a handle on a file, the most well know being 0,1,2 for standard in, standard out, and standard error.

See: https://www.netadmintools.com/art295.html

0

Based on my observation, it seems that

lsof | grep <pid> | wc -l 

will give duplicate count, because every thread in the specified process will add a line, e.g. if your process have 8 threads, the result will be more than 8x the actual file count.

On the other hand,

lsof -p <PID> | wc -l

produce more exact result, because each file is counted (printed) only once.

Although I have not found official reference for this issue yet.