Android performance optimization uses rxlifecycle to solve Rxjava memory leaks __java

Source: Internet
Author: User

Android performance optimization using Rxlifecycle to resolve Rxjava memory leaks
Objective:
In fact, Rxjava caused by the memory leak is I accidentally found, is to understand how retrofit and Rxjava in combination with the adapter mode to solve, the result found that Rxjava is to cause memory leaks, All want to look up the data to learn how to solve the memory leak caused by Rxjava, found that the use of Rxlifecycle Open source framework can be resolved, this weekend to learn how to use rxlifecycle.

Reference to the background of the leak:
Rxjava as a responsive programming framework, is the current programming network red, is well-known, its concise coding style, easy to read the chain method calls, powerful asynchronous support, etc. make Rxjava is widely used, it is easier to control and switch threads through the thread scheduler, If the worker thread exits the activity or fragment if it has not finished executing, it will either be alive or fragment unable to release the memory leak.

What is rxlifecycle.
Rxlifecycle is an open source framework developed by Trello to address memory leaks caused by Rxjava.

GitHub Address: Https://github.com/trello/RxLifecycle

How to use Rxlifecycle.
1. Add a reference to the Build.gradle file
Compile ' com.trello.rxlifecycle2:rxlifecycle:2.2.1 '

If you are want to bind to android-specific lifecycles
Compile ' com.trello.rxlifecycle2:rxlifecycle-android:2.2.1 '

If you are want pre-written activities and fragments you can subclass as providers
Compile ' com.trello.rxlifecycle2:rxlifecycle-components:2.2.1 '

If you are want pre-written support preference fragments you can subclass as providers
Compile ' com.trello.rxlifecycle2:rxlifecycle-components-preference:2.2.1 '

If you are want to use Navi for providers
Compile ' com.trello.rxlifecycle2:rxlifecycle-navi:2.2.1 '

If you are want to use the Android lifecycle for providers
Compile ' com.trello.rxlifecycle2:rxlifecycle-android-lifecycle:2.2.1 '

If you are want to use Kotlin syntax
Compile ' com.trello.rxlifecycle2:rxlifecycle-kotlin:2.2.1 '

If you are want to use Kotlin syntax with Android lifecycle
Compile ' com.trello.rxlifecycle2:rxlifecycle-android-lifecycle-kotlin:2.2.1 '
2.) According to the different needs of the activity to inherit rxactivity, fragment inheritance Rxfragment

Public class MainActivity7 extends Rxactivity {
Private TextView mtextview;

@Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
    Setcontentview (R.layout.activity_main);
    Mtextview = (TextView) Findviewbyid (R.id.text);
    Analog memory leak Testrxjava ();
Finish ();
        private void Testrxjava () {observable.create () {new observable.onsubscribe<string> () {@Override
            public void Call (SUBSCRIBER&LT. Super string> subscriber) {int i = 0;
            while (I < 1000000000) {i++;
            } subscriber.onnext (String.valueof (i));
        Subscriber.oncompleted ();
            }). Compose (This.<string>binduntilevent (Activityevent.pause)). Subscribeon (Schedulers.io ()) . Observeon (Androidschedulers.mainthread ()). Subscribe (New action1<string> () {@Over
                Ride public void Call (String s) {mtextview.settext (s);

}
            });

}
@Override protected void OnDestroy () {Super.ondestroy ();
Lapplication.getrefwatcher (). Watch (this);
 }

}

Currently supported activity/fragment structure diagrams

3.) Use of bindtolifecycle ()
In the subclass, the compose operator in observable is invoked, completes the observable release event and the current component binding, and implements lifecycle synchronization. Thus, the observable subscription is automatically canceled when the current component lifecycle ends.

Observable.create (New Observable.onsubscribe () {
@Override
public void Call (subscriber

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.