为什么我们应该关闭 java.util.Scanner 变量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24573084/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why we should I close a java.util.Scanner variable?
提问by Divyanshu Jimmy
I got a warning in Eclipse with the following code:
我在 Eclipse 中收到警告,代码如下:
Code:
代码:
Scanner money = new Scanner(System.in);
System.out.println(money.nextLine());
//money.close();
Warning:
警告:
Description Resource Path Location Type
Resource leak: 'money' is never closed apples.java /SwordsNMoney/src line 6 Java P
What is this warning and what does 'Resource Leak' mean?
这是什么警告,“资源泄漏”是什么意思?
Thank you.
谢谢你。
采纳答案by SparkOn
Resource leakis generally an erroneous pattern of resource consumption where a program does not release the resource it has acquired. This can lead to poor services.
资源泄漏通常是一种错误的资源消耗模式,其中程序不释放它已获取的资源。这可能会导致服务质量差。
Garbage collection can only manage memory, not other system resources. If your Java program has plenty of free memory, garbage collection will not be triggered automatically.
垃圾回收只能管理内存,不能管理其他系统资源。如果您的 Java 程序有足够的空闲内存,则不会自动触发垃圾回收。
All OSes have limits on the number of sockets, file handles, etc., that can be open. Thus, the unintentional maintenence of references to non-memory resources can lead to a resource leak. So it is extremely important to manage non-memory resources.
所有操作系统对可以打开的套接字、文件句柄等的数量都有限制。因此,无意维护对非内存资源的引用可能导致资源泄漏。所以管理非内存资源是极其重要的。
Classes which utilize non-memory resources should provide ways to explicitly allocate/deallocate those resources. We need to explictly call close()
methods for deallocation of file descriptors in finally{}
, as it will execute whether or not an exception is thrown.
使用非内存资源的类应该提供显式分配/解除分配这些资源的方法。我们需要显式调用close()
中文件描述符的释放方法finally{}
,因为无论是否抛出异常,它都会执行。
回答by Aniket Inge
Scanner
opens an underlying OS's file descriptor (or file channel, or stream), which typically is written in a non-managed(typically C
language).
Scanner
打开底层操作系统的文件描述符(或文件通道或流),它通常是用非托管(通常是C
语言)编写的。
A stream kept open, can sometimes stay opened until the kernel decides to close it(like, after the program has completed execution... highly implementation dependent).
一个流保持打开状态,有时可以保持打开状态直到内核决定关闭它(例如,在程序完成执行之后......高度依赖于实现)。
Hence its a good idea to close the resource explicitly.
因此,明确关闭资源是个好主意。
回答by Rahul Tripathi
You need to call 'close' on IO classes.
您需要在 IO 类上调用“关闭”。
You can use try catch and finally block and in the finally block you can close the scanner.
您可以使用 try catch 和 finally 块,在 finally 块中您可以关闭扫描仪。
From the docs
从文档
Close
If this scanner has not yet been closed then if its underlying readable also implements the Closeable interface then the readable's close method will be invoked. If this scanner is already closed then invoking this method will have no effect.
关闭
如果这个扫描器还没有关闭,那么如果它的底层可读也实现了 Closeable 接口,那么可读的 close 方法将被调用。如果此扫描器已关闭,则调用此方法将无效。
回答by paubo147
It could be, that the underlying resource, which is a file descriptor (in this case System.in) is never released, and thus blocked if you do not close it properly.
可能是,作为文件描述符(在本例中为 System.in)的基础资源永远不会被释放,因此如果您没有正确关闭它,则会被阻止。
As far as I know, Java has some kind of handling-mechanisms to close open Scanners automatically. Someone knows more about this?
据我所知,Java 有某种处理机制来自动关闭打开的扫描器。有人对此了解更多吗?
回答by rs4706
Leak in general defined as not gets garbage collected. Examples are thread leak, object leak- In such cases memory occupied by thread, object not get garbage collected.
泄漏通常定义为没有被垃圾收集。例子是线程泄漏、对象泄漏——在这种情况下,线程占用了内存,对象不会被垃圾收集。
Here you are not closing money reference variable. So reference not gets released. So its referred as Reference leak.
在这里,您不是关闭资金参考变量。所以参考不会被释放。所以它被称为参考泄漏。