简体   繁体   English

Singleton object 中的包装器变量能否在 Singleton 不是 Z37A6259CC0C1DAE299A7866648 时收集垃圾?

[英]Can a wrapper variable within a Singleton object get garbage collected while Singleton is not null?

In this Singleton instance, can this private Long severDate become garbage collected when the singleton instance is still not null?在这个 Singleton 实例中,当 singleton 实例仍然不是 null 时,这个private Long severDate成为垃圾回收?

I encountered this is in an Android app.我在 Android 应用程序中遇到了这个问题。 Wondering Android OS somehow freeing unused individual memory values.想知道 Android 操作系统以某种方式释放未使用的单个 memory 值。

public class DataHolder {

    private static DataHolder self;

    private Long severDate; 
    private int numb;

    public static synchronized DataHolder getInstance() {
            if (self == null) {
                self = new DataHolder();
            }
            return self;
    }
 
}

Until "Long severDate" has a references to it, GC wont be deleting it.在 "Long severDate" 引用它之前,GC 不会删除它。 Now it all depends on the useCase whether u have created the instance of this singleton class in some classes or not.现在这一切都取决于你是否在某些类中创建了这个 singleton class 的实例。 PS: In android make sure that u create the obj of this class in onCreate method. PS:在 android 中确保你在 onCreate 方法中创建了这个 class 的 obj。

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

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