How I Optimized Bitmap Memory Management in Matnnegar Using Kotlin’s Property Delegation🚀

Mahmoud Afarideh
Matnnegar Engineering
3 min readSep 21, 2023

--

Optimizing Bitmap Memory in Matnnegar with Kotlin Delegation

Introduction📑

Bitmap management is a critical aspect of Android app development, and it becomes especially crucial in apps like Matnnegar, where users can write and design text on photos📸. Matnnegar requires efficient memory usage for its complex image editing features, which frequently involve overlaying text on images, adding stickers🎨, and editing backgrounds. Failing to manage Bitmap memory correctly can lead to performance issues🐌 and crashes💥. This article explores how Kotlin’s property delegation feature can be effectively employed for Bitmap memory management, focusing on its implementation in the Matnnegar app.

The Importance of Bitmap Recycling♻️

In Android, a Bitmap is a representation of an image that consists of pixels with a specified width, height, and color format🎨. Recycling Bitmaps allows an app to reclaim memory as soon as possible when you are sure that the bitmap is no longer being used. If you do not recycle Bitmaps, you run the risk of out-of-memory errors🚫. Bitmap recycling is particularly vital in apps like Matnnegar, where users are likely to load multiple high-resolution images for editing📷.

Traditional Bitmap Management in Android📱

Conventionally, the Bitmap object needs to be recycled using the recycle() method to free up native heap memory. This can be done as follows:

if (bitmap != null) {
bitmap?.recycle()
}
bitmap = newBitmap

While effective, applying this technique in multiple sections or UI components of an app like Matnnegar would result in code repetition🔄 and increased complexity🤯.

Understanding Kotlin’s Property Delegation📘

Kotlin’s property delegation feature allows you to delegate the getter and setter functions of a property to another class🔄, thereby encapsulating the logic related to that property in one place✅. This avoids code redundancy and makes the code more maintainable. The by keyword is used to specify the delegate class.

For example:

class Example {
var name: String by DelegateClass()
}

class DelegateClass {
operator fun getValue(thisRef: Any?, property: KProperty<*>): String {
return "Hello, Delegation!"
}

operator fun setValue(thisRef: Any?, property: KProperty<*>, value: String) {
println("Assigning value: $value")
}
}

Customizing Bitmap Memory Management with Kotlin Delegation🛠

To centralize and simplify the logic for Bitmap memory management, I created a class named EfficientBitmapDelegate using Kotlin's property delegation. This class takes care of recycling Bitmaps when they are no longer needed or when they are replaced with new ones🔄.

Here is the EfficientBitmapDelegate class:

class EfficientBitmapDelegate {

private var bitmap: Bitmap? = null

operator fun getValue(thisRef: Any?, property: Any?): Bitmap? {
return bitmap
}

operator fun setValue(thisRef: Any?, property: Any?, value: Bitmap?) {
if (value?.generationId == bitmap?.generationId) return
if (value == null || value.generationId != bitmap?.generationId) {
bitmap?.recycle()
}
bitmap = value
}

}

Key Points:

  • The class uses a nullable Bitmap variable, bitmap.
  • It overrides getValue and setValue functions.
  • It recycles old Bitmaps either when they are replaced or set to null.

Implementing EfficientBitmapDelegate in Matnnegar🛠

In Matnnegar, users frequently change background images and stickers, so effective Bitmap memory management is essential. To achieve this, I used EfficientBitmapDelegate as a delegate for properties corresponding to these Bitmap objects🎯.

Example usage in Matnnegar:

var bitmap: Bitmap? by EfficientBitmapDelegate()

This way, whenever the bitmap property changes, the old Bitmap gets automatically recycled, freeing up valuable memory resources💡.

Conclusion🎬

For image-heavy apps like Matnnegar, effective memory management is not just an optimization — it’s a requirement💯. Recycling Bitmaps is especially crucial to prevent out of memory errors🚫 and ensure smooth performance🚀. Kotlin’s property delegation provides a clean, centralized approach to managing Bitmap memory👌. By implementing SafeBitmapDelegate, I could significantly reduce the risk of memory-related issues in Matnnegar, making the app more efficient and reliable🌟.

--

--