Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find out number of files currently open by Java application?

Tags:

java

Suppose a lot of what your application does deals with reading contents of files. It goes without saying that files that are opened then closed and life is good unless ... new files come in faster then old files get closed. This is the pickle of a situation I found myself in.

Now, is there a way to reliably know how many files are opened by the process? Something that is as reliable as looking at ls /proc/my_pid/fd | wc -l from inside the JVM?

I suspect the answer may be OS-specific, so let me add that I am running Java on Linux.

like image 742
James Raitsev Avatar asked May 03 '13 14:05

James Raitsev


People also ask

How do I fix too many open files in Java?

1) Check for leaks in your application And here is how to fix it: the safe way to release is to use the try-with-resources statement. The try-with-resources statement is a try statement that declares one or more resources. A resource is an object that must be closed after the program is finished with it.

How do I see the number of open files in Linux?

Using the /proc file system The pfiles command displays the current limit of the number of open files for the process and more information about all currently open files of that process. See the following example. Run the sleep 120 seconds command in background and then run pfiles command on the PID of the sleep command.

How to read certain number of elements from a file in Java?

To read a fixed number of elements from a file you can either read a required number of data elements from the file and process them or, read the entire file into a collection or an array and process it for every n element.

How to monitor the number of open files in a process?

The pfiles command 2. Using the /proc file system The pfiles command displays the current limit of the number of open files for the process and more information about all currently open files of that process. See the following example. Run the sleep 120 seconds command in background and then run pfiles command on the PID of the sleep command.

How do I list all open files in a process?

This command lists all open files belonging to processes owned by the user named "al": $ lsof -u al This command lists files that are open in the directory specified, but it does not descend into sub-directories: $ lsof +d '/Users/al'


Video Answer


2 Answers

On Unix, one way is using the ManagementFactory to get the OperatingSystemMxBean and if it is a UnixOperatingSystemMXBean, you can use the getOpenFileDescriptorCount() method.

Example code:

import java.lang.management.ManagementFactory; import java.lang.management.OperatingSystemMXBean; import com.sun.management.UnixOperatingSystemMXBean;  public class OpenFileCount{     public static void main(String[] args){         OperatingSystemMXBean os = ManagementFactory.getOperatingSystemMXBean();         if(os instanceof UnixOperatingSystemMXBean){             System.out.println("Number of open fd: " + ((UnixOperatingSystemMXBean) os).getOpenFileDescriptorCount());         }     } } 
like image 140
FDinoff Avatar answered Oct 07 '22 20:10

FDinoff


If you want to get the number of ls /proc/my_pid/fd | wc -l in java, you could use JMX.

When you have MBeanServerConnection, you can get attribute "OpenFileDescriptorCount". this will give you the same result as the above ls..|wc -l.

also there is another attribute "MaxFileDescriptorCount", which tells your the max count allowed.

If you just want to know the value, but not getting it in your code, you could either do your ls..|wc -l way, or read from jconsole (with GUI).

NOTE

ls /proc/my_pid/fd | wc -l 

this number indicates how many fd opened by your process(java application). e.g. your business files are counted, also those jars, libraries files are counted too. If you just want to get the count of your business files, you have to implement a counter by yourself. Or, say you want to do it with shellscript, grep something out then wc -l

EDIT

add code example, but it is just an example. not written in IDE, not tested with compiler. :)

ObjectName oName = new ObjectName("java.lang:type=OperatingSystem"); MBeanServerConnection conn ; // you should get the connection following the api, take a look the java api/ google some example javax.management.AttributeList list = conn.getAttributes(oName, new String[]{"OpenFileDescriptorCount", "MaxFileDescriptorCount"}); for(Attribute attr: list.asList()){     System.out.println(attr.getName() + ": " + attr.getValue()); } 
like image 33
Kent Avatar answered Oct 07 '22 19:10

Kent