public void beforeTest() {
    if (cachedEnabled) {
      return;
    }
    PooledByteBufAllocatorMetric metric = PooledByteBufAllocator.DEFAULT.metric();
    List<PoolArenaMetric> heaps = metric.heapArenas();
    activeHeapAllocations = heaps.stream().mapToLong(PoolArenaMetric::numActiveAllocations).sum();
    heapAllocations = heaps.stream().mapToLong(PoolArenaMetric::numAllocations).sum();
    heapDeallocations = heaps.stream().mapToLong(PoolArenaMetric::numDeallocations).sum();

    List<PoolArenaMetric> directs = metric.directArenas();
    activeDirectAllocations = directs.stream().mapToLong(PoolArenaMetric::numActiveAllocations).sum();
    directAllocations = directs.stream().mapToLong(PoolArenaMetric::numAllocations).sum();
    directDeallocations = directs.stream().mapToLong(PoolArenaMetric::numDeallocations).sum();
  }
Method to call at any method that is tagged {@link org.junit.Before} to collect some allocation stats.
ambry-test-utils/src/main/java/com/github/ambry/utils/NettyByteBufLeakHelper.java