use of org.neo4j.memory.MemoryTracker in project neo4j by neo4j.
the class CapacityLimitingBlockAllocatorDecoratorTest method maxMemoryLimit.
@Test
void maxMemoryLimit() {
final MemoryTracker tracker = mock(MemoryTracker.class);
final OffHeapBlockAllocator allocator = mock(OffHeapBlockAllocator.class);
when(allocator.allocate(anyLong(), any(MemoryTracker.class))).then(invocation -> {
final long size = invocation.<Long>getArgument(0);
return new MemoryBlock(0, size);
});
final CapacityLimitingBlockAllocatorDecorator decorator = new CapacityLimitingBlockAllocatorDecorator(allocator, 1024);
final List<MemoryBlock> blocks = new ArrayList<>();
for (int i = 0; i < 8; i++) {
final MemoryBlock block = decorator.allocate(128, tracker);
blocks.add(block);
}
assertThrows(MemoryAllocationLimitException.class, () -> decorator.allocate(128, tracker));
decorator.free(blocks.remove(0), tracker);
assertDoesNotThrow(() -> decorator.allocate(128, tracker));
assertThrows(MemoryAllocationLimitException.class, () -> decorator.allocate(256, tracker));
decorator.free(blocks.remove(0), tracker);
assertThrows(MemoryAllocationLimitException.class, () -> decorator.allocate(256, tracker));
decorator.free(blocks.remove(0), tracker);
assertDoesNotThrow(() -> decorator.allocate(256, tracker));
}
use of org.neo4j.memory.MemoryTracker in project neo4j by neo4j.
the class MutableLinearProbeLongHashSetTest method freeFrozenMemory.
@Test
void freeFrozenMemory() {
final MemoryTracker memoryTrackerSpy = spy(new LocalMemoryTracker());
final MutableLinearProbeLongHashSet set2 = new MutableLinearProbeLongHashSet(new OffHeapMemoryAllocator(blockAllocator), memoryTrackerSpy);
verify(memoryTrackerSpy).allocateNative(anyLong());
set2.addAll(100, 200, 300);
set2.freeze();
set2.remove(100);
set2.freeze();
set2.clear();
set2.close();
verify(memoryTrackerSpy, times(3)).releaseNative(anyLong());
}
use of org.neo4j.memory.MemoryTracker in project neo4j by neo4j.
the class InCacheTest method shouldTrackMemory.
@Test
void shouldTrackMemory() {
// given
InCache cache = new InCache();
ListValue list = list(stringValue("a"), stringValue("b"), stringValue("c"));
// when
MemoryTracker memoryTracker = mock(MemoryTracker.class);
cache.check(stringValue("a"), list, memoryTracker);
// then
ArgumentCaptor<Long> arg = ArgumentCaptor.forClass(Long.class);
verify(memoryTracker).allocateHeap(arg.capture());
assertThat(arg.getValue()).isGreaterThan(0);
}
use of org.neo4j.memory.MemoryTracker in project neo4j by neo4j.
the class UnsafeDirectByteBufferFactoryTest method shouldAllocateBuffer.
@Test
void shouldAllocateBuffer() {
// given
MemoryTracker tracker = new LocalMemoryTracker();
try (UnsafeDirectByteBufferAllocator factory = new UnsafeDirectByteBufferAllocator()) {
// when
int bufferSize = 128;
factory.allocate(bufferSize, tracker);
// then
assertEquals(bufferSize, tracker.usedNativeMemory());
}
}
Aggregations