简体   繁体   中英

Kotlin Android Extensions vs Android Data Binding Library, memory usage

I have a question about memory used in both cases ->Android Data Binding vs Android Kotlin Extensions. In which case will be less memory used on the device?

Kotlin Android-extension is calling first findViewById and after that, the result will be stored locally in a cache. and this means memory used.

DataBinding creates a path between layouts and activities/fragments through the binding class that was created.

My concern is to use the one that is more efficient when we think of memory usage on the user's device. Could anyone help me to figure out the answer? I tend to say dataBinding is more efficient. Here is a similar question but is not in the efficiency direction.

Thanks!

tldr: As far as I see it there is no difference depending memory usage, only if you don't use all views of the layout. Both cache the views, however andoid kotlin gets on demand while databinding initializes all. Depending performance, kotlin extensions is slightly(I would say in most cases not recognizable) faster than databinding during fragment/activity creation.

Documentation Kotlin Android Extensions:

Adds a hidden caching function and a field inside each Kotlin Activity. The method is pretty small so it doesn't increase the size of APK much. Replaces each synthetic property call with a function call.

How this works is that when invoking a synthetic property, where the receiver is a Kotlin Activity/Fragment class that is in module sources, the caching function is invoked. For instance, given

class MyActivity : Activity() fun MyActivity.a() { 
         this.textView.setText(“”) 
}

a hidden caching function is generated inside MyActivity, so we can use the caching mechanism.

However in the following case:

fun Activity.b() { 
    this.textView.setText(“”)
}

We wouldn't know if this function would be invoked on only Activities from our sources or on plain Java Activities also. As such, we don't use caching there, even if MyActivity instance from the previous example is the receiver.

Reason: Kotlin uses synthetic properties and those are called on demand using caching function(Hence slight fast Activity/Fragment loading) while Databinding binds all views at once (that consumes slight more time).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM