Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java - getOpenFileDescriptorCount for Windows

How to get the number of open File descriptors under Windows?

On unix there is this:

UnixOperatingSystemMXBean.getOpenFileDescriptorCount()

But there doesn't seem to be an equivalent for windows?

like image 291
benji Avatar asked Nov 08 '22 19:11

benji


1 Answers

This was going to be a comment but got a little long winded.

Conflicting answers as to why there may be a lack of equivalence here on ServerFault: Windows Server 2008 R2 max open files limit. TLDR: Windows is only limited by available hardware vs Windows is limited by 32 vs 64 bit implementation (MS Technet Blog Post - Pushing the Limits of Windows: Handles). Granted, this is old information.

But! if you note the JavaDocs for the com.sun.management package, you will of course note the conspicuous absence of a Windows version of the the UnixOperatingSystemMXBean that would extend OperatingSystemMXBean to provide the functionality. Even UnixOperatingSystemMXBean only exists to provide getMaxFileDescriptorCount() and getOpenFileDescriptorCount() so it seems unlikely that Windows has the same concept.

Edit:

I did find a nice little program that sort of shows this off, which I tweaked. Descriptors.java

import java.lang.management.ManagementFactory;
import java.lang.management.OperatingSystemMXBean;
import java.lang.reflect.Method;

class Descriptors {
    public static void main(String [ ] args) {
        System.out.println(osMxBean.getClass().getName());
        OperatingSystemMXBean osMxBean = ManagementFactory.getOperatingSystemMXBean();
        try {
            Method getMaxFileDescriptorCountField = osMxBean.getClass().getDeclaredMethod("getMaxFileDescriptorCount");
            Method getOpenFileDescriptorCountField = osMxBean.getClass().getDeclaredMethod("getOpenFileDescriptorCount");
            getMaxFileDescriptorCountField.setAccessible(true);
            getOpenFileDescriptorCountField.setAccessible(true);
            System.out.println(getOpenFileDescriptorCountField.invoke(osMxBean) + "/" + getMaxFileDescriptorCountField.invoke(osMxBean));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

On Linux:

com.sun.management.UnixOperatingSystem
11/2048

On Windows:

sun.management.OperatingSystemImpl
java.lang.NoSuchMethodException: 
sun.management.OperatingSystemImpl.getMaxFileDescriptorCount()
at java.lang.Class.getDeclaredMethod(Unknown Source)
at Descriptors.main(Descriptors.java:10)
like image 79
Jon Sampson Avatar answered Nov 15 '22 06:11

Jon Sampson