In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces the relevant knowledge of how to solve the memory leak caused by the use of Handler in Android, the content is detailed and easy to understand, the operation is simple and fast, and has a certain reference value. I believe you will gain something after reading this article on how to solve the memory leak caused by the use of Handler in Android. Let's take a look.
What is a memory leak?
Java uses a directed graph mechanism to automatically check objects in memory through GC (when the check is determined by the virtual machine), and if GC finds that an object or group of objects is in an unreachable state, it reclaims the object from memory. That is, if an object is not pointed to by any reference, it will be recycled when it is discovered by GC; in addition, if a set of objects contains only references to each other without references from outside them (for example, two objects An and B hold references to each other, but no external objects hold references to An or B), this is still unreachable and will also be recycled by GC.
The cause of memory leak caused by using Handler in Android
Private Handler handler = new Handler () {public void handleMessage (android.os.Message msg) {if (msg.what = = 1) {noteBookAdapter.notifyDataSetChanged ();}
The above is a simple use of Handler. When using inner classes (including anonymous classes) to create a Handler, the Handler object implicitly holds a reference to an external class object (usually an Activity) (otherwise how could you manipulate the View in the Activity through Handler? ). Handler is usually accompanied by a time-consuming background thread (such as pulling pictures from the network), which notifies Handler through a message mechanism after the task is completed (such as downloading the images), and then Handler updates the images to the interface. However, if the user turns off Activity during a network request, normally, Activity is no longer used, it may be recycled during the GC check, but since the thread has not finished executing at this time, and the thread holds a reference to Handler (otherwise, how does it send messages to Handler? The Handler also holds a reference to the Activity, causing the Activity to not be recycled (that is, a memory leak) until the network request ends (for example, the image has been downloaded). In addition, if you execute the postDelayed () method of Handler, which loads your Handler into a Message and pushes the Message into MessageQueue, there will be a chain of MessageQueue-> Message-> Handler-> Activity before the delay you set arrives, causing your Activity to be held by reference and cannot be recycled.
Second, the harm of memory leakage
The harm of memory leak is that the virtual machine occupies too much memory, resulting in OOM (memory overflow) and program errors.
For Android applications, it means that your user opens an Activity, closes it after using it, and leaks memory; opens, closes, and leaks; after a few times, the program takes up more memory than the system limit, FC.
III. Solutions
The solution to memory leak caused by using Handler
Method 1: protect it through program logic.
1. Stop your background thread when shutting down Activity. If the thread stops, it is equivalent to cutting off the Handler and the external connection, and the Activity will naturally be recycled at the appropriate time.
two。 If your Handler is referenced by delay's Message, then use the corresponding Handler's removeCallbacks () method to remove the message object from the message queue.
Method 2: declare Handler as a static class.
PS: in Java, both non-static inner classes and anonymous inner classes implicitly hold references to their external classes, and static inner classes do not hold references to external classes.
Static classes do not hold objects of external classes, so your Activity can be recycled at will. Because Handler no longer holds references to external class objects, the program does not allow you to manipulate objects in Activity in Handler. So you need to add a weak reference to Activity (WeakReference) in Handler.
The code is as follows:
Static class MyHandler extends Handler {WeakReference mWeakReference; public MyHandler (Activity activity) {mWeakReference=new WeakReference (activity);} @ Override public void handleMessage (Message msg) {final Activity activity=mWeakReference.get (); if (activityinvalid null) {if (msg.what = = 1) {noteBookAdapter.notifyDataSetChanged ();}
PS: what is WeakReference?
WeakReference weak reference, compared with strong reference (that is, we often say reference), its characteristic is that GC ignores weak reference when recycling, that is, even if there is a weak reference pointing to an object, as long as the object is not pointed to by a strong reference (in fact, most of the time there is no soft reference required, but the concept of soft reference can be ignored here), the object will be recycled when it is checked by GC. For the above code, after the user closes Activity, even if the background thread is not finished, because there is only one weak reference from Handler pointing to Activity, GC will still recycle the Activity when checking. In this way, the problem of memory leaks will not occur.
This is the end of the article on "how to solve the memory leak caused by the use of Handler in Android". Thank you for reading! I believe that everyone has a certain understanding of the knowledge of "how to solve the memory leak caused by the use of Handler in Android". If you want to learn more knowledge, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.