小编Dan*_*l P的帖子

为什么对服务对象有静态的弱引用?

我在下面遇到了这个android代码。有没有在服务中创建静态弱引用对象以获取其引用的用例?我知道静态变量不适合垃圾回收。通常,创建任何静态变量的弱引用是否会更改其gc属性?

例如:

private static WeakReference<MyService> mService;
public static MyService getInstance(){
    if(mService != null){
          return mService.get();
    } 
    return null;
}
Run Code Online (Sandbox Code Playgroud)

在我的onCreate中

public void onCreate(){
   super.onCreate();
   mService = new WeakReference<MyService>(this);
}
Run Code Online (Sandbox Code Playgroud)

java service android garbage-collection finalize

5
推荐指数
1
解决办法
1876
查看次数

标签 统计

android ×1

finalize ×1

garbage-collection ×1

java ×1

service ×1