BufferPool

class BufferPool(maxBuffersPerSizeClass: Int = 8, maxRetainedBytes: Long = 16 * 1024 * 1024)

Created by pedro on 29/7/26.

Pool of reusable ByteArray to avoid allocating one per encoded frame.

An encoded frame must be copied out of the MediaCodec output buffer before giving it back to the codec, so the copy is unavoidable, but the allocation is not. At 6Mbps a stream allocates around 750KB/s of short lived arrays, and all of it ends in the GC.

Buffers are grouped in power of two size classes so a frame gets the smallest available array able to contain it. Arrays are handed out dirty (never zeroed) because the caller always writes the whole frame and reads it back limited to the frame size.

Thread safe: acquired from the encoder thread and released from the sender thread.

Constructors

Link copied to clipboard
constructor(maxBuffersPerSizeClass: Int = 8, maxRetainedBytes: Long = 16 * 1024 * 1024)

Types

Link copied to clipboard
object Companion

Functions

Link copied to clipboard
fun acquire(minSize: Int): ByteArray

Return an array of at least minSize bytes. The content is undefined.

Link copied to clipboard
fun clear()
Link copied to clipboard
Link copied to clipboard
fun release(buffer: ByteBuffer)

fun release(buffer: ByteArray)

Give an array back to the pool. Arrays not created by acquire, and arrays already released, are ignored, so the same array can never be handed out to two users at once.