简体   繁体   中英

Kotlin delegates memory usage

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!

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.

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

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