java 如何销毁java对象?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11404964/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to destroy java objects?
提问by manix
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 Observerand MVC patterns.
好吧,我开发了一个使用多个对象关系的 Java 应用程序,这些关系使内存使用成本过高。我没有管理 java 内存的经验,因为应用程序设计使得销毁对象和重新利用先前清除的空间变得困难。例如,我正在使用观察者和 MVC 模式。
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
如果对象无法从任何活动线程或任何静态引用访问,则该对象有资格进行垃圾收集或 GC
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?
根据这个上下文,当有多个引用时如何处理对象销毁?或者当您对彼此有复杂的引用时,我需要如何管理内存?
回答by haylem
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.
如果局部变量没有被“链接”到其他任何东西(添加到集合、复合等......) . 对于 UI 对象,在对象图方面确实很难推理,请确保正确处理它们或阅读文档以确保它们会自然处理。
"Leave [GC] Alone!!"
“让 [GC] 一个人待着!!”
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.
您无法“管理”内存。您可以简单地管理参考。这个想法是通过简单地不引用它们来“严格”你与对象的连接。然后它们会一直存在于内存中,直到 GC 消灭它们。
Do not attempt to mess with the GC to force it to do things. It's a rather clever beast, and while you can tryto instruct it to react to some requestsexplicitly - it might ignore you - it's usually a bad idea: do not invoke the GC explicitly, avoid finalizers and explicit nullingif you don't understand their implications.
不要试图弄乱 GC 来强迫它做事。这是一个相当聪明的野兽,虽然您可以尝试指示它显式地对某些请求做出反应- 它可能会忽略您 - 这通常是一个坏主意:不要显式调用 GC,如果您不理解,请避免终结器和显式清零它们的含义。
Note to answer your comment
注意回答你的评论
Simply nulling areference 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.
也许这听起来很乏味,但是如果您从手动管理内存的语言(C 或 C++,以命名最明显的 2 个引用)来考虑它,则指向动态分配对象的释放和空指针确实会破坏它们,但您仍然需要从列表(或任何容器)中删除该元素,否则它们看起来就像空桶一样指向空指针。
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
回答by emory
The whole point of java garbage collection is that you don't have to do anything. Garbage collection is done for you.
Java垃圾收集的全部意义在于您不必做任何事情。垃圾收集已为您完成。
回答by Cuga
Assign every reference you want the GC to collect to null
.
将您希望 GC 收集的每个引用分配给null
。
回答by Martinsos
What you could do is make one intermediate class. For example, if you have instance of class A, for which you have a lot of references and you want to remove it but a lot of references makes it difficult, you can do the following: create instance of class B that contains nothing other than reference to instance of class A (like some kind of proxy). Now you will have a lot of references to instance of class B but just one reference to instance of class A, which you can easily remove and garbage collector will collect instance of class A.
你可以做的是做一个中级班。例如,如果你有一个类 A 的实例,你有很多引用,你想删除它,但很多引用使它变得困难,你可以执行以下操作:创建类 B 的实例,除了对 A 类实例的引用(如某种代理)。现在您将有很多对 B 类实例的引用,但只有一个对 A 类实例的引用,您可以轻松删除它们,垃圾收集器将收集类 A 的实例。
Image shows difference when using proxy (instance of class B): Now only one reference has to be removed.
图像显示了使用代理时的不同(B 类实例):现在只需要删除一个引用。
回答by Tom Hawtin - tackline
For the most part, GC will do it's magic in good time.
在大多数情况下,GC 会及时发挥作用。
You can have a situation whereby, say, a view is observing a model and you want to ditch the view but keep the model. In that case you will need to remember the observer callback objects, and remove them when you discar the view. You don't necessarily have to have special fields for each observer - a set of tasks that unregister a callback each will be fine. Or, more complexly, you can have a layer of transient indirection over the model which unzips from the underlying one. I suggest avoiding weird stuff with weak references of one sort or another.
您可能会遇到这样一种情况,例如,一个视图正在观察一个模型,而您想放弃该视图但保留该模型。在这种情况下,您需要记住观察者回调对象,并在丢弃视图时将其删除。您不必为每个观察者设置特殊字段 - 一组取消注册回调的任务就可以了。或者,更复杂的是,您可以在从底层解压缩的模型上设置一个瞬态间接层。我建议避免使用一种或另一种弱引用的奇怪东西。
In case where you may have finalisers (or require some kind of weak map eviction), such as presumably with a java.awt.Frame, You may want a layer of indirection between the resource and the memory hog which can simply be nulled out.
如果您可能有终结器(或需要某种弱映射驱逐),例如可能使用 java.awt.Frame,您可能需要资源和内存猪之间的间接层,可以简单地将其清除。