SegmentBufferMonitor { public void trackAllocation(@NotNull Buffer buffer) { BufferReference reference = new BufferReference(buffer, referenceQueue); buffers.add(reference); allocated(reference); trackDeallocations(); } SegmentBufferMonitor(@NotNull StatisticsProvider statisticsProvider); void trackAllocation(@NotNull Buffer buffer); static final String DIRECT_BUFFER_COUNT; static final String DIRECT_BUFFER_CAPACITY; static final String HEAP_BUFFER_COUNT; static final String HEAP_BUFFER_CAPACITY; }
@Test public void heapBuffer() { Buffer buffer = Buffer.allocate(42); segmentBufferMonitor.trackAllocation(buffer); assertEquals(0, stats.get(DIRECT_BUFFER_COUNT).getCount()); assertEquals(0, stats.get(DIRECT_BUFFER_CAPACITY).getCount()); assertEquals(1, stats.get(HEAP_BUFFER_COUNT).getCount()); assertEquals(42, stats.get(HEAP_BUFFER_CAPACITY).getCount()); buffer = null; System.gc(); assertEquals(0, stats.get(DIRECT_BUFFER_COUNT).getCount()); assertEquals(0, stats.get(DIRECT_BUFFER_CAPACITY).getCount()); assertTrue(stats.get(HEAP_BUFFER_COUNT).getCount() <= 1); assertTrue(stats.get(HEAP_BUFFER_CAPACITY).getCount() <= 42); }
@Test public void directBuffer() { Buffer buffer = Buffer.allocateDirect(42); segmentBufferMonitor.trackAllocation(buffer); assertEquals(1, stats.get(DIRECT_BUFFER_COUNT).getCount()); assertEquals(42, stats.get(DIRECT_BUFFER_CAPACITY).getCount()); assertEquals(0, stats.get(HEAP_BUFFER_COUNT).getCount()); assertEquals(0, stats.get(HEAP_BUFFER_CAPACITY).getCount()); buffer = null; System.gc(); assertTrue(stats.get(DIRECT_BUFFER_COUNT).getCount() <= 1); assertTrue(stats.get(DIRECT_BUFFER_CAPACITY).getCount() <= 42); assertEquals(0, stats.get(HEAP_BUFFER_COUNT).getCount()); assertEquals(0, stats.get(HEAP_BUFFER_CAPACITY).getCount()); }