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

后端 未结 5 1752
北恋
北恋 2020-12-13 09:01

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 ... n

5条回答
  •  悲哀的现实
    2020-12-13 09:22

    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());
            }
        }
    }
    

提交回复
热议问题