简体   繁体   中英

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. 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. 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. Generally speaking leaks appear when you hold reference to the 'big' objects like activity, and because of that it can not be destroyed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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