简体   繁体   English

Android线程内存泄漏

[英]Android thread memory Leak

I write to the shared preferences when Ever I get a certain broadcast msg from a service I have running. 当我从运行的服务中获取某个广播消息时,会写共享的首选项。 I want to know If this will cause a memory leak? 我想知道是否会导致内存泄漏? If so how can I fix it. 如果是这样,我该如何解决。 I will be required to run this code ever 20-25 min. 我将被要求每20-25分钟运行一次此代码。 Do the old thread Die? 旧线程死了吗?

if(Wifi_Connected)
{

    Thread thread2=  new Thread(new Runnable() {
                              @Override
                              public void run() {
                                  SharedPreferences.Editor e = sharedData.edit();
                                  e.putInt("Value",1);
                                  e.commit();
                              }
                          });

                    thread2.start();

}

No, it may cause some UI hiccups. 否,这可能会导致UI出现打ic。 Commit operation is very fast, so it will not hold outer class too long. 提交操作非常快,因此不会使外部类保持太长时间。 But you better use apply() method which performs operation in background thread. 但是最好使用apply()方法在后台线程中执行操作。 Generally speaking leaks appear when you hold reference to the 'big' objects like activity, and because of that it can not be destroyed. 一般来说,当您保留对诸如活动之类的“大”对象的引用时,就会出现泄漏,因此无法将其销毁。

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

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