简体   繁体   English

这是内存泄漏还是误报?

[英]Is this a memory leak or a false positive?

This is my code: 这是我的代码:

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Scanner;

public class temp {
    public static void main(String[] args) throws FileNotFoundException {
        BufferedReader a = new BufferedReader(new FileReader("a"));
        Scanner scanner = new Scanner(a).useDelimiter(",");
        scanner.close();
    }
}

I get a warning at new Scanner(a) that says (I'm compiling with jdk1.7.0_05.): 我在new Scanner(a)上发出警告说(我正在用jdk1.7.0_05编译。):

Resource leak: '<unassigned Closeable value>' is never closed.

Am I doing something wrong, or is this just a false warning? 我做错了什么,或者这只是一个错误的警告?

If you split the code like this, will the warning go away? 如果您像这样拆分代码,警告会消失吗?

  Scanner scanner = new Scanner(a);
  scanner.useDelimiter(",");
  scanner.close();

Yes, your code has a potential (but not real) memory leak. 是的,您的代码有潜在的(但不是真正的)内存泄漏。 You assign the return value of useDelimiter(a) to the local variable scanner , but the constructor result is thrown away. 您将useDelimiter(a)的返回值分配给局部变量scanner ,但构造函数结果将被丢弃。 That is why you get the warning. 这就是你得到警告的原因。

In practice, the return value of useDelimiter(a) is exactly the same object as the one returned from the constructor call, so your code closes the resource just fine. 实际上, useDelimiter(a)的返回值与构造函数调用返回的对象完全相同,因此您的代码可以很好地关闭资源。 But this is something the compiler/code analysis tool cannot detect as it would have to know the useDelimiters implementation for that. 但这是编译器/代码分析工具无法检测到的,因为它必须知道useDelimiters实现。

And a really good code analysis tool should have shown you an additional warning, because you are closing a resource which has not been opened in this method (the return value of useDelimiter). 一个非常好的代码分析工具应该向您显示一个额外的警告,因为您正在关闭一个尚未在此方法中打开的资源(useDelimiter的返回值)。 If you had those 2 messages together, the symptoms might have been more clear to you. 如果您将这两条消息放在一起,症状可能会更清楚。

Have you try : 你试过:

Scanner scanner = new Scanner(new BufferedReader(new FileReader("a"))).useDelimiter(",");

If it does not work you have to add a.close(); 如果它不起作用,你必须添加a.close();

What is giving you this warning? 什么给你这个警告? Presumably it warns because the allocation/closing isn't done in a try/finally block, which generally is a bad idea (in this specific case not a problem though because the only thing that can throw an error is the new FileReader and if it throws no resource was actually allocated - but that can change with a single method call..) 据推测它会发出警告,因为分配/关闭不是在try / finally块中完成的,这通常是一个坏主意(在这种特定情况下不是问题,因为唯一可以抛出错误的是新的FileReader,如果它抛出没有实际分配的资源 - 但是可以通过单个方法调用来改变..)

Closing a scanner closes the underlying stream (to be exact anything that implements itself Closeable (yes BufferedReader does) so the code is fine apart from that. 关闭扫描程序会关闭底层流(确切地说是任何实现自身的东西都是Closeable (是的, BufferedReader Closeable )所以代码就可以了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM