diff --git a/java/memory/src/main/java/org/apache/arrow/memory/AllocationManager.java b/java/memory/src/main/java/org/apache/arrow/memory/AllocationManager.java index e277ec0da90..50c0908b4f0 100644 --- a/java/memory/src/main/java/org/apache/arrow/memory/AllocationManager.java +++ b/java/memory/src/main/java/org/apache/arrow/memory/AllocationManager.java @@ -53,7 +53,6 @@ public abstract class AllocationManager { // see JIRA for details private final LowCostIdentityHashMap map = new LowCostIdentityHashMap<>(); private final long amCreationTime = System.nanoTime(); - private final long size; // The ReferenceManager created at the time of creation of this AllocationManager // is treated as the owning reference manager for the underlying chunk of memory @@ -61,11 +60,10 @@ public abstract class AllocationManager { private volatile BufferLedger owningLedger; private volatile long amDestructionTime = 0; - protected AllocationManager(BaseAllocator accountingAllocator, long size) { + protected AllocationManager(BaseAllocator accountingAllocator) { Preconditions.checkNotNull(accountingAllocator); accountingAllocator.assertOpen(); - this.size = size; this.root = accountingAllocator.root; // we do a no retain association since our creator will want to retrieve the newly created @@ -181,11 +179,11 @@ void release(final BufferLedger ledger) { /** * Return the size of underlying chunk of memory managed by this Allocation Manager. * + *

The underlying memory chunk managed can be different from the original requested size. + * * @return size of underlying memory chunk */ - public long getSize() { - return size; - } + public abstract long getSize(); /** * Return the absolute memory address pointing to the fist byte of underling memory chunk. diff --git a/java/memory/src/main/java/org/apache/arrow/memory/NettyAllocationManager.java b/java/memory/src/main/java/org/apache/arrow/memory/NettyAllocationManager.java index 1a3824162d3..35b02f54ecd 100644 --- a/java/memory/src/main/java/org/apache/arrow/memory/NettyAllocationManager.java +++ b/java/memory/src/main/java/org/apache/arrow/memory/NettyAllocationManager.java @@ -34,13 +34,13 @@ public class NettyAllocationManager extends AllocationManager { static final UnsafeDirectLittleEndian EMPTY = INNER_ALLOCATOR.empty; static final long CHUNK_SIZE = INNER_ALLOCATOR.getChunkSize(); - private final int size; + private final int allocatedSize; private final UnsafeDirectLittleEndian memoryChunk; - NettyAllocationManager(BaseAllocator accountingAllocator, int size) { - super(accountingAllocator, size); - this.memoryChunk = INNER_ALLOCATOR.allocate(size); - this.size = memoryChunk.capacity(); + NettyAllocationManager(BaseAllocator accountingAllocator, int requestedSize) { + super(accountingAllocator); + this.memoryChunk = INNER_ALLOCATOR.allocate(requestedSize); + this.allocatedSize = memoryChunk.capacity(); } /** @@ -61,9 +61,14 @@ protected void release0() { memoryChunk.release(); } + /** + * Returns the underlying memory chunk size managed. + * + *

NettyAllocationManager rounds requested size up to the next power of two. + */ @Override public long getSize() { - return size; + return allocatedSize; } /** diff --git a/java/memory/src/test/java/org/apache/arrow/memory/TestBaseAllocator.java b/java/memory/src/test/java/org/apache/arrow/memory/TestBaseAllocator.java index f01ff653b10..2be96c0c506 100644 --- a/java/memory/src/test/java/org/apache/arrow/memory/TestBaseAllocator.java +++ b/java/memory/src/test/java/org/apache/arrow/memory/TestBaseAllocator.java @@ -391,9 +391,9 @@ public void testCustomizedAllocationManager() { private BaseAllocator createAllocatorWithCustomizedAllocationManager() { return new RootAllocator(BaseAllocator.configBuilder() .maxAllocation(MAX_ALLOCATION) - .allocationManagerFactory((accountingAllocator, size) -> new AllocationManager(accountingAllocator, size) { + .allocationManagerFactory((accountingAllocator, requestedSize) -> new AllocationManager(accountingAllocator) { private final Unsafe unsafe = getUnsafe(); - private final long address = unsafe.allocateMemory(size); + private final long address = unsafe.allocateMemory(requestedSize); @Override protected long memoryAddress() { @@ -402,10 +402,15 @@ protected long memoryAddress() { @Override protected void release0() { - unsafe.setMemory(address, size, (byte) 0); + unsafe.setMemory(address, requestedSize, (byte) 0); unsafe.freeMemory(address); } + @Override + public long getSize() { + return requestedSize; + } + private Unsafe getUnsafe() { Field f = null; try {