Search in sources :

Example 41 with MemoryTracker

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));
}
Also used : MemoryBlock(org.neo4j.kernel.impl.util.collection.OffHeapBlockAllocator.MemoryBlock) ArgumentMatchers.anyLong(org.mockito.ArgumentMatchers.anyLong) ArrayList(java.util.ArrayList) MemoryTracker(org.neo4j.memory.MemoryTracker) Test(org.junit.jupiter.api.Test)

Example 42 with MemoryTracker

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());
}
Also used : LocalMemoryTracker(org.neo4j.memory.LocalMemoryTracker) EmptyMemoryTracker(org.neo4j.memory.EmptyMemoryTracker) LocalMemoryTracker(org.neo4j.memory.LocalMemoryTracker) MemoryTracker(org.neo4j.memory.MemoryTracker) Test(org.junit.jupiter.api.Test)

Example 43 with MemoryTracker

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);
}
Also used : ListValue(org.neo4j.values.virtual.ListValue) EmptyMemoryTracker(org.neo4j.memory.EmptyMemoryTracker) MemoryTracker(org.neo4j.memory.MemoryTracker) Test(org.junit.jupiter.api.Test)

Example 44 with MemoryTracker

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());
    }
}
Also used : LocalMemoryTracker(org.neo4j.memory.LocalMemoryTracker) LocalMemoryTracker(org.neo4j.memory.LocalMemoryTracker) MemoryTracker(org.neo4j.memory.MemoryTracker) Test(org.junit.jupiter.api.Test)

Aggregations

MemoryTracker (org.neo4j.memory.MemoryTracker)44 Test (org.junit.jupiter.api.Test)22 LocalMemoryTracker (org.neo4j.memory.LocalMemoryTracker)10 CursorContext (org.neo4j.io.pagecache.context.CursorContext)9 Value (org.neo4j.values.storable.Value)6 Transaction (org.neo4j.graphdb.Transaction)5 InternalTransaction (org.neo4j.kernel.impl.coreapi.InternalTransaction)5 Map (java.util.Map)4 Optional (java.util.Optional)4 IndexDescriptor (org.neo4j.internal.schema.IndexDescriptor)4 SchemaDescriptor (org.neo4j.internal.schema.SchemaDescriptor)4 Log (org.neo4j.logging.Log)4 ValueTuple (org.neo4j.values.storable.ValueTuple)4 URI (java.net.URI)3 ArrayList (java.util.ArrayList)3 Collections.emptyMap (java.util.Collections.emptyMap)3 Objects.requireNonNullElse (java.util.Objects.requireNonNullElse)3 HttpServletRequest (javax.servlet.http.HttpServletRequest)3 IOException (java.io.IOException)2 Arrays (java.util.Arrays)2