### Question:
PooledMemoryManager implements MemoryManager<Buffer>, WrapperAware { Pool[] getPools() { return Arrays.copyOf(pools, pools.length); } PooledMemoryManager(); @SuppressWarnings("unused")  PooledMemoryManager(final boolean isDirect);  PooledMemoryManager(final int baseBufferSize, final int numberOfPools, final int growthFactor, final int numberOfPoolSlices,
            final float percentOfHeap, final float percentPreallocated, final boolean isDirect); @Override Buffer allocate(final int size); @Override Buffer allocateAtLeast(int size); @Override Buffer reallocate(final Buffer oldBuffer, final int newSize); @Override void release(final Buffer buffer); @Override boolean willAllocateDirect(final int size); @Override MonitoringConfig<MemoryProbe> getMonitoringConfig(); @Override Buffer wrap(final byte[] data); @Override Buffer wrap(byte[] data, int offset, int length); @Override Buffer wrap(final String s); @Override Buffer wrap(final String s, final Charset charset); @Override Buffer wrap(final ByteBuffer byteBuffer); static final int DEFAULT_BASE_BUFFER_SIZE; static final int DEFAULT_NUMBER_OF_POOLS; static final int DEFAULT_GROWTH_FACTOR; static final float DEFAULT_HEAP_USAGE_PERCENTAGE; static final float DEFAULT_PREALLOCATED_BUFFERS_PERCENTAGE; }### Answer:
@Test public void testDefaultPoolInitialization() throws Exception { final int numProcessors = Runtime.getRuntime().availableProcessors(); final long maxMemory = Runtime.getRuntime().maxMemory(); final long totalMemory = (long) (maxMemory * DEFAULT_HEAP_USAGE_PERCENTAGE); final long upperBound = (long) (maxMemory * .105f); PooledMemoryManager mm = new PooledMemoryManager(); PooledMemoryManager.Pool[] pools = mm.getPools(); assertEquals(DEFAULT_NUMBER_OF_POOLS, pools.length); int bufSize = DEFAULT_BASE_BUFFER_SIZE; for (int i = 0, len = pools.length; i < len; i++) { assertEquals(bufSize, pools[i].getBufferSize()); bufSize <<= DEFAULT_GROWTH_FACTOR; } PooledMemoryManager.PoolSlice[] slices = pools[0].getSlices(); assertEquals(numProcessors, slices.length); assertEquals(slices[0].getMaxElementsCount(), slices[0].elementsCount()); long consumedMemory = 0; for (int i = 0, len = pools.length; i < len; i++) { consumedMemory += pools[i].size(); } assertTrue(consumedMemory + " >= " + totalMemory + " failed", consumedMemory >= totalMemory); assertTrue(consumedMemory + " <= " + upperBound + " failed", consumedMemory <= upperBound); }
@Test public void testCustomizedPoolInitialization() throws Exception { final float memoryPercentage = 0.05f; final int processors = Runtime.getRuntime().availableProcessors(); final long maxMemory = Runtime.getRuntime().maxMemory(); PooledMemoryManager mm = new PooledMemoryManager(2048, 1, 0, processors, memoryPercentage, 1.0f, isDirect); final long memoryPerPool = (long) (maxMemory * memoryPercentage); final long upperBound = (long) (maxMemory * 0.0505f); PooledMemoryManager.Pool[] pools = mm.getPools(); assertEquals(1, pools.length); assertTrue(pools[0].size() + " >= " + memoryPerPool + " failed", pools[0].size() >= memoryPerPool); assertTrue(pools[0].size() + " <= " + upperBound + " failed", pools[0].size() <= upperBound); assertEquals(2048, pools[0].getBufferSize()); final float preallocatedPercentage = 0.25f; mm = new PooledMemoryManager(2048, 1, 0, processors, memoryPercentage, preallocatedPercentage, isDirect); pools = mm.getPools(); final PoolSlice slice0 = pools[0].getSlices()[0]; assertEquals((int) (slice0.getMaxElementsCount() * preallocatedPercentage), slice0.elementsCount()); }