বিটম্যাপ মেমরি পরিচালনা

দ্রষ্টব্য: বেশিরভাগ ক্ষেত্রে, আমরা সুপারিশ করি যে আপনি আপনার অ্যাপে বিটম্যাপ আনতে, ডিকোড করতে এবং প্রদর্শন করতে গ্লাইড লাইব্রেরি ব্যবহার করুন। অ্যান্ড্রয়েডে বিটম্যাপ এবং অন্যান্য চিত্রগুলির সাথে কাজ করার সাথে সম্পর্কিত এইগুলি এবং অন্যান্য কাজগুলি পরিচালনা করার বেশিরভাগ জটিলতা গ্লাইড বিমূর্ত করে। গ্লাইড ব্যবহার এবং ডাউনলোড করার বিষয়ে তথ্যের জন্য, GitHub-এ গ্লাইড সংগ্রহস্থলে যান।

ক্যাশিং বিটম্যাপ- এ বর্ণিত পদক্ষেপগুলি ছাড়াও, আবর্জনা সংগ্রহ এবং বিটম্যাপ পুনঃব্যবহারের সুবিধার্থে আপনি করতে পারেন এমন কিছু নির্দিষ্ট জিনিস রয়েছে। প্রস্তাবিত কৌশলটি Android এর কোন সংস্করণ(গুলি) আপনি লক্ষ্য করছেন তার উপর নির্ভর করে৷ এই ক্লাসের সাথে অন্তর্ভুক্ত BitmapFun নমুনা অ্যাপটি আপনাকে দেখায় কিভাবে আপনার অ্যাপটিকে Android এর বিভিন্ন সংস্করণে দক্ষতার সাথে কাজ করার জন্য ডিজাইন করতে হয়।

এই পাঠের জন্য পর্যায় সেট করার জন্য, বিটম্যাপ মেমরির অ্যান্ড্রয়েডের ব্যবস্থাপনা কীভাবে বিকশিত হয়েছে তা এখানে রয়েছে:

  • অ্যান্ড্রয়েড 2.2 (API লেভেল 8) এবং তার নিচে, যখন আবর্জনা সংগ্রহ করা হয়, তখন আপনার অ্যাপের থ্রেড বন্ধ হয়ে যায়। এটি একটি ব্যবধান সৃষ্টি করে যা কর্মক্ষমতা হ্রাস করতে পারে। অ্যান্ড্রয়েড 2.3 একযোগে আবর্জনা সংগ্রহ যোগ করে, যার অর্থ বিটম্যাপ আর উল্লেখ না করার পরেই মেমরিটি পুনরুদ্ধার করা হয়।
  • অ্যান্ড্রয়েড 2.3.3 (API লেভেল 10) এবং তার নিচে, একটি বিটম্যাপের জন্য ব্যাকিং পিক্সেল ডেটা নেটিভ মেমরিতে সংরক্ষণ করা হয়। এটি বিটম্যাপ থেকে আলাদা, যা ডালভিক হিপে সংরক্ষণ করা হয়। নেটিভ মেমরিতে পিক্সেল ডেটা অনুমানযোগ্য পদ্ধতিতে প্রকাশ করা হয় না, সম্ভাব্যভাবে একটি অ্যাপ্লিকেশন সংক্ষিপ্তভাবে মেমরির সীমা অতিক্রম করে এবং ক্র্যাশ করে। Android 3.0 (API স্তর 11) থেকে Android 7.1 (API স্তর 25) এর মাধ্যমে পিক্সেল ডেটা সংশ্লিষ্ট বিটম্যাপের সাথে ডালভিক হিপে সংরক্ষণ করা হয়। Android 8.0 (API স্তর 26) এবং উচ্চতর, বিটম্যাপ পিক্সেল ডেটা নেটিভ হিপে সংরক্ষণ করা হয়।

নিম্নলিখিত বিভাগগুলি বিভিন্ন Android সংস্করণের জন্য বিটম্যাপ মেমরি পরিচালনাকে কীভাবে অপ্টিমাইজ করা যায় তা বর্ণনা করে৷

অ্যান্ড্রয়েড 2.3.3 এবং নিম্নতর মেমরি পরিচালনা করুন

