简体   繁体   English

如何销毁java对象?

[英]How to destroy java objects?

Well, I have developed a java application using several objects relationships that make the memory usage too expensive.好吧,我开发了一个使用多个对象关系的 Java 应用程序,这些关系使内存使用成本过高。 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.我没有管理 java 内存的经验,因为应用程序设计使得销毁对象和重新利用先前清除的空间变得困难。 For example, I am using Observer and MVC patterns.例如,我正在使用观察者和 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?或者当您对彼此有复杂的引用时,我需要如何管理内存?

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 对象,在对象图方面确实很难推理,请确保正确处理它们或阅读文档以确保它们会自然处理。

JVM 中引用计数的简化视图

"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.不要试图弄乱 GC 来强迫它做事。 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 .这是一个相当聪明的野兽,虽然您可以尝试指示它显式地对某些请求做出反应 - 它可能会忽略您 - 这通常是一个坏主意不要显式调用 GC如果您不理解,请避免终结器和显式清零它们的含义


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 .通过这样做, 您只会将一个 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进一步阅读

The whole point of java garbage collection is that you don't have to do anything. Java垃圾收集的全部意义在于您不必做任何事情。 Garbage collection is done for you.垃圾收集已为您完成。

将您希望 GC 收集的每个引用分配给null

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).例如,如果你有一个类 A 的实例,你有很多引用,你想删除它,但很多引用使它变得困难,你可以执行以下操作:创建类 B 的实例,除了对 A 类实例的引用(如某种代理)。 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.现在您将有很多对 B 类实例的引用,但只有一个对 A 类实例的引用,您可以轻松删除它们,垃圾收集器将收集类 A 的实例。

Image shows difference when using proxy (instance of class B): Now only one reference has to be removed.图像显示了使用代理时的不同(B 类实例):现在只需要删除一个引用。

在此处输入图片说明

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,您可能需要资源和内存猪之间的间接层,可以简单地将其清除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM