AudioInputStream.lose()未正确释放资源,是否有解决方法?

2022-04-09 00:00:00 audio java inputstream ioexception

这是关于我观察到的一个场景的问题:如何有效地关闭由AudioSystems.getAudioInputStream这样的Java库打开的、我无法查看和控制的系统资源?

请看我的以下代码片段:

public static void main(String[] args) throws UnsupportedAudioFileException, IOException {
    System.out.println("Before everthing starts...Number of open fd: " + ((UnixOperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean()).getOpenFileDescriptorCount());
    AudioInputStream stream = AudioSystem.getAudioInputStream(new File("CaptchaResource/silence20ms.wav"));
    System.out.println("After reading in one AudioInputStream...Number of open fd: " + ((UnixOperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean()).getOpenFileDescriptorCount());
    stream.close();
    System.out.println("After closing the AudioInputStream...Number of open fd: " + ((UnixOperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean()).getOpenFileDescriptorCount());
    stream = AudioSystem.getAudioInputStream(new File("CaptchaResource/silence20ms.wav"));
    System.out.println("After reading in one AudioInputStream...Number of open fd: " + ((UnixOperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean()).getOpenFileDescriptorCount());
    stream.close();
    System.out.println("After closing the AudioInputStream...Number of open fd: " + ((UnixOperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean()).getOpenFileDescriptorCount())
    stream = AudioSystem.getAudioInputStream(new File("CaptchaResource/silence20ms.wav"));
    System.out.println("After reading in one AudioInputStream...Number of open fd: " + ((UnixOperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean()).getOpenFileDescriptorCount());
    stream.close();
    System.out.println("After closing the AudioInputStream...Number of open fd: " + ((UnixOperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean()).getOpenFileDescriptorCount());
}

如您所见,我使用以下行输出了程序运行时打开的文件描述符的数量:

System.out.println("Number of open fd: " + ((UnixOperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean()).getOpenFileDescriptorCount());

以下是我观察到的输出:

Before everthing starts...Number of open fd: 4
After reading in one AudioInputStream...Number of open fd: 44
After closing the AudioInputStream...Number of open fd: 43
After reading in one AudioInputStream...Number of open fd: 45
After closing the AudioInputStream...Number of open fd: 44
After reading in one AudioInputStream...Number of open fd: 46
After closing the AudioInputStream...Number of open fd: 45

您可以看到,除了第一次从4跳到44之外,每次我调用getAudioInputStream时都会打开另外两个文件描述符。而当我在流上调用Close()时,我只能关闭一个。

因此,当我的程序必须连续多次处理此操作时,随着时间的推移,我最终将达到Unix系统中1024个打开文件描述符的限制。

通过查看getAudioInputStream的source code,我不确定这是否与由于可能存在多个提供程序而在此方法内打开了多个读取器有关,不确定那里的实现细节。

这听起来像是JDK中报告的错误,如:https://bugs.openjdk.java.net/browse/JDK-8013586

这方面有什么变通办法吗?如何重写代码以避免发生这种无法控制的情况?


解决方案

好吧,我不知道是否有其他人有更好的方法来做这件事。现在我将自己回答这个问题,因为这是我要使用的解决方法: 在方法结尾处调用System.gc()。

不是世界上最伟大的事情。

相关文章