অ্যান্ড্রয়েড 2.3.3 (API লেভেল 10) এবং তার নিচে, recycle() ব্যবহার করার পরামর্শ দেওয়া হয়। আপনি যদি আপনার অ্যাপে প্রচুর পরিমাণে বিটম্যাপ ডেটা প্রদর্শন করেন, তাহলে আপনি OutOfMemoryError ত্রুটির মধ্যে পড়তে পারেন। recycle() পদ্ধতি একটি অ্যাপকে যত তাড়াতাড়ি সম্ভব মেমরি পুনরুদ্ধার করতে দেয়।

সতর্কতা: আপনি শুধুমাত্র তখনই recycle() ব্যবহার করবেন যখন আপনি নিশ্চিত হন যে বিটম্যাপ আর ব্যবহার করা হচ্ছে না। আপনি যদি recycle() কল করেন এবং পরে বিটম্যাপ আঁকার চেষ্টা করেন, আপনি ত্রুটিটি পাবেন: "Canvas: trying to use a recycled bitmap"

নিম্নলিখিত কোড স্নিপেট কলিং recycle() এর একটি উদাহরণ দেয়। এটি একটি বিটম্যাপ বর্তমানে প্রদর্শিত হচ্ছে বা ক্যাশে রয়েছে কিনা তা ট্র্যাক করতে এটি রেফারেন্স কাউন্টিং ( mDisplayRefCount এবং mCacheRefCount ভেরিয়েবলে) ব্যবহার করে। কোডটি বিটম্যাপ পুনর্ব্যবহার করে যখন এই শর্তগুলি পূরণ হয়:

  • mDisplayRefCount এবং mCacheRefCount উভয়ের জন্য রেফারেন্স গণনা 0।
  • বিটম্যাপটি null নয়, এবং এটি এখনও পুনর্ব্যবহার করা হয়নি।

কোটলিন

private var cacheRefCount: Int = 0
private var displayRefCount: Int = 0
...
// Notify the drawable that the displayed state has changed.
// Keep a count to determine when the drawable is no longer displayed.
fun setIsDisplayed(isDisplayed: Boolean) {
    synchronized(this) {
        if (isDisplayed) {
            displayRefCount++
            hasBeenDisplayed = true
        } else {
            displayRefCount--
        }
    }
    // Check to see if recycle() can be called.
    checkState()
}

// Notify the drawable that the cache state has changed.
// Keep a count to determine when the drawable is no longer being cached.
fun setIsCached(isCached: Boolean) {
    synchronized(this) {
        if (isCached) {
            cacheRefCount++
        } else {
            cacheRefCount--
        }
    }
    // Check to see if recycle() can be called.
    checkState()
}

@Synchronized
private fun checkState() {
    // If the drawable cache and display ref counts = 0, and this drawable
    // has been displayed, then recycle.
    if (cacheRefCount <= 0
            && displayRefCount <= 0
            && hasBeenDisplayed
            && hasValidBitmap()
    ) {
        getBitmap()?.recycle()
    }
}

@Synchronized
private fun hasValidBitmap(): Boolean =
        getBitmap()?.run {
            !isRecycled
        } ?: false

জাভা

private int cacheRefCount = 0;
private int displayRefCount = 0;
...
// Notify the drawable that the displayed state has changed.
// Keep a count to determine when the drawable is no longer displayed.
public void setIsDisplayed(boolean isDisplayed) {
    synchronized (this) {
        if (isDisplayed) {
            displayRefCount++;
            hasBeenDisplayed = true;
        } else {
            displayRefCount--;
        }
    }
    // Check to see if recycle() can be called.
    checkState();
}

// Notify the drawable that the cache state has changed.
// Keep a count to determine when the drawable is no longer being cached.
public void setIsCached(boolean isCached) {
    synchronized (this) {
        if (isCached) {
            cacheRefCount++;
        } else {
            cacheRefCount--;
        }
    }
    // Check to see if recycle() can be called.
    checkState();
}

private synchronized void checkState() {
    // If the drawable cache and display ref counts = 0, and this drawable
    // has been displayed, then recycle.
    if (cacheRefCount <= 0 && displayRefCount <= 0 && hasBeenDisplayed
            && hasValidBitmap()) {
        getBitmap().recycle();
    }
}

