在循环内定义java对象,是否需要使用null来释放内存?
如果我有一个循环并在其中创建一个新对象,
for ( int i ; i < 10 ; i++)
{
MyObject obj = new MuObject();
obj.use();
}
我是否需要在循环的开头或结尾处说 obj = null 以释放该对象使用的内存,或者通过使用“new”将该对象发送到 GC ?我可以从内存使用方面看到这一点吗?
更新:所以如果我有大对象和长循环,我应该将对象分配为 null 还是 no ?
If I have a loop and create a new object inside it
for ( int i ; i < 10 ; i++)
{
MyObject obj = new MuObject();
obj.use();
}
Do I need to say obj = null, inside the loop at the beginning or end to release memory used by that object , or by using "new" that object will be send to GC ? and can I see this in terms of memory usage ?
update : so in case I have big object and long loop , should I assign the object to null or no ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
检查这个: http://javarevisited.blogspot.com/2011/ 04/garbage-collection-in-java.html
“如果某个对象无法从任何活动线程或任何静态线程访问,则该对象有资格进行垃圾收集或 GC参考文献”。循环结束后,您在循环内创建的对象没有任何外部引用指向它们,并且可以进行垃圾回收。
编辑:
如果您想查看内存使用情况,可以使用具有此类功能的 IDE 来分析您的应用程序。例如,NetBeans 有一个很好的界面,可以显示对象分配的实时内存使用情况。
编辑2:
“如果我有大对象和长循环,我应该将对象分配为 null 或 no 吗?”
不,您不需要这样做。一旦循环的一次迭代完成,就不会再有对该迭代中创建的任何对象的活动引用,因此循环长或短并不重要。
Check this: http://javarevisited.blogspot.com/2011/04/garbage-collection-in-java.html
"An Object becomes eligible for Garbage collection or GC if its not reachable from any live threads or any static references". After the loop ends, the objects that you created inside the loop do not have any external references pointing to them and are eligible for garbage collection.
EDIT:
If you want to see memory usage, you can profile your application using an IDE that has such a feature. For example, NetBeans has a nice interface that shows live memory usage for object allocation.
EDIT 2:
"so in case I have big object and long loop , should I assign the object to null or no ?"
No, you do not need to do this. Once one iteration of the loop is complete, there are no active references to any objects created in that iteration so it does not matter that you have a long or short loop.
都不是,真的。
new
仅构造新对象。当没有对该对象的引用时,例如超出范围(即不在循环块中),它将有资格进行垃圾收集。请注意,Java 的垃圾收集器不会立即收集对象 - 它会在需要时分批进行收集。我建议查看 VisualVM,包括您的 JDK。它有一个内存视图和一个通过插件的垃圾收集器视图。
请注意,您不能依赖操作系统的“使用中”计数 - Java 堆很少会收缩,尤其是在没有任何主要集合的情况下。
Neither, really.
new
only constructs new objects. When there are no references to the object, such as falling out of scope (i.e., not in the loop block), it will be eligible for garbage collection. Note that Java's garbage collector does not immediately collect objects - it does it in batches when it feels that it is required.I would suggest looking at VisualVM, including with your JDK. It has a memory view, and a garbage collector view through a plugin.
Note that you cannot rely on the operating system "in use" count - the Java heap will rarely shrink especially if there aren't any major collections.
不,您不需要将 obj 设置为 null。当下一个循环迭代重新分配它时,先前的引用将是垃圾(除非有其他东西指向它)并且有资格进行清理。这就是自动垃圾收集的意义所在。
然而,在某些情况下,您必须注意控制内存的因素。如果您设置了静态对象指针,它可能永远不会被清理。 (它确实不是垃圾,因为它有一个实时引用)。一个常见的问题是缓存;它可能保留着永远不会被清理的陈旧数据。
Nope, you don't need to set obj to null. When it is reassigned by the next loop iteration the previous reference will be garbage (unless something else points to it) and eligible for cleanup. That's the point of automatic garbage collection.
However there are some cases when you have to watch for things to control memory. If you have an static object pointer set it may never get cleaned up. (it's really not garbage since it has a live reference). One common issue is caches; it may hold unto old stale data that never gets cleaned up.
即使您调用 GC,它也不会立即执行,但为了良好的实践,您可以这样做。
对于内存管理,您可以查看 IDE 的功能,例如 netbeans、Eclipse 等。
As it is even if you call the GC it will not do it immediately, but for good practice you can do so.
For memory management you can look into the features of IDE's like netbeans, Eclipse, etc.