栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

文件描述符泄漏示例?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

文件描述符泄漏示例?

由于Dalvik的 FileInputStream
会在被垃圾回收时自行关闭(对于OpenJDK
/
Oracle也是这样),因此它不如实际泄漏文件描述符那样普遍。当然,文件描述符将被“泄漏”,直到GC运行,因此取决于您的程序,可能需要一段时间才能收回它们。


为了实现更持久的泄漏,您必须通过在内存中的某个位置对其进行引用来防止对该流进行垃圾收集。

这是一个简短的示例,该示例每1秒加载一次属性文件,并跟踪每次更改的情况:

public class StreamLeak {        public static class Revision {        final ZonedDateTime time = ZonedDateTime.now();        final PropertiesFile file;        Revision(PropertiesFile file) { this.file = file;        }    }        public static class PropertiesFile {        private final InputStream stream;        private Properties properties;        PropertiesFile(InputStream stream) { this.stream = stream;        }        Properties getProperties() { if(this.properties == null) {     properties = new Properties();     try {         properties.load(stream);     } catch(IOException e) {         e.printStackTrace();     } } return properties;        }        @Override        public boolean equals(Object o) { if(o instanceof PropertiesFile) {     return ((PropertiesFile)o).getProperties().equals(getProperties()); } return false;        }    }    public static void main(String[] args) throws IOException, InterruptedException {        URL url = new URL(args[0]);        linkedList<Revision> revisions = new linkedList<>();        // Loop indefinitely        while(true) { // Load the file PropertiesFile pf = new PropertiesFile(url.openStream()); // See if the file has changed if(revisions.isEmpty() || !revisions.getLast().file.equals(pf)) {     // Store the new revision     revisions.add(new Revision(pf));     System.out.println(url.toString() + " has changed, total revisions: " + revisions.size()); } Thread.sleep(1000);        }    }}

由于延迟加载,我们将 InputStream 保留在 PropertiesFile中 ,每当我们创建新的 Revision
时都会将其保留,并且由于我们从不关闭流,因此此处将泄漏文件描述符。

现在,当程序终止时,操作系统将关闭这些打开的文件描述符,但是只要程序正在运行,它将继续泄漏文件描述符,如使用
lsof 可以看到的那样:

$ lsof | grep pf.properties | head -n 3java    6938   raniz   48r      REG    252,0    0    262694 /tmp/pf.propertiesjava    6938   raniz   49r      REG    252,0    0    262694 /tmp/pf.propertiesjava    6938   raniz   50r      REG    252,0    0    262694 /tmp/pf.properties$ lsof | grep pf.properties | wc -l    431

而且,如果我们强制运行GC,则可以看到大部分返回:

$ jcmd 6938 GC.run6938:Command executed successfully$ lsof | grep pf.properties | wc -l2

剩下的两个描述符是存储在 Revision中 的描述符。

我在Ubuntu机器上运行了此程序,但是如果在Android上运行,则输出看起来类似。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/414461.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号