资源泄漏:'in'永远不会关闭

问题描述 投票:74回答:13

为什么Eclipse会让我变暖“资源泄漏:'in'永远不会关闭”以下代码?

public void readShapeData() {
        Scanner in = new Scanner(System.in);
        System.out.println("Enter the width of the Rectangle: ");
        width = in.nextDouble();
        System.out.println("Enter the height of the Rectangle: ");
        height = in.nextDouble();
java eclipse input resources memory-leaks
13个回答
54
投票

因为您没有关闭扫描仪

in.close();

2
投票
// An InputStream which is typically connected to keyboard input of console programs

Scanner in= new Scanner(System.in);

上面的行将使用参数System.in调用Scanner类的构造函数,并将返回对新构造的对象的引用。

它连接到连接到键盘的输入流,因此现在在运行时您可以使用用户输入来执行所需的操作。

//Write piece of code 

要消除内存泄漏 -

in.close();//write at end of code.

1
投票

扫描仪应该关闭。关闭读者,流......以及这类对象以释放资源和消除内存泄漏是一种很好的做法。并在finally块中执行此操作以确保即使在处理这些对象时发生异常也将其关闭。


0
投票
Scanner sc = new Scanner(System.in);

//do stuff with sc

sc.close();//write at end of code.

-1
投票
in.close();
scannerObject.close(); 

它将关闭Scanner并关闭警告。


44
投票

正如其他人所说,你需要在IO类上调用'close'。我将补充一点,这是一个使用try - finally块而没有捕获的优秀位置,如下所示:

public void readShapeData() throws IOException {
    Scanner in = new Scanner(System.in);
    try {
        System.out.println("Enter the width of the Rectangle: ");
        width = in.nextDouble();
        System.out.println("Enter the height of the Rectangle: ");
        height = in.nextDouble();
    } finally {
        in.close();
    }
}

这可确保您的扫描仪始终处于关闭状态,从而确保正确的资源清理。

同样,在Java 7或更高版本中,您可以使用“try-with-resources”语法:

try (Scanner in = new Scanner(System.in)) {
    ... 
}

8
投票

您需要在in.close()块中调用finally以确保它发生。

从Eclipse文档中,这就是为什么它标记这个特定问题(强调我的):

实现接口java.io.Closeable(自JDK 1.5以来)和java.lang.AutoCloseable(自JDK 1.7以来)的类被认为代表外部资源,当不再需要它们时,应使用close()方法关闭它们。

Eclipse Java编译器能够分析使用此类型的代码是否符合此策略。

...

编译器将使用“资源泄漏标记[违规]:'流'永远不会关闭”。

完整的解释here


6
投票

它告诉您需要关闭使用System.inScanner.close()上实例化的扫描仪。通常每个读者都应该关闭。

请注意,如果您关闭System.in,您将无法再次阅读它。你也可以看看Console课程。

public void readShapeData() {
    Console console = System.console();
    double width = Double.parseDouble(console.readLine("Enter the width of the Rectangle: "));
    double height = Double.parseDouble(console.readLine("Enter the height of the Rectangle: "));
    ...
}

4
投票

如果您使用的是JDK7或8,则可以将try-catch与资源一起使用。这将自动关闭扫描程序。

try ( Scanner scanner = new Scanner(System.in); )
  {
    System.out.println("Enter the width of the Rectangle: ");
    width = scanner.nextDouble();
    System.out.println("Enter the height of the Rectangle: ");
    height = scanner.nextDouble();
  }
catch(Exception ex)
{
    //exception handling...do something (e.g., print the error message)
    ex.printStackTrace();
}

3
投票

当你完成它时,你应该对你的扫描仪进行close

in.close();

2
投票

通常,处理I / O的类的实例应在完成后关闭。所以在代码的最后你可以添加in.close()


2
投票
private static Scanner in;

我通过声明为私有静态Scanner类变量来修复它。不知道为什么修复它,但这是eclipse推荐我做的。


2
投票

添加private static Scanner in;并不能解决问题,它只会清除警告。使扫描仪静止意味着它永远保持打开状态(或直到课程被卸载,这几乎是“永远”)。编译器不再给你任何警告,因为你告诉他“永远保持打开”。但这不是你真正想要的,因为一旦你不再需要它们就应该关闭资源。

HTH,曼弗雷德。

© www.soinside.com 2019 - 2024. All rights reserved.