如何销毁java对象? [英] How to destroy java objects?
问题描述
好吧,我已经开发了一个使用多个对象关系的java应用程序,这使得内存使用过于昂贵。我没有管理Java内存的经验,因为应用程序设计使得难以销毁对象并重新利用先前清除的空间。例如,我正在使用:不要明确调用GC ,避免终结器和显式清零 如果您不了解其含义。
注意回答您的评论
简单地将已添加到多个集合或组合的对象的引用引用将不符合收集条件。通过这样做,你只会删除一个参考
您需要将此对象从所有引用它的列表或容器中移除(基本上,使它们忘记此对象)。一旦没有对象仍然记住或有一个链接到您创建的对象,它将成为垃圾收集器图中的一个孤独的项目,这使得它成为删除的候选对象。
也许这听起来很乏味,但如果你从手动管理内存的语言中想到它(C或C ++,要命名最多2个明显的引用),那么释放和空指针指向动态分配的对象确实会销毁它们,但是您仍然需要从列表(或任何容器)中删除该元素,否则它们将显示为空桶以空指针。
进一步阅读
- Garbage Collection (特别是关于可达性)
- (日期,但非常好)
- Java理论与实践:垃圾收集简史 - 垃圾收集如何工作? ://javarevisited.blogspot.fr/2011/04/garbage-collection-in-java.htmlrel =nofollow noreferrer>垃圾收集如何在Java中工作
- 处理Java程序中的内存泄漏
- 从Java代码到Java堆
- 这些SO问题:
- Java内存解释 (提供很多额外的信息和链接)
Java内存管理最佳实践
Well, I have developed a java application using several objects relationships that make the memory usage too expensive. I have no experience managing the java memory because the application design make it difficult to destroy objects and re utilize the space previously cleared. For example, I am using Observer and MVC patterns.
So, the theory say that..
An Object becomes eligible for Garbage collection or GC if its not reachable from any live threads or any static reference
In other words you can say that an object becomes eligible for garbage collection if its all references are null.
But, in my short experience, has be too difficult for me destroy all references from objects that I want to remove from memory (for example, when a frame is closed) when you have a scenario like mine, where you don't know how many references to your classes exists.
According to this context, how can I deal with object destruction when there are multiple references to it? or how do I need to manage the memory when you have a complex references to each others?
Keeping Track
According this context, how can I deal with object destruction when there are multiples references to it?
By making sure these references are not needed anymore.
If you isolate them, even in a big isolated graph of unused objects not connected anymore to your main progam, then they are all eligible for garbage collection.
Local variables which have reached the end of their scope will be eligible for garbage collection (and so will their contained) objects, if they have not been "linked" to anything else (added to a collection, ac omposite, etc...). For UI objects, which can indeed be difficult to reason with in terms of object graphs, make sure to dispose of them correctly or to read the documentation to make sure that they would naturally disposed of.
"Leave [GC] Alone!!"
or how do I need to manage the memory when you have a complex references to each others?
You can't "manage" the memory. You can simply manage references. The idea is to "severe" your connections to your objects by simply not having references to them. They then live up in memory until the GC exterminates them.
Do not attempt to mess with the GC to force it to do things. It's a rather clever beast, and while you can try to instruct it to react to some requests explicitly - it might ignore you - it's usually a bad idea: do not invoke the GC explicitly, avoid finalizers and explicit nulling if you don't understand their implications.
Note to answer your comment
Simply nulling a reference to an object that has been added to several collections or composites will not make it eligible for collection. By doing this, you'd have only nulled one reference.
You do need to remove this object from all the lists or containers that have a reference to it (basically, making them "forget" about this object). Once no objects still "remembers" or has a "link" to your created object, it becomes a lonely item in the Garbage Collector's graph, which makes it a candidate for deletion.
Maybe it sounds tedious, but if you think of it from a language where you manually manage memory (C or C++, to name the most 2 obvious references), free-ing and null-ing pointers to your dynamically allocated objects would indeed destroy them, but you'd still need to remove the element from the lists (or any containers) or they would appear like empty buckets to a null pointer.
Further Reading
- Garbage Collection (especially the section on reachability)
- Sun Microsystems's Whitepaper on in the Java HotSpot Virtual Machine (dated, but very good)
- Java Theory and Practice: A Brief History of Garbage Collection - How does Garbage Collection Work?
- Java Theory and Practice: Garbage Collection Performance
- How Garbage Collection Works in Java
- Handling Memory Leaks in Java Programs
- From Java Code to Java Heap
- These SO questions on:
- Java Memory Explained (provides a lot of additional info and links)
- Java Memory Management Best Practices
这篇关于如何销毁java对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!