private synchronized boolean hasValidBitmap() {
    Bitmap bitmap = getBitmap();
    return bitmap != null && !bitmap.isRecycled();
}

অ্যান্ড্রয়েড 3.0 এবং উচ্চতর মেমরি পরিচালনা করুন

Android 3.0 (API স্তর 11) BitmapFactory.Options.inBitmap ক্ষেত্র প্রবর্তন করে। এই বিকল্পটি সেট করা থাকলে, Options অবজেক্ট গ্রহণকারী ডিকোড পদ্ধতিগুলি সামগ্রী লোড করার সময় একটি বিদ্যমান বিটম্যাপ পুনরায় ব্যবহার করার চেষ্টা করবে। এর মানে হল যে বিটম্যাপের মেমরি পুনরায় ব্যবহার করা হয়, যার ফলে কর্মক্ষমতা উন্নত হয় এবং মেমরি বরাদ্দ এবং ডি-অ্যালোকেশন উভয়ই সরিয়ে ফেলা হয়। যাইহোক, inBitmap কীভাবে ব্যবহার করা যেতে পারে তার সাথে কিছু সীমাবদ্ধতা রয়েছে। বিশেষ করে, Android 4.4 (API লেভেল 19) এর আগে, শুধুমাত্র সমান আকারের বিটম্যাপ সমর্থিত। বিস্তারিত জানার জন্য, অনুগ্রহ করে inBitmap ডকুমেন্টেশন দেখুন।

পরে ব্যবহারের জন্য একটি বিটম্যাপ সংরক্ষণ করুন

নিম্নলিখিত স্নিপেটটি প্রদর্শন করে যে কীভাবে একটি বিদ্যমান বিটম্যাপ নমুনা অ্যাপে পরবর্তীতে ব্যবহারের জন্য সংরক্ষণ করা হয়। যখন একটি অ্যাপ অ্যান্ড্রয়েড 3.0 বা উচ্চতর সংস্করণে চলছে এবং LruCache থেকে একটি বিটম্যাপ উচ্ছেদ করা হয়, তখন বিটম্যাপের একটি নরম রেফারেন্স একটি HashSet এ স্থাপন করা হয়, যাতে পরবর্তীতে inBitmap সাথে পুনরায় ব্যবহার করা যায়:

কোটলিন

var reusableBitmaps: MutableSet<SoftReference<Bitmap>>? = null
private lateinit var memoryCache: LruCache<String, BitmapDrawable>
// If you're running on Honeycomb or newer, create a
// synchronized HashSet of references to reusable bitmaps.
if (Utils.hasHoneycomb()) {
    reusableBitmaps = Collections.synchronizedSet(HashSet<SoftReference<Bitmap>>())
}

memoryCache = object : LruCache<String, BitmapDrawable>(cacheParams.memCacheSize) {

    // Notify the removed entry that is no longer being cached.
    override fun entryRemoved(
            evicted: Boolean,
            key: String,
            oldValue: BitmapDrawable,
            newValue: BitmapDrawable
    ) {
        if (oldValue is RecyclingBitmapDrawable) {
            // The removed entry is a recycling drawable, so notify it
            // that it has been removed from the memory cache.
            oldValue.setIsCached(false)
        } else {
            // The removed entry is a standard BitmapDrawable.
            if (Utils.hasHoneycomb()) {
                // We're running on Honeycomb or later, so add the bitmap
                // to a SoftReference set for possible use with inBitmap later.
                reusableBitmaps?.add(SoftReference(oldValue.bitmap))
            }
        }
    }
}

জাভা

Set<SoftReference<Bitmap>> reusableBitmaps;
private LruCache<String, BitmapDrawable> memoryCache;

// If you're running on Honeycomb or newer, create a
// synchronized HashSet of references to reusable bitmaps.
if (Utils.hasHoneycomb()) {
    reusableBitmaps =
            Collections.synchronizedSet(new HashSet<SoftReference<Bitmap>>());
}

