垃圾收集器何时会删除使用单例模式的对象实例?
垃圾收集器何时会删除使用单例模式的对象实例?
一个物体比普通物体停留的时间更长吗?
如何在 Java 中手动强制删除/垃圾回收对象?
谢谢。
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?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
有一个对单例的静态引用,因此在类加载器符合垃圾回收条件之前,它不符合垃圾回收条件。
你不能强制任何对象被垃圾收集;您可以请求垃圾收集器使用
System.gc()
但这只是一个请求。如果您确实想让“单例”符合垃圾回收的条件,您可能希望有一个方法将静态变量设置为 null(并希望没有其他任何东西获取引用的副本)。显然,下次有人请求实例时,需要重新创建它......当然,此时它并不是真正的单例。
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 request that the garbage collector runs with
System.gc()
but it's only a request.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.
Java中有一些特殊的对象,称为GC根。它们始终是可到达的,从这些根可以到达的对象也是如此。这些 GC 根永远不会被垃圾回收,从这些根可到达的对象也是如此。在 Java 中,静态变量形成 GC 根。
Singleton 类具有对实例化单例对象的静态引用,因此它永远不会被垃圾收集,除非正如 Jon Skeet 所说,加载此类(类加载器)的上下文本身符合垃圾收集的条件,在这种情况下,静态引用将不再是 GC root。
此处有相关答案。
我认为这是 Java 1.2 之前的一个错误,如果没有全局引用,单例实例可能会被垃圾收集,但这在 Java 1.2 中得到了修复,现在它有资格进行垃圾收集的唯一方法是加载的类加载器这个类被垃圾收集了。
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.
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.
Relevant answer here.
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.
如果您在单例类中保留对它的静态引用,则引用计数不能降至 0,因此永远不应该收集它。
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.
static
字段可以并且确实会被GC,但是这不会发生在简单的应用程序中。静态字段由类引用,类由类加载器引用。 ClassLoader 被每个类和该类的每个实例引用。
然而,在 OSGi 容器和应用程序服务器中,删除对应用程序或库及其类加载器的所有引用并不罕见。此时,类加载器和每个静态字段都可以进行 GC。
static
fields can and do get GCed however this doesn't happen in a simple application.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.
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.