java 垃圾收集器何时会擦除使用单例模式的对象实例?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4127458/
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
When would the garbage collector erase an instance of an object that uses Singleton pattern?
提问by Julio
When would the garbage collector erase an instance of an object that uses Singleton pattern?
垃圾收集器何时会擦除使用单例模式的对象实例?
Does an object hang around any longer than a regular object?
物体是否比普通物体停留的时间更长?
How can you manually force deletion/garbage collection of an object in Java?
如何在 Java 中手动强制删除/垃圾收集对象?
Thanks.
谢谢。
回答by Jon Skeet
There's a static reference to a singleton, so it won't be eligible for garbage collection until the classloader is eligible for garbage collection.
有一个对单例的静态引用,因此在类加载器有资格进行垃圾收集之前,它没有资格进行垃圾收集。
You can't force any object to be garbage collected; you can requestthat the garbage collector runs with System.gc()
but it's only a request.
你不能强制任何对象被垃圾回收;您可以请求垃圾收集器运行,System.gc()
但这只是一个请求。
If you really want to make a "singleton" eligible for garbage collection, you'd probably want to have a method to set the static variable to null (and hope that nothing else had taken a copy of the reference). Obviously the next time anyone asked for an instance, it would need to be recreated... at which point it's not really a singleton, of course.
如果你真的想让一个“单例”有资格进行垃圾回收,你可能想要一个方法来将静态变量设置为 null(并希望没有其他东西复制了引用)。显然,下次有人请求实例时,它需要重新创建……当然,此时它并不是真正的单身人士。
回答by Aniket Thakur
There are special objects in Java called GC roots. They are always reachable and so are the objects that can be reached from these roots. These GC roots can never be garbage collected and so do the objects that are reachable from these roots. In Java static variables form GC roots.
Java 中有一些特殊的对象称为 GC 根。它们总是可以到达的,从这些根可以到达的对象也是如此。这些 GC 根永远不会被垃圾收集,从这些根可到达的对象也是如此。在 Java 中,静态变量形成 GC 根。
Singleton class has a static reference to the instantiated singleton object and hence it will never be garbage collected unless ofcourse as Jon Skeet as stated that the context that loaded this class (class loader) is itself eligible for garbage collection in which case that static reference will not longer be a GC root.
单例类有一个对实例化单例对象的静态引用,因此它永远不会被垃圾收集,除非当然就像 Jon Skeet 所说的那样,加载这个类的上下文(类加载器)本身就有资格进行垃圾收集,在这种情况下,静态引用将不再是 GC 根。
I think this was a bug prior to Java 1.2 when singleton instance could be garbage collected if there was no global reference to it but that was fixed in Java 1.2 and only way now it can be eligible for garbage collection is if the class loader that loaded this class was garbage collected.
我认为这是 Java 1.2 之前的一个错误,如果没有对它的全局引用,可以对单例实例进行垃圾收集,但在 Java 1.2 中已修复,现在唯一可以进行垃圾收集的方法是加载的类加载器这个类是垃圾收集的。
回答by Reese Moore
If you're keeping a static reference to it in your singleton class, then the reference count cannot drop to 0 and therefore it shouldn't ever get collected.
如果您在单例类中保留对它的静态引用,则引用计数不能降为 0,因此它不应该被收集。
回答by Peter Lawrey
static
fields can and do get GCed however this doesn't happen in a simple application.
static
字段可以并且确实得到 GC,但是这不会发生在简单的应用程序中。
The static fields are referenced by the Class and the Class is referenced by the ClassLoader. The ClassLoader is referenced by every class and every instance of that class.
静态字段由类引用,类由类加载器引用。ClassLoader 被每个类和该类的每个实例引用。
However in OSGi containers and application servers it is not unusual to drop every reference to an application or library and it's class loader. At this point the class loader and every static field can be GC-ed.
然而,在 OSGi 容器和应用程序服务器中,删除对应用程序或库及其类加载器的每个引用并不罕见。此时可以对类加载器和每个静态字段进行 GC 处理。