Garbage collection will only free the resources within the JVM. The resources assigned to the VM by the operating system will still be reserved. I've seen several poorly written applications run out of resources due to poorly handled database connections, sockets and files. Rule of thumb: if it has a close() method, invoke it when you are done with it. And invoke it in a finally block so it will be called even if an exception occurs.大体上翻译一下:垃圾回收机制仅在Java虚拟机所控制的范围内释放资源。 对于类似于数据库连接、socket以及文件操作等, 如果有close方法,在你完成任务后执行它 并且最好在finally块内做close,因为即使发生了例外,这些代码也能被调用。
The resources assigned to the VM by the operating system will still be reserved.
I've seen several poorly written applications run out of resources due to poorly handled database connections, sockets and files. Rule of thumb:
if it has a close() method, invoke it when you are done with it.
And invoke it in a finally block so it will be called even if an exception occurs.大体上翻译一下:垃圾回收机制仅在Java虚拟机所控制的范围内释放资源。
对于类似于数据库连接、socket以及文件操作等,
如果有close方法,在你完成任务后执行它
并且最好在finally块内做close,因为即使发生了例外,这些代码也能被调用。
也就是说,有close方法,必须得自己调用一下才行。对于使用完了的对象来讲,Java不推荐使用类似于C++的析构函数来释放内存(C++中new完后得delete,Java中new完,使用后,将其置成null比较好),因为GC会调节最适当的时间来释放内存,在程序中滥用delete会降低Java程序的性能(但应该不会引发额外的错误)。
确保某个对象不被引用、使用,GC就会自动回收,释放内存
阿帕奇的开源框架里close都是在finalize中的,这不就已经默认了即使没有close,gc让然会清理?
这不就默认了,gc不会自动清理吗!