如何让Java对象保持活动状态?
我只是在考虑一种方法,让 Java 对象不被垃圾回收,即使它在合理的时间内没有被引用。
怎么做呢?
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.
How to do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在您的主类中有一个静态容器,您可以在其中放置对对象的引用。它可以是 Map、List 等。那么你将始终拥有对该对象的引用,并且它不会被回收。 (为什么你想要这样做是另一个问题......)
也就是说:只要存在对对象的可达引用,它就不会是垃圾-集。如果您的代码有一个引用并尝试使用它,该对象就会在那里。你不需要做任何特别的事情来实现这一点(你也不应该)。 (可到达引用意味着该引用本身可以从它所引用的对象以外的其他对象到达。更简单地说:GC 理解循环引用,因此可以清理 A 和B 即使它们互相引用,只要没有其他内容引用它们中的任何一个。)
Have a
static
container in your main class that you put a reference to the objects in. It can be aMap
,List
, whatever. 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.)
如果有任何机会某个对象将来再访问时,该对象不会被垃圾回收。
这是因为,如果您拥有对该对象的引用,则它不会被垃圾收集,而如果您没有对该对象的引用,则您将无法进行垃圾回收。能够访问它。
换句话说,普通引用永远不会仅仅因为垃圾收集器观察到该对象很长时间没有被访问并认为是时候回收它而神秘地变成
null
。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.您还可以在其自己的类中创建该对象的静态实例。例如,如果它是单例,则在类中具有静态实例字段。
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.
有一些机制可以保存对对象的引用,但如果没有其他引用,仍然允许对其进行垃圾收集。
看看弱引用和软引用。如果您想了解有关 jvm 的可访问性的更多详细信息,请参阅:
http://download.oracle.com/javase/6/docs/api/java/lang/ref/package-summary.html#reachability
就时间而言担心,垃圾收集器不知道或关心某个对象的使用频率。另一个对象有对目标的引用(即使它没有使用它),或者没有对目标的引用。如果没有对该对象的引用,则该对象永远无法再次使用,并且最终将被释放(即使您愿意,也无法再次获得该对象的引用)对象的寿命越长,它的寿命就越长由于分代垃圾回收,jvm 需要释放它。
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. If you want more details on reachability as far as the jvm is concerned, see:
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.
从表面上看,这个问题没有意义。如果一个对象未被引用(或者更准确地说,如果它不可访问),那么垃圾收集器将收集它。如果你想防止一个对象被垃圾收集,那么你必须确保它是可访问的。 (实际上,它必须强可达才能保证它不会被GC:请参阅@Austen Holmes的回答和他引用的页面。)
但实际上,我认为你很困惑“引用”/引用/可访问或使用;即访问字段或调用对象方法的行为。如果这就是您所问的,那么我可以保证垃圾收集器既不知道也不关心您的代码最近是否访问/使用了对象。
可达性标准实际上是关于您的代码是否可以在将来的某个时刻访问该对象,以及(因此)是否需要保留该对象才能使其正常工作。可达性规则意味着如果一个对象可以被访问,那么它将会被保留。距离您上次访问它已有多长时间都没有什么区别。
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.)
But actually, I think that you are confusing "refered" / referenced / reachable with accessed or used; i.e. 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.