JVM是否自动关闭文件? [英] Does JVM automatically closes files?

查看:75
本文介绍了JVM是否自动关闭文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读到某个地方,不必自己关闭它,只要离开它,JVM就会帮助您做到这一点.是真的吗

Somewhere I read that it's unnecessary to close it by yourself, just leave it, JVM will help you do this. Is it true?

假设我需要使用

Source.fromFile(fileName).getLines() 

直接,没有

val source = Source.fromFile(fileName)
val lines = source.getLines()
............
source.close()

第一种方式,我无法直接访问源并关闭它.我的JVM工作了很长时间.而且我需要关闭一个文件(需要关闭未使用的资源).

In a first way, I can't get access directly to the source and close it. My JVM works for a long period. And I need a file to be closed (unused resources need to be closed).

如果有人可以在此处留下一些链接或解释,那就太好了.

It would be great if somebody could leave some links or explanations here.

推荐答案

我读到某个地方,不必自己关闭它,只要离开它,JVM就会帮助您做到这一点.是真的吗

Somewhere I read that it's unnecessary to close it by yourself, just leave it, JVM will help you do this. Is it true?

部分正确.

如果打开文件,请使用它,然后放下文件或流句柄(或任何您想调用的文件),然后GC会找到它,然后GC会将文件句柄对象排队以进行最终确定.当完成确定时,文件处理程序的finalize()方法将释放资源.即文件描述符.

If you open a file, use it and then drop the file or stream handle (or whatever you want to call it), AND the GC finds it, THEN the GC will queue the file handle object for finalization. When the finalization occurs the file handler's finalize() method will release the resources; i.e. the file descriptor.

但是,依靠GC进行操作是个坏主意.

However, it is a bad idea to rely on the GC to do this.

  • 如果文件句柄可访问,则GC不会最终确定它.
  • 您有办法知道下一次GC何时运行 1 .
  • 并且当GC运行时,它不一定会收集所有垃圾 2 .
  • 排队等待完成的对象直到GC完成后才真正完成. (请参阅@Holger的评论)
  • If a file handle is reachable, then the GC won't finalize it.
  • You have of way of knowing when the GC is going to run next1.
  • And when the GC runs, it won't necessarily collect all of the garbage2.
  • And objects that are queued for finalization don't get actually finalized until after the GC finishes. (See @Holger's comment)

将这四件事放在一起,在GC收集并关闭废弃的文件句柄之前,应用程序可以很容易地用完文件描述符.如果发生这种情况,您很可能会在打开文件,目录,套接字等操作的操作中遇到异常.

Put these four things together, and an application can easily run out of file descriptors before the GC gets around to collecting and closing the abandoned file handles. If that happens, you are liable to get exceptions on operations that open files, directories, sockets, etcetera.

以下是您可以运行(在Linux/UNIX上)以查看发生这种情况的示例:

Here's an example you can run (on Linux / UNIX) to see this happening:

import java.io.FileInputStream;

public class Test {
    public static void main(String[] args) throws Exception {
        for (int i = 0; i < 100000; i++) {
            new FileInputStream("/etc/motd");
        }
    }
}

$ javac Test.java 
$ java Test 
Exception in thread "main" java.io.FileNotFoundException: /etc/motd (Too many open files)
    at java.io.FileInputStream.open0(Native Method)
    at java.io.FileInputStream.open(FileInputStream.java:195)
    at java.io.FileInputStream.<init>(FileInputStream.java:138)
    at java.io.FileInputStream.<init>(FileInputStream.java:93)
    at Test.main(Test.java:6)


1-典型的GC仅在堆(或堆的一部分)达到给定的充满"阈值时运行.如果应用程序没有分配很多对象,则达到阈值可能要花费很长时间.

2-现代JVM使用分代垃圾收集器,它们以不同的速率收集堆的不同部分.

这篇关于JVM是否自动关闭文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