JVM不会释放程序退出时程序员未明确关闭的所有资源 [英] Doesn't the JVM release all the resources that are not explicitly closed by the programmer on program exit

查看:50
本文介绍了JVM不会释放程序退出时程序员未明确关闭的所有资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直听说Java中的资源在使用后必须关闭,否则这些资源将被耗尽.对于使用很少资源(例如1个或2个文件读取器/缓冲读取器以及全部)的小型程序,这真的是一个值得关注的问题吗?JVM不会跟踪程序正在使用的资源吗?程序退出后,它不会释放所有这些资源吗?如果没有,为什么即使程序退出后,JVM仍会阻止这些资源被阻塞?

I've always heard that resources in java must be closed after use or these resources will get exhausted. Is it really a matter of concern for small programs that use very few resources(like 1 or 2 file readers/ buffered readers and all)? Doesn't the JVM keep track of the resources being used by a program? And wouldn't it release all those resources once the program exits? If not, why would the JVM keep these resources blocked even after the program exit?

推荐答案

这些资源确实在正常的JVM退出时关闭.但是,您可能并不总是知道如何调用方法,并且可能被另一个程序员在外部调用2000次,并且这些资源将开始堆积.

These resources are indeed closed upon a normal JVM exit. However, you may not always know how your method is being called, and it could perhaps be called 2000 times externally by another programmer, and those resources will start to stack up.

此外,某些非主流操作系统可能会遇到以下问题:如果JVM异常停止(通过 Runtime.getRuntime().halt()或内部发生重大内部错误/不一致)JVM),则资源可能保持打开状态(由于未运行清除代码),直到重新引导或手动释放之前,这些资源可能无法使用.即使在主流系统上,插座也可以保持打开状态数分钟.

In addition, certain non-mainstream OSs may run into the issue that if the JVM were to halt abnormally(via Runtime.getRuntime().halt() or a significant internal error/inconsistency within the JVM) then resources could remain open(due to cleanup code not being run), potentially unusable until rebooted or manually released. Even on mainstream systems sockets could remain open for multiple minutes.

这篇关于JVM不会释放程序退出时程序员未明确关闭的所有资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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