Android performance optimization using rxlifecycle to solve memory leak in rxjava

preface:

In fact, I accidentally discovered the memory leak caused by rxjava. I wanted to know how to solve it through the adapter mode in the combination of retrofit and rxjava, but I found that rxjava will cause memory leak. I wanted to find out the data and learn how to solve the memory leak caused by rxjava. I found that it can be solved by using the rxlifecycle open source framework, Let's learn how to use rxlifecycle this weekend.

Background of reference leakage:

Rxjava, as a responsive programming framework, is currently a popular programming framework. It is a household name. Its concise coding style, easy-to-use and readable chained method calls, strong asynchronous support, etc. make rxjava widely used. It is easier to control and switch threads through the thread scheduler. If the working thread has not finished executing, it will exit the activity or fragment, The activity or fragment cannot be released, causing a memory leak.

What is rxlifecycle?

Rxlifecycle is an open source framework developed by trello to solve memory leaks caused by rxjava.

GitHub address: https://github.com/trello/RxLifecycle

How to use rxlifecycle?

1.) add a reference in the build.gradle file

According to your own needs, I use the following two

2.) according to different needs, activity inherits rxactivity and fragment inherits rxfragment

Activity / fragment structure diagram currently supported

3.) how to use bindtollifecycle()

In the subclass, use the compose operator in observable to call to complete the binding between the event published by observable and the current component, so as to realize life cycle synchronization. Thus, the subscription to observable is automatically cancelled at the end of the current component life cycle.

4.) use binduntileevent()

Use the activityevent class, where create, start, resume, pause, stop and destroy correspond to the methods in the life cycle respectively. Use binduntileevent to specify which lifecycle method call to unsubscribe from.

5.) customize an rxactivity / rxfragment

You only need to implement the lifecycleprovider < activityevent > interface for the activity you want. Here, post the source code of rxactivity and modify it.

Summary:

This paper summarizes how to solve the memory leakage problem of rxjava through rxlifecycle, and also gives us a warning. No matter how good a framework is, it has both good and bad sides. At this time, it is very important to do a good job in technology selection and risk avoidance.

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>