[quote=引用 24 楼 NoMasp 的回复:] 会,不过建议手动清理,比如说可能你忘了清理以后又用到它了,但已经被系统给清掉了
[quote=引用 13 楼 wozuiqiangdeaoyi 的回复:] [quote=引用 3 楼 Android_iPhone 的回复:] 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,因为即使发生了例外,这些代码也能被调用。
会,不过建议手动清理,比如说可能你忘了清理以后又用到它了,但已经被系统给清掉了
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,因为即使发生了例外,这些代码也能被调用。
[quote=引用 3 楼 Android_iPhone 的回复:] 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,因为即使发生了例外,这些代码也能被调用。
虽然Java有自动内存回收机制,但是如果是数据库连接、网络连接、文件操作等,不close是不会被回收的,属于不正确的代码。 也就是说,有close方法,必须得自己调用一下才行。 对于使用完了的对象来讲,Java不推荐使用类似于C++的析构函数来释放内存(C++中new完后得delete,Java中new完,使用后,将其置成null比较好),因为GC会调节最适当的时间来释放内存,在程序中滥用delete会降低Java程序的性能(但应该不会引发额外的错误)。
62,614
社区成员
307,327
社区内容
加载中
试试用AI创作助手写篇文章吧