In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how to solve the memory leak caused by Handler". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to solve the memory leak caused by Handler.
Let's start with a simple set of code.
Public class SampleActivity extends Activity {private final Handler mHandler = new Handler () {@ Override public void handleMessage (Message msg) {/ /...}
When we write this in an Activity, Android Lint prompts us for a warning: In Android, Handler classes should be static or leaks might occur.
Meaning: in Android, the Handler class should be static or leakage may occur.
Why is this the case?
Get to know Handler.
When the Android program is created for the second time, a Looper object is created in the main thread at the same time. Looper implements a simple message queue that processes Message objects one by one. All the major events of the program framework (for example, click time on the screen, methods of the Activity life cycle, etc.) are contained in the Message object and then added to the message queue of Looper, processed one by one. The Looper of the main thread exists throughout the life cycle of the application.
When a Handler object is created in the main thread, it is associated with the message queue of Looper. Message holds the current Handler reference when Message is added to the message queue, and Handler#handleMessage (Message) is called when Looper processes the current message.
In java, the inner class of no-static implicitly holds a reference to the current class. The class of static does not.
Where did the memory leak occur? Take a look at the following code
Public class SampleActivity extends Activity {private final Handler mHandler = new Handler () {@ Override public void handleMessage (Message msg) {/ /...}} @ Override protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState) / / send a message mHandler.postDelayed (new Runnable () {@ Override public void run () {}}, 600000) that will be executed in 10 minutes; / / end the current Activity finish ();}}
When the Activity is over, it will continue to survive until the Message queue processes the Message. This Message holds a reference to Handler, while Handler has a reference to hold Activity (SampleActivity). All resources of this Activity cannot and will not be recycled until this message is processed, so a memory leak occurs.
For the solution, look at the following code
Public class SampleActivity extends Activity {/ * uses static inner classes and does not hold references to the current object * / private static class MyHandler extends Handler {private final WeakReference mActivity; public MyHandler (SampleActivity activity) {mActivity = new WeakReference (activity);} @ Override public void handleMessage (Message msg) {SampleActivity activity = mActivity.get () If (activity! = null) {/ /...}} private final MyHandler mHandler = new MyHandler (this); / * use a static inner class and do not hold a reference to the current object * / private static final Runnable sRunnable = new Runnable () {@ Override public void run () {}} @ Override protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); / / send a message mHandler.postDelayed (sRunnable, 600000) executed in 10 minutes; / / end finish ();}} so far, I believe you have a better understanding of "how to solve the memory leak caused by Handler". You might as well do it in practice! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.