Class BitmapPool

  • All Implemented Interfaces:

    
    public final class BitmapPool
    
                        

    A thread-safe pool of reusable Bitmap objects to reduce allocation overhead during screen recording.

    The pool maintains a maximum number of bitmaps based on device memory availability:

    • Standard devices: 3 bitmaps

    • Low-RAM devices: 2 bitmaps

    This helps prevent excessive memory usage while still providing performance benefits from bitmap reuse.

    Thread Safety: All methods are thread-safe and can be called from any thread.

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
    • Field Summary

      Fields 
      Modifier and Type Field Description
    • Enum Constant Summary

      Enum Constants 
      Enum Constant Description
    • Method Summary

      Modifier and Type Method Description
      final Bitmap acquire(Integer width, Integer height) Acquires a Bitmap from the pool or creates a new one if no match is found.
      final Unit release(Bitmap bitmap) Releases a Bitmap back to the pool for reuse.
      final Unit clear() Clears the pool, recycling all pooled bitmaps.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • BitmapPool

        BitmapPool(Context context)
    • Method Detail

      • acquire

         final Bitmap acquire(Integer width, Integer height)

        Acquires a Bitmap from the pool or creates a new one if no match is found.

        Searches the pool for a bitmap matching the requested dimensions. Non-matching bitmaps remain in the pool for future use.

        Parameters:
        width - The desired width of the bitmap
        height - The desired height of the bitmap
        Returns:

        A Bitmap ready for use, or null if allocation fails (e.g., OutOfMemoryError)

      • release

         final Unit release(Bitmap bitmap)

        Releases a Bitmap back to the pool for reuse.

        If the pool is at maximum capacity, the bitmap will be recycled instead.

        Parameters:
        bitmap - The bitmap to release
      • clear

         final Unit clear()

        Clears the pool, recycling all pooled bitmaps.

        This should be called when screen recording stops to free memory.