简体   繁体   English

如何保持Java对象的存活?

[英]How to keep alive Java objects?

I'm just thinking about a way of keeping away Java objects from garbage collection even if it is not being referred for a reasonable amount of time. 我正在考虑一种方法来防止垃圾收集中的Java对象,即使它没有在合理的时间内被引用。

How to do that? 怎么做?

Have a static container in your main class that you put a reference to the objects in. It can be a Map , List , whatever. 在主类中有一个static容器,您可以在其中放置对象的引用。它可以是MapList等等。 Then you'll always have a reference to the object, and it won't be reclaimed. 然后你将总是有一个对象的引用,它将不会被回收。 ( Why you would want to do this is another question...) 为什么你想这样做是另一个问题...)

Which is to say: As long as a reachable reference to an object exists, it will not be garbage-collected. 也就是说:只要存在对象的可访问引用,就不会对其进行垃圾回收。 If your code has a reference and tries to use it, the object will be there. 如果您的代码有引用并尝试使用它,那么该对象就在那里。 You don't have to do anything special to make that happen (nor should you). 你没有做任何特别的事情来实现这一点(你也不应该)。 (A reachable reference means that the reference is from something that is, itself, reachable from something other than the things it references. Put more simply: The GC understands about circular references and so can clean up A and B even if they refer to each other, as long as nothing else refers to either of them.) 可达参考意味着引用来自某些东西,它本身可以从它引用的东西以外的其他东西到达。更简单地说:GC理解循环引用,因此即使它们引用每个引用也可以清理A和B.另外,只要没有其他任何内容指的是其中任何一个。)

[...] even if it is not being referred for a reasonable amount of time . [...]即使它没有被推荐一段合理的时间

If there's any chance what so ever that an object will be accessed in the future, the object will not be garbage collected . 如果有什么那么一个对象会在将来访问的任何机会 ,对象将不会被垃圾收集

This is due to the fact that if you have a reference to the object, it won't be garbage collected, and if you don't have a reference to the object, there's no way you will be able to access it ever. 这是因为如果你有一个对象的引用,它将不会被垃圾收集,如果你没有对该对象的引用,你将永远无法访问它。

In other words, an ordinary reference will never mystically turn into a null just because the garbage collector observed that the object hadn't been accessed for a long time and thought it was time to reclaim it. 换句话说,普通引用永远不会神秘地转变为null只是因为垃圾收集器发现该对象长时间未被访问并且认为是时候回收它了。

You could also create a static instance of the object in its own class. 您还可以在自己的类中创建对象的静态实例。 For example if it is a singleton, having a static instance field in the class. 例如,如果它是单例,则在类中具有静态实例字段。

There are mechanisms that will hold a reference to an object, but still allow it to be garbage collected, if there are no other references otherwise. 有一些机制可以保存对象的引用,但如果没有其他引用则仍然允许它被垃圾回收。

Look at WeakReference and SoftReference. 看看WeakReference和SoftReference。 If you want more details on reachability as far as the jvm is concerned, see: 如果您想了解有关jvm的可达性的更多详细信息,请参阅:

http://download.oracle.com/javase/6/docs/api/java/lang/ref/package-summary.html#reachability http://download.oracle.com/javase/6/docs/api/java/lang/ref/package-summary.html#reachability

As far as time is concerned, the garbage collector doesn't know or care about how often an object is used. 就时间而言,垃圾收集器不知道或不关心对象的使用频率。 Either another object has a reference to the target (even if it's not using it), or there are no references to the target. 另一个对象都有对目标的引用(即使它没有使用它),或者没有对目标的引用。 If there are no references to the object, it could never be used again, and will eventually be freed (even if you wanted to, you couldn't obtain a reference to the object again) The longer-living an object is, the longer it takes for the jvm to free it, due to generational garbage collection. 如果没有对该对象的引用,它将永远不会被再次使用,并且最终将被释放(即使您想要,也无法再次获得对该对象的引用)对象的生命周期越长,对象的生命周期越长由于世代垃圾收集,jvm需要释放它。

I'm just thinking about a way of keeping away Java objects from garbage collection even if it is not being referred for a reasonable amount of time. 我正在考虑一种方法来防止垃圾收集中的Java对象,即使它没有在合理的时间内被引用。

On the face of it, this question doesn't make sense. 从表面上看,这个问题没有意义。 If an object is not referenced (or more a precisely, if it is not reachable) then the garbage collector will collect it. 如果没有引用对象(或者更准确地说,如果它不可访问),那么垃圾收集器将收集它。 If you want to prevent an object from being garbage collected then you have to make sure that it is reachable. 如果要防止对象被垃圾回收,则必须确保它是可访问的。 (Actually, it has to be strongly reachable to guarantee that it won't be GC'ed : see @Austen Holmes answer and the page that he references.) (实际上,它必须能够很好保证它不会被GC控制:请参阅@Austen Holmes的答案和他引用的页面。)

But actually, I think that you are confusing "refered" / referenced / reachable with accessed or used; 但实际上,我认为你对访问或使用的“引用”/引用/可访问感到困惑; ie with the act of accessing a field or call a method of the object. 即通过访问字段或调用对象的方法的行为 If that is what you are asking, then I can assure that the garbage collector neither knows or cares whether your code has recently accessed / used an object. 如果这就是您要求的,那么我可以确保垃圾收集器既不知道也不关心您的代码最近是否访问过/使用过对象。

The reachability criteria is actually about whether your code could access the object at some point in the future, and (therefore) whether the object needs to be kept so that this will work. 可达性标准实际上是关于您的代码是否可以在将来的某个时刻访问该对象,以及(因此)是否需要保留该对象以使其可用。 The reachability rule means that if an object could be accessed, then it will be kept. 该可达性规则意味着,如果一个对象可以访问,那么它被保留。 It makes no difference how long it was since you last accessed it. 自您上次访问它以来,它没有任何区别。

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

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