Loading...
Home
  • Tech Blogs
  • Videos
  • Conferences
    • Droidcon News
    • Upcoming Conferences
    • Become a Partner
    • Past Events
    • Keep Me Informed
    • Diversity Scholarships
  • Community
    • droidcon Team
    • How to Hold a Droidcon
  • Android Careers
Sign In

Global CSS

 

Avoiding memory leaks when using Data Binding and View Binding

 

 
Anders Ullnæss
Mobile developer based in Amsterdam
Published: August 25, 2020
Tweet
Share
 

 

In our current project we are using a lot of Data Binding and recently we have started using View Binding as well.

A little while back, we were inspired by Pierre-Yves Ricau and Emily Kager’s awesome video covering how to find and fix memory leaks with LeakCanary, so we added it to our project. We soon saw falling birds all over the place.

 

Image for post

This bird couldn’t handle our Data Binding memory leaks :(

 

Memory leaks everywhere

When using Data Binding we used to add the binding as a lateinit var to our fragment like this:

 

 

This leads to a memory leak:

 

Image for post

 

Image for post

 

LeakCanary is telling us our binding is keeping a reference to our view (mRoot) after the Fragment’s onDestroyView() was called and this might lead to a memory leak.

When it comes to View Binding, Google’s own docs are actually teaching us how to do it the proper way to avoid getting memory leaks:

 

 

This technique uses an optional backing field and a non-optionalval which is only valid between onCreateView and onDestroyView.
In onCreateView the optional backing field is set and in onDestroyView it is cleared. This fixes the memory leak!

This way of doing it is also covered in the video I mentioned earlier at this timestamp.

You might feel a bit uneasy about the double bangs !!, but all we are saying here is that we are sure this field will not be null when we use it (in the lifecycle between onCreateView and onDestroyView), which is exactly the same we are saying when we use lateinit var.

 

Cool, but now I have to add x lines to every fragment?

Our app has a lot of fragments and it would be quite tedious to apply this fix everywhere. A while back I found this blog post by Kirill Rozov where he talks about a small view binding delegate library he made which saves you quite some hassle/lines.

Read his blog post for the details, but using this we can now write our binding like this:

 

 

His view binding property delegate takes care of the setting and clearing of the binding at the proper time in the lifecycle, so that we don’t have to do that ourselves in every fragment.

Using another trick I learned from his examples, we can actually get rid of the whole onCreateView method. It turns out Fragment has a constructor that takes a reference to a layout and then inflates that layout for you in onCreateView (has this always been there?!):

 

 

This is great for view binding, but how does this help me with data binding?

Maybe this is common knowledge, but I had an aha moment when I found out that a data binding is a view binding:

 

 

This means that everything we did in this post can be applied no matter if we are using view binding or data binding!

Please let me know if this was helpful or if you have any questions or see any mistakes or things that could be done even better.

 

 

 

Tags: Android, View Binding, Data Binding, Memory Leak

 

View original article at: 


 

Originally published: August 22, 2020

Android News
Our Engineering Roadmap
Our Engineering Roadmap

By Mark Ng

We just completed our engineering road map for our Android apps at Australia Post. Each year we get together and try to decide on what we are going to do from an engineering perspective for the next 12 months. Each team gets to decide on what should be done now, what they want to complete by the end of the year and whats on the horizon for next year.

By ProAndroidDev -
Android News
Android Activity Lifecycle considered harmful
Android Activity Lifecycle considered harmful

By Eric Silverberg

The Android activity lifecycle is one of the first architectural concepts learned by a new Android developer. Ask any Android dev and they’ll likely all have seen this handy diagram from Google: 

By ProAndroidDev -
Android News
Our Safe Approach to Android Jetpack Navigation in a Multi-Modular App
Our Safe Approach to Android Jetpack Navigation in a Multi-Modular App

By Alejandro Weichandt

It has been a year since we started working on the Android version of the mobile app at Sync. During that year, we faced more than once that moment when we had to choose which path to follow on an Architectural decision. This story is about Navigation.

By ProAndroidDev -
Android News
Custom KotlinX Serializers
Custom KotlinX Serializers

By Jobin Lawrance

Let’s say we have a third-party class that we are using as a type in one of our data class that we want to be serialized, then we have to write a custom serializable for @Serializable to work.

 

By ProAndroidDev -
droidcon News

Tech Showcases,

Developer Resources &

Partners

/portal/rest/jcr/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/home-details/EmployerBrandingHeader
EmployerBrandingHeader
https://jobs.droidcon.com/
/portal/rest/jcr/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/jobs-droidcon/jobs.droidcon.com
jobs.droidcon.com

Latest Android Jobs

http://www.kotlinweekly.net/
/portal/rest/jcr/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/kotlin-weekly/Kotlin Weekly
Kotlin Weekly

Your weekly dose of Kotlin

https://proandroiddev.com/
/portal/rest/jcr/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/pad/ProAndroidDev
ProAndroidDev

Android Tech Blogs, Case Studies and Step-by-Step Coding

/detail?content-id=/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/Zalando/Zalando
/portal/rest/jcr/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/Zalando/Zalando
Zalando

Meet one of Berlin's top employers

/detail?content-id=/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/Academy for App Success/Academy for App Success
/portal/rest/jcr/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/Academy for App Success/Academy for App Success
Academy for App Success

Google Play resources tailored for the global droidcon community

Follow us

Team droidcon

Get in touch with us

Write us an Email

 

 

Quicklinks

> Code of Conduct

> Terms and Conditions

> How to hold a conference

> FAQs

> Imprint

Droidcon is a registered trademark of Mobile Seasons GmbH Copyright © 2020. All rights reserved.

powered by Breakpoint One