memoryCache = new LruCache<String, BitmapDrawable>(cacheParams.memCacheSize) {

    // Notify the removed entry that is no longer being cached.
    @Override
    protected void entryRemoved(boolean evicted, String key,
            BitmapDrawable oldValue, BitmapDrawable newValue) {
        if (RecyclingBitmapDrawable.class.isInstance(oldValue)) {
            // The removed entry is a recycling drawable, so notify it
            // that it has been removed from the memory cache.
            ((RecyclingBitmapDrawable) oldValue).setIsCached(false);
        } else {
            // The removed entry is a standard BitmapDrawable.
            if (Utils.hasHoneycomb()) {
                // We're running on Honeycomb or later, so add the bitmap
                // to a SoftReference set for possible use with inBitmap later.
                reusableBitmaps.add
                        (new SoftReference<Bitmap>(oldValue.getBitmap()));
            }
        }
    }
....
}

একটি বিদ্যমান বিটম্যাপ ব্যবহার করুন

চলমান অ্যাপে, ডিকোডার পদ্ধতিগুলি তারা ব্যবহার করতে পারে এমন একটি বিদ্যমান বিটম্যাপ আছে কিনা তা পরীক্ষা করে। উদাহরণ স্বরূপ:

কোটলিন

fun decodeSampledBitmapFromFile(
        filename: String,
        reqWidth: Int,
        reqHeight: Int,
        cache: ImageCache
): Bitmap {

    val options: BitmapFactory.Options = BitmapFactory.Options()
    ...
    BitmapFactory.decodeFile(filename, options)
    ...

    // If we're running on Honeycomb or newer, try to use inBitmap.
    if (Utils.hasHoneycomb()) {
        addInBitmapOptions(options, cache)
    }
    ...
    return BitmapFactory.decodeFile(filename, options)
}

জাভা

public static Bitmap decodeSampledBitmapFromFile(String filename,
        int reqWidth, int reqHeight, ImageCache cache) {

    final BitmapFactory.Options options = new BitmapFactory.Options();
    ...
    BitmapFactory.decodeFile(filename, options);
    ...

    // If we're running on Honeycomb or newer, try to use inBitmap.
    if (Utils.hasHoneycomb()) {
        addInBitmapOptions(options, cache);
    }
    ...
    return BitmapFactory.decodeFile(filename, options);
}

পরবর্তী স্নিপেট addInBitmapOptions() পদ্ধতিটি দেখায় যা উপরের স্নিপেটে বলা হয়েছে। inBitmap মান হিসাবে সেট করার জন্য এটি একটি বিদ্যমান বিটম্যাপের সন্ধান করে। মনে রাখবেন যে এই পদ্ধতিটি শুধুমাত্র inBitmap জন্য একটি মান সেট করে যদি এটি একটি উপযুক্ত মিল খুঁজে পায় (আপনার কোডটি কখনই অনুমান করা উচিত নয় যে একটি মিল পাওয়া যাবে):

কোটলিন

private fun addInBitmapOptions(options: BitmapFactory.Options, cache: ImageCache?) {
    // inBitmap only works with mutable bitmaps, so force the decoder to
    // return mutable bitmaps.
    options.inMutable = true

    // Try to find a bitmap to use for inBitmap.
    cache?.getBitmapFromReusableSet(options)?.also { inBitmap ->
        // If a suitable bitmap has been found, set it as the value of
        // inBitmap.
        options.inBitmap = inBitmap
    }
}

// This method iterates through the reusable bitmaps, looking for one
// to use for inBitmap:
fun getBitmapFromReusableSet(options: BitmapFactory.Options): Bitmap? {
    mReusableBitmaps?.takeIf { it.isNotEmpty() }?.let { reusableBitmaps ->
        synchronized(reusableBitmaps) {
            val iterator: MutableIterator<SoftReference<Bitmap>> = reusableBitmaps.iterator()
            while (iterator.hasNext()) {
                iterator.next().get()?.let { item ->
                    if (item.isMutable) {
                        // Check to see it the item can be used for inBitmap.
                        if (canUseForInBitmap(item, options)) {
                            // Remove from reusable set so it can't be used again.
                            iterator.remove()
                            return item
                        }
                    } else {
                        // Remove from the set if the reference has been cleared.
                        iterator.remove()
                    }
                }
            }
        }
    }
    return null
}

জাভা

