@SuppressWarnings("unchecked")
@Test
public void testUsedHeapMemory() {

    T allocator =  newAllocator(true);
    ByteBufAllocatorMetric metric = ((ByteBufAllocatorMetricProvider) allocator).metric();
    assertEquals(0, metric.usedHeapMemory());
    ByteBuf buffer = allocator.heapBuffer(1024, 4096);
    int capacity = buffer.capacity();
    assertEquals(expectedUsedMemory(allocator, capacity), metric.usedHeapMemory());
    // Double the size of the buffer
    buffer.capacity(capacity << 1);
    capacity = buffer.capacity();
    assertEquals(expectedUsedMemory(allocator, capacity), metric.usedHeapMemory());
    buffer.release();
    assertEquals(expectedUsedMemoryAfterRelease(allocator, capacity), metric.usedHeapMemory());
}