Not enough memory? Talk about the most common memory leaks in Android development and how to solve them

Not enough memory? Talk about the most common memory leaks in Android development and how to solve them

[[140732]]

Have you ever felt that the available memory of your Android phone keeps decreasing after you turn it on? A big part of the reason is because of memory leakage. This leaked memory cannot be recycled, so the available memory is getting less and less;

What is a memory leak? GC cannot recycle the object that should have been recycled, and this object causes a memory leak.

1. Forget to recycle Bitmap after use, because the implementation of Bitmap calls the Native method through JNI, and the GC mechanism cannot normally recycle the memory space requested by Bitmap (this was the case before API10, and then it was allocated in the Heap, but in order to be compatible with the old version... calling recycled explicitly can also allow GC to recycle faster);

How should Bitmap be recycled?

  1. // bitmap!=null should be placed in front. If the bitmap is empty, an error will be reported when calling the isRecycled method.  
  2.  
  3. if (bitmap!= null &&!bitmap.isRecycled){
  4.  
  5. bitmap.recycled(); //Recycle bitmap  
  6.  
  7. bitmap = null ; // Make the bitmap object a virtual reference so that GC can recycle it faster  
  8.  
  9. }

Next, let's talk about two more common memory leaks. You can expand on these two.

  1. btn_hint.setOnClickListener( new View.OnClickListener() {
  2.  
  3. @Override  
  4.  
  5. public   void onClick(View v) {
  6.  
  7. Toast.makeText(MainActivity. this , "Hello" , Toast.LENGTH_SHORT).show();
  8.  
  9. }
  10.  
  11. });

Is this very common? You may also write it like this (encapsulating it has the same meaning, as long as you reference the current activity). You may want to ask, is there any problem here?

The problem is that if the user presses the back key before the Toast disappears, this Activity will cause a memory leak.

Reason? Toast holds the current Activity, which prevents the Activity from being destroyed by GC.

Solution: Let Toast hold ApplicationContext; in fact, as long as it is not Layout, Context can use ApplicationContext;

By the way, here's a little tip: In non-Activity, you cannot directly get Context using getContext. You need to rely on Context to get resources. In this case, you can consider maintaining a global Context in your own Application for classes that cannot directly get Context, saving the need to pass parameters around (ApplicationContext is not recommended for views)

  1. private   static Context mContext;
  2.  
  3. public   static MyApplication getInstance() { //For external calls...  
  4.  
  5. return mApplication;
  6.  
  7. }
  8.  
  9. @Override  
  10.  
  11. public   void onCreate() {
  12.  
  13. super .onCreate();
  14.  
  15. mContext = getApplicationContext();
  16.  
  17. }

Another common... memory leak

  1. new Thread() {
  2.  
  3. public   void run() {
  4.  
  5. //Network request  
  6.  
  7. };
  8.  
  9. }.start();

Is there a problem here? Are you kidding me?

Create a new thread in the Activity to make a network request. If the thread is not finished and the user presses the back key, the memory leak will also occur.

Reason: The Thread is an anonymous inner class, so it will implicitly hold the outer class (here is Activity)

Solutions: Various; do not use anonymous inner classes, or maintain a thread pool or a thread queue for the entire application. The latter two methods make the thread independent of the Activity to avoid memory leaks.

Context is needed in many places such as resource acquisition, and anonymous inner classes are used in many places, which leads to a great memory leak risk here, but many friends may not have noticed it. I hope this article can give you some inspiration.

<<:  Simplify the framework for switching between PageView and TabView

>>:  When developing mobile apps, you should avoid these 5 details

Recommend

Qihoo 360 Tan Xiaosheng: The Tough Job——CTO

At the "CTO Training Camp" event hosted...

Advertising landing page production process

One of the things that netizens hate most when su...

What is a virtual world, and can it really be lived in?

Author: Duan Yuechu The virtual world is a fascin...

BlackBerry Priv review

On November 13, BlackBerry held a new product lau...

Tik Tok account promotion strategy for restaurant franchise!

We live in the colorful 21st century. With the co...

What are the examples and methods of APP promotion plans?

In recent years, mobile Internet has developed ra...

Who is the next Perfect Diary? Re-answering "Changes in Chinese Brands in 2020"!

“What has changed in our world?” 1. New brands: N...

How do communities with millions of members operate?

Since 2016, countless knowledge payment platforms...

iOS WeChat update: The withdrawal time can be extended to three hours

Yesterday, iOS WeChat updated to version 8.0.40, ...

How Weibo red envelopes replicated the magic of the Ice Bucket Challenge

Red envelopes are given out every year, but this ...

User Activation Methodology of AARRR Model

What is the AARRR model ? The AARRR model is also...

How to build a community O2O user operation system from 3 aspects

The o2o company I work for is a one-stop communit...