Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin delegates memory usage

Tags:

android

kotlin

While designing android chat application several classes were creates with use of Kotlin delegates. These classes are often used and passed between classes; so, I would like to ask you about memory usage of having such structure of classes on an example of one such case.

Concrete class ImageMessage that uses Kotlin delegates:

open class ImageMessage(
    image: AppImage,
    val timestamp: Long
) : AppImage by image

This is AppImage interface used in ImageMessage:

interface AppImage {
    var firebaseStorageUrl: String
    val image: Bitmap
}

In the chat application 2 collections are present: one is list of anonymous objects that implement AppImage, and the other is list of ImageMessages that were created with help of the anonymous classes.

Kinda like this:

val image = object : AppImage{...}
imagesList.add(image)
val imageMessage = ImageMessage(image, System.currentTimeInMillis())
imageMessageList.add(imageMessage)

Question: does this implementation results in 2 bitmap images stored or there is one bitmap object linked twice? Seeing new for me delegates syntax makes me hesitate while answering the question; so, I ask Your help!

like image 401
S.Drumble2 Avatar asked Dec 22 '25 02:12

S.Drumble2


1 Answers

Take a look when decomplied it to Java. First, ImageMessage implements AppImage and override method getFirebaseStorageUrl and getImage return value depend on AppImage image. So it means only one instance of Bitmap will be created and get through image instance.

public class ImageMessage implements AppImage {
   private final long timestamp;
   // $FF: synthetic field
   private final AppImage $$delegate_0;

   public final long getTimestamp() {
      return this.timestamp;
   }

   public ImageMessage(@NotNull AppImage image, long timestamp) {
      Intrinsics.checkParameterIsNotNull(image, "image");
      super();
      this.$$delegate_0 = image;
      this.timestamp = timestamp;
   }

   @NotNull
   public String getFirebaseStorageUrl() {
      return this.$$delegate_0.getFirebaseStorageUrl();
   }

   public void setFirebaseStorageUrl(@NotNull String var1) {
      Intrinsics.checkParameterIsNotNull(var1, "<set-?>");
      this.$$delegate_0.setFirebaseStorageUrl(var1);
   }

   @NotNull
   public Bitmap getImage() {
      return this.$$delegate_0.getImage();
   }
}

Easy way to understand is to declare ImageMessage as follows:

open class ImageMessage(override var firebaseStorageUrl: String, override val image: Bitmap, val timestamp: Long) : AppImage

firebaseStorageUrl and image are implemented by AppImage.

open class ImageMessage(
    abc: AppImage,
    val timestamp: Long
) : AppImage by abc

This class of ImageMessage implements AppImage by means of delegates, but, in essence, it is the same ImageMessage that explicit implements AppImage as above.

P.s.: I just change name of delegate to help you understand.

like image 120
Công Hải Avatar answered Dec 24 '25 19:12

Công Hải



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!