“Resource leak: ‘sc‘ is never closed”的解决及解释

简介: “Resource leak: ‘sc‘ is never closed”的解决及解释

   Resource leak: 'sc' is never closed  是指编程过程中命名的“sc”这个Scanner对象没有关闭。

(一般不会造成代码无法运行,但会增加运行需要的内存)

解决方法:

       在设置输入内容的代码下一行输入 sc.close;

注: “sc”是在Scanner实例化中对象的命名。

    例如 Scanner b=new Scannner(System.in);   则输入 b.close;进行“b”这个Scanner对象的关闭。

        问题解决


为什么需要关闭这个所谓的Scanner对象呢?


       关闭Scanner是因为System.in在被第一次声明时会打开InputStream(指输入流)。


       注:上文第一次声明指的是Scanner sc=new Scanner(System.in);


               输入流可以简单理解为java编程中机器读取的输入内容。


       输入流在运行代码过程中非常占据内存,打开输入流后,机器为随时读取输入内容,一直占用部分内存。


       为了节省运行内存,调用Scanner.close ()来关闭输入流,致使节省内存。


相关文章
|
Web App开发 前端开发
【前端异常】Unchecked runtime.lastError: Could not establish connection. Receiving end does not exist.
【前端异常】Unchecked runtime.lastError: Could not establish connection. Receiving end does not exist.
750 0
Flutter之运行提示Could not update files on device: Connection closed before full header was received
Flutter之运行提示Could not update files on device: Connection closed before full header was received
753 0
|
3月前
|
XML 缓存 API
【Azure API 管理】使用APIM进行XML内容读取时遇见的诡异错误 Expression evaluation failed. Object reference not set to an instance of an object.
【Azure API 管理】使用APIM进行XML内容读取时遇见的诡异错误 Expression evaluation failed. Object reference not set to an instance of an object.
|
Linux 开发工具 Android开发
[√]leak tracer的stack address始终无法被addr2line识别
[√]leak tracer的stack address始终无法被addr2line识别
138 0
|
Unix Java Linux
Runtime.exec方法之获取process id
Runtime.exec方法之获取process id
216 0
在main函数中创建新对象时出错 No enclosing instance of type ooo is accessible. Must qualify the allocation with a
在main函数中创建新对象时出错 No enclosing instance of type ooo is accessible. Must qualify the allocation with a
在main函数中创建新对象时出错 No enclosing instance of type ooo is accessible. Must qualify the allocation with a
|
消息中间件 Java Kafka
Kafka Windows运行错误: Native memory allocation (mmap) failed to map 1073741824 bytes for Failed to comm
Kafka Windows运行错误: Native memory allocation (mmap) failed to map 1073741824 bytes for Failed to comm
696 0
|
Web App开发 Java
错误:Reference file contains errors http://www.ibm.com/webservices/xsd/j2ee_web_services_client_1_1.xsdv......
引用:http://topic.csdn.net/u/20070125/18/8d676afd-868a-4c2d-9a1b-07eed6b5683d.html   6楼 xsi:schemaLocation= "http://java.
1544 0
Resource leak: 'sc' is never closed 资源泄漏:'sc'永远不会关闭
我们使用Scanne类时总是出现警告的解决方法: Scanner sc = new Scanner(System.in); 出现问题如下图所示: 解决问题如下图所示: 我的GitHub地址:https://github.
2377 0