diff --git a/community/io/src/main/java/org/neo4j/io/mem/GrabAllocator.java b/community/io/src/main/java/org/neo4j/io/mem/GrabAllocator.java index 7433069c6c49d..4cb2f6d343b8e 100644 --- a/community/io/src/main/java/org/neo4j/io/mem/GrabAllocator.java +++ b/community/io/src/main/java/org/neo4j/io/mem/GrabAllocator.java @@ -166,13 +166,8 @@ private void initCause( NativeMemoryAllocationRefusedError error, OutOfMemoryErr protected synchronized void finalize() throws Throwable { super.finalize(); - close(); - } - - @Override - public void close() - { Grab current = grabs; + while ( current != null ) { current.free(); diff --git a/community/io/src/main/java/org/neo4j/io/mem/MemoryAllocator.java b/community/io/src/main/java/org/neo4j/io/mem/MemoryAllocator.java index 25050bb9623a8..856d05667817c 100644 --- a/community/io/src/main/java/org/neo4j/io/mem/MemoryAllocator.java +++ b/community/io/src/main/java/org/neo4j/io/mem/MemoryAllocator.java @@ -50,9 +50,4 @@ static MemoryAllocator createAllocator( String expectedMemory, MemoryAllocationT * @throws OutOfMemoryError if the requested memory could not be allocated. */ long allocateAligned( long bytes, long alignment ); - - /** - * Close allocator and release all allocated resources - */ - void close(); } diff --git a/community/io/src/test/java/org/neo4j/io/mem/MemoryAllocatorTest.java b/community/io/src/test/java/org/neo4j/io/mem/MemoryAllocatorTest.java index 65d47520a6c04..d1aab5ffebfb7 100644 --- a/community/io/src/test/java/org/neo4j/io/mem/MemoryAllocatorTest.java +++ b/community/io/src/test/java/org/neo4j/io/mem/MemoryAllocatorTest.java @@ -119,10 +119,10 @@ public void allocatingMustIncreaseMemoryUsedAndDecreaseAvailableMemory() throws } @Test - public void trackMemoryAllocations() + public void trackMemoryAllocations() throws Throwable { LocalMemoryTracker memoryTracker = new LocalMemoryTracker(); - MemoryAllocator allocator = MemoryAllocator.createAllocator( "2m", memoryTracker ); + GrabAllocator allocator = (GrabAllocator) MemoryAllocator.createAllocator( "2m", memoryTracker ); assertEquals( 0, memoryTracker.usedDirectMemory() ); @@ -130,7 +130,8 @@ public void trackMemoryAllocations() assertEquals( ByteUnit.mebiBytes( 1 ), memoryTracker.usedDirectMemory() ); - allocator.close(); + //noinspection FinalizeCalledExplicitly + allocator.finalize(); assertEquals( 0, memoryTracker.usedDirectMemory() ); } } diff --git a/community/io/src/test/java/org/neo4j/io/pagecache/impl/muninn/LargePageListIT.java b/community/io/src/test/java/org/neo4j/io/pagecache/impl/muninn/LargePageListIT.java index eb3fbc3a3b7fd..9769ae6a02c01 100644 --- a/community/io/src/test/java/org/neo4j/io/pagecache/impl/muninn/LargePageListIT.java +++ b/community/io/src/test/java/org/neo4j/io/pagecache/impl/muninn/LargePageListIT.java @@ -26,7 +26,6 @@ import org.neo4j.io.ByteUnit; import org.neo4j.io.mem.MemoryAllocator; import org.neo4j.memory.GlobalMemoryTracker; -import org.neo4j.memory.LocalMemoryTracker; import static org.hamcrest.Matchers.is; import static org.junit.Assert.assertEquals; @@ -45,7 +44,7 @@ public void veryLargePageListsMustBeFullyAccessible() throws Exception long pageCacheSize = ByteUnit.gibiBytes( 513 ) + pageSize; int pages = Math.toIntExact( pageCacheSize / pageSize ); - MemoryAllocator mman = MemoryAllocator.createAllocator( "2 GiB", new LocalMemoryTracker() ); + MemoryAllocator mman = MemoryAllocator.createAllocator( "2 GiB", GlobalMemoryTracker.INSTANCE ); SwapperSet swappers = new SwapperSet(); long victimPage = VictimPageReference.getVictimPage( pageSize, GlobalMemoryTracker.INSTANCE ); diff --git a/community/io/src/test/java/org/neo4j/io/pagecache/impl/muninn/PageListTest.java b/community/io/src/test/java/org/neo4j/io/pagecache/impl/muninn/PageListTest.java index 4888083c2748f..77a3a8636fed8 100644 --- a/community/io/src/test/java/org/neo4j/io/pagecache/impl/muninn/PageListTest.java +++ b/community/io/src/test/java/org/neo4j/io/pagecache/impl/muninn/PageListTest.java @@ -51,7 +51,6 @@ import org.neo4j.io.pagecache.tracing.FlushEventOpportunity; import org.neo4j.io.pagecache.tracing.PageFaultEvent; import org.neo4j.memory.GlobalMemoryTracker; -import org.neo4j.memory.LocalMemoryTracker; import org.neo4j.unsafe.impl.internal.dragons.UnsafeUtil; import static org.hamcrest.Matchers.equalTo; @@ -89,7 +88,7 @@ public static Iterable parameters() public static void setUpStatics() { executor = Executors.newCachedThreadPool( new DaemonThreadFactory() ); - mman = MemoryAllocator.createAllocator( "1 MiB", new LocalMemoryTracker() ); + mman = MemoryAllocator.createAllocator( "1 MiB", GlobalMemoryTracker.INSTANCE ); } @AfterClass diff --git a/community/io/src/test/java/org/neo4j/io/pagecache/impl/muninn/SequenceLockStressIT.java b/community/io/src/test/java/org/neo4j/io/pagecache/impl/muninn/SequenceLockStressIT.java index cb507294fc332..4e7fbfb46b7aa 100644 --- a/community/io/src/test/java/org/neo4j/io/pagecache/impl/muninn/SequenceLockStressIT.java +++ b/community/io/src/test/java/org/neo4j/io/pagecache/impl/muninn/SequenceLockStressIT.java @@ -35,7 +35,6 @@ import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicInteger; -import org.neo4j.memory.GlobalMemoryTracker; import org.neo4j.test.rule.RepeatRule; import org.neo4j.unsafe.impl.internal.dragons.UnsafeUtil; @@ -57,7 +56,7 @@ public static void shutDownExecutor() @Before public void allocateLock() { - lockAddr = UnsafeUtil.allocateMemory( Long.BYTES, GlobalMemoryTracker.INSTANCE ); + lockAddr = UnsafeUtil.allocateMemory( Long.BYTES ); UnsafeUtil.putLong( lockAddr, 0 ); } diff --git a/community/primitive-collections/src/main/java/org/neo4j/collection/primitive/Primitive.java b/community/primitive-collections/src/main/java/org/neo4j/collection/primitive/Primitive.java index eb46a32a66b65..cc8206fb1ccb8 100644 --- a/community/primitive-collections/src/main/java/org/neo4j/collection/primitive/Primitive.java +++ b/community/primitive-collections/src/main/java/org/neo4j/collection/primitive/Primitive.java @@ -36,6 +36,7 @@ import org.neo4j.collection.primitive.hopscotch.PrimitiveLongIntHashMap; import org.neo4j.collection.primitive.hopscotch.PrimitiveLongLongHashMap; import org.neo4j.collection.primitive.hopscotch.PrimitiveLongObjectHashMap; +import org.neo4j.memory.GlobalMemoryTracker; import org.neo4j.memory.MemoryAllocationTracker; import static org.neo4j.collection.primitive.hopscotch.HopScotchHashingAlgorithm.NO_MONITOR; @@ -83,6 +84,11 @@ public static PrimitiveLongSet longSet( int initialCapacity ) VALUE_MARKER, NO_MONITOR ); } + public static PrimitiveLongSet offHeapLongSet() + { + return offHeapLongSet( GlobalMemoryTracker.INSTANCE ); + } + public static PrimitiveLongSet offHeapLongSet( MemoryAllocationTracker allocationTracker ) { return offHeapLongSet( DEFAULT_OFFHEAP_CAPACITY, allocationTracker ); @@ -114,6 +120,11 @@ public static PrimitiveLongLongMap longLongMap( int initialCapacity ) return new PrimitiveLongLongHashMap( new LongKeyLongValueTable( initialCapacity ), NO_MONITOR ); } + public static PrimitiveLongLongMap offHeapLongLongMap() + { + return offHeapLongLongMap( GlobalMemoryTracker.INSTANCE ); + } + public static PrimitiveLongLongMap offHeapLongLongMap( MemoryAllocationTracker allocationTracker ) { return offHeapLongLongMap( DEFAULT_OFFHEAP_CAPACITY, allocationTracker ); @@ -145,6 +156,11 @@ public static PrimitiveIntSet intSet( int initialCapacity ) VALUE_MARKER, NO_MONITOR ); } + public static PrimitiveIntSet offHeapIntSet() + { + return offHeapIntSet( GlobalMemoryTracker.INSTANCE ); + } + public static PrimitiveIntSet offHeapIntSet( MemoryAllocationTracker allocationTracker ) { return new PrimitiveIntHashSet( new IntKeyUnsafeTable<>( DEFAULT_OFFHEAP_CAPACITY, VALUE_MARKER, allocationTracker ), diff --git a/community/primitive-collections/src/main/java/org/neo4j/collection/primitive/hopscotch/UnsafeTable.java b/community/primitive-collections/src/main/java/org/neo4j/collection/primitive/hopscotch/UnsafeTable.java index 0ca18aee5994e..69613970b5be4 100644 --- a/community/primitive-collections/src/main/java/org/neo4j/collection/primitive/hopscotch/UnsafeTable.java +++ b/community/primitive-collections/src/main/java/org/neo4j/collection/primitive/hopscotch/UnsafeTable.java @@ -59,7 +59,7 @@ protected UnsafeTable( int capacity, int bytesPerKey, VALUE valueMarker, MemoryA if ( UnsafeUtil.allowUnalignedMemoryAccess ) { allocatedBytes = dataSize; - this.allocatedAddress = this.address = allocateMemory( allocatedBytes ); + this.allocatedAddress = this.address = UnsafeUtil.allocateMemory( allocatedBytes, this.allocationTracker ); } else { @@ -75,7 +75,7 @@ protected UnsafeTable( int capacity, int bytesPerKey, VALUE valueMarker, MemoryA } allocatedBytes = dataSize + Integer.BYTES - 1; - this.allocatedAddress = allocateMemory( allocatedBytes ); + this.allocatedAddress = UnsafeUtil.allocateMemory( allocatedBytes, this.allocationTracker ); this.address = UnsafeUtil.alignedMemory( allocatedAddress, Integer.BYTES ); } @@ -196,7 +196,7 @@ public void removeHopBit( int index, int hd ) @Override public void close() { - deallocateMemory(); + UnsafeUtil.free( allocatedAddress, allocatedBytes, allocationTracker ); } protected static void alignmentSafePutLongAsTwoInts( long address, long value ) @@ -226,13 +226,4 @@ protected static long alignmentSafeGetLongAsTwoInts( long address ) return lsb | (msb << Integer.SIZE); } - private long allocateMemory( long bytesToAllocate ) - { - return UnsafeUtil.allocateMemory( bytesToAllocate, allocationTracker ); - } - - private void deallocateMemory() - { - UnsafeUtil.free( allocatedAddress, allocatedBytes, allocationTracker ); - } } diff --git a/community/unsafe/src/main/java/org/neo4j/unsafe/impl/internal/dragons/UnsafeUtil.java b/community/unsafe/src/main/java/org/neo4j/unsafe/impl/internal/dragons/UnsafeUtil.java index 79f5093043a92..5426df4c4c714 100644 --- a/community/unsafe/src/main/java/org/neo4j/unsafe/impl/internal/dragons/UnsafeUtil.java +++ b/community/unsafe/src/main/java/org/neo4j/unsafe/impl/internal/dragons/UnsafeUtil.java @@ -38,6 +38,7 @@ import java.util.concurrent.atomic.AtomicLong; import java.util.stream.Collectors; +import org.neo4j.memory.GlobalMemoryTracker; import org.neo4j.memory.MemoryAllocationTracker; import static java.lang.String.format; @@ -346,6 +347,11 @@ public static String newSharedArrayString( char[] chars ) } } + public static long allocateMemory( long sizeInBytes ) + { + return allocateMemory( sizeInBytes, GlobalMemoryTracker.INSTANCE ); + } + /** * Allocate a block of memory of the given size in bytes, and return a pointer to that memory. *

diff --git a/community/unsafe/src/test/java/org/neo4j/unsafe/impl/internal/dragons/UnsafeUtilTest.java b/community/unsafe/src/test/java/org/neo4j/unsafe/impl/internal/dragons/UnsafeUtilTest.java index 79356a50042a0..b75140a58e30b 100644 --- a/community/unsafe/src/test/java/org/neo4j/unsafe/impl/internal/dragons/UnsafeUtilTest.java +++ b/community/unsafe/src/test/java/org/neo4j/unsafe/impl/internal/dragons/UnsafeUtilTest.java @@ -276,7 +276,7 @@ public void mustSupportReadingFromAndWritingToFields() public void mustSupportReadingAndWritingOfPrimitivesToMemory() { int sizeInBytes = 8; - long address = allocateMemory( sizeInBytes, GlobalMemoryTracker.INSTANCE ); + long address = allocateMemory( sizeInBytes ); try { putByte( address, (byte) 1 ); @@ -483,7 +483,7 @@ public void unsafeArrayElementAccess() public void directByteBufferCreationAndInitialisation() throws Exception { int sizeInBytes = 313; - long address = allocateMemory( sizeInBytes, GlobalMemoryTracker.INSTANCE ); + long address = allocateMemory( sizeInBytes ); try { setMemory( address, sizeInBytes, (byte) 0 ); @@ -504,7 +504,7 @@ public void directByteBufferCreationAndInitialisation() throws Exception a.limit( 202 ); int sizeInBytes2 = 424; - long address2 = allocateMemory( sizeInBytes2, GlobalMemoryTracker.INSTANCE ); + long address2 = allocateMemory( sizeInBytes2 ); try { setMemory( address2, sizeInBytes2, (byte) 0 );