private static void addInBitmapOptions(BitmapFactory.Options options,
        ImageCache cache) {
    // inBitmap only works with mutable bitmaps, so force the decoder to
    // return mutable bitmaps.
    options.inMutable = true;

    if (cache != null) {
        // Try to find a bitmap to use for inBitmap.
        Bitmap inBitmap = cache.getBitmapFromReusableSet(options);

        if (inBitmap != null) {
            // If a suitable bitmap has been found, set it as the value of
            // inBitmap.
            options.inBitmap = inBitmap;
        }
    }
}

// This method iterates through the reusable bitmaps, looking for one
// to use for inBitmap:
protected Bitmap getBitmapFromReusableSet(BitmapFactory.Options options) {
        Bitmap bitmap = null;

    if (reusableBitmaps != null && !reusableBitmaps.isEmpty()) {
        synchronized (reusableBitmaps) {
            final Iterator<SoftReference<Bitmap>> iterator
                    = reusableBitmaps.iterator();
            Bitmap item;

            while (iterator.hasNext()) {
                item = iterator.next().get();

                if (null != item && item.isMutable()) {
                    // Check to see it the item can be used for inBitmap.
                    if (canUseForInBitmap(item, options)) {
                        bitmap = item;

                        // Remove from reusable set so it can't be used again.
                        iterator.remove();
                        break;
                    }
                } else {
                    // Remove from the set if the reference has been cleared.
                    iterator.remove();
                }
            }
        }
    }
    return bitmap;
}

অবশেষে, এই পদ্ধতিটি নির্ধারণ করে যে একজন প্রার্থী বিটম্যাপ inBitmap জন্য ব্যবহার করা আকারের মানদণ্ড পূরণ করে কিনা:

কোটলিন

private fun canUseForInBitmap(candidate: Bitmap, targetOptions: BitmapFactory.Options): Boolean {
    return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        // From Android 4.4 (KitKat) onward we can re-use if the byte size of
        // the new bitmap is smaller than the reusable bitmap candidate
        // allocation byte count.
        val width = ceil((targetOptions.outWidth * 1.0f / targetOptions.inSampleSize).toDouble()).toInt()
        val height = ceil((targetOptions.outHeight * 1.0f / targetOptions.inSampleSize).toDouble()).toInt()
        val byteCount: Int = width * height * getBytesPerPixel(candidate.config)
        byteCount <= candidate.allocationByteCount
    } else {
        // On earlier versions, the dimensions must match exactly and the inSampleSize must be 1
        candidate.width == targetOptions.outWidth
                && candidate.height == targetOptions.outHeight
                && targetOptions.inSampleSize == 1
    }
}

/**
 * A helper function to return the byte usage per pixel of a bitmap based on its configuration.
 */
private fun getBytesPerPixel(config: Bitmap.Config): Int {
    return when (config) {
        Bitmap.Config.ARGB_8888 -> 4
        Bitmap.Config.RGB_565, Bitmap.Config.ARGB_4444 -> 2
        Bitmap.Config.ALPHA_8 -> 1
        else -> 1
    }
}

জাভা

static boolean canUseForInBitmap(
        Bitmap candidate, BitmapFactory.Options targetOptions) {

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        // From Android 4.4 (KitKat) onward we can re-use if the byte size of
        // the new bitmap is smaller than the reusable bitmap candidate
        // allocation byte count.
        int width = (int) Math.ceil(targetOptions.outWidth * 1.0f / targetOptions.inSampleSize);
        int height = (int) Math.ceil(targetOptions.outHeight * 1.0f / targetOptions.inSampleSize);
        int byteCount = width * height * getBytesPerPixel(candidate.getConfig());
        return byteCount <= candidate.getAllocationByteCount();
    }

    // On earlier versions, the dimensions must match exactly and the inSampleSize must be 1
    return candidate.getWidth() == targetOptions.outWidth
            && candidate.getHeight() == targetOptions.outHeight
            && targetOptions.inSampleSize == 1;
}

/**
 * A helper function to return the byte usage per pixel of a bitmap based on its configuration.
 */
static int getBytesPerPixel(Config config) {
    if (config == Config.ARGB_8888) {
        return 4;
    } else if (config == Config.RGB_565) {
        return 2;
    } else if (config == Config.ARGB_4444) {
        return 2;
    } else if (config == Config.ALPHA_8) {
        return 1;
    }
    return 1;
}