@Override
    public void release(ByteBuffer previouslyAllocated) {
        lock.lock();
        try {
            previouslyAllocated.clear();

            if (previouslyAllocated.capacity() != batchSize) {
                throw new IllegalArgumentException("Released buffer with unexpected size "
                    + previouslyAllocated.capacity());
            }

            // Free the buffer if the number of pooled buffers is already the maximum number of batches.
            // Otherwise return the buffer to the memory pool.
            if (free.size() >= maxRetainedBatches) {
                numAllocatedBatches--;
            } else {
                free.offer(previouslyAllocated);
            }
        } finally {
            lock.unlock();
        }
    }
Release a previously allocated byte buffer.

The byte buffer is pooled if the number of pooled byte buffer is less than the maxRetainedBatches in
the constructor. Otherwise, the byte buffer is return to the JVM for garbage collection.
raft/src/main/java/org/apache/kafka/raft/internals/BatchMemoryPool.java