Skip to content

Commit

Permalink
Unlink local memory tracker from global one
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrei Koval committed Jul 31, 2018
1 parent c7dc5ab commit 5799fd6
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 26 deletions.
Expand Up @@ -22,7 +22,6 @@
/**
* Memory allocation tracker that can be used in local context that required
* tracking of memory that is independent from global.
* All allocations/de-allocation reported to this trackers also will be reported to global tracker transparently.
*/
public class LocalMemoryTracker implements MemoryAllocationTracker
{
Expand All @@ -31,19 +30,17 @@ public class LocalMemoryTracker implements MemoryAllocationTracker
@Override
public void allocated( long bytes )
{
GlobalMemoryTracker.INSTANCE.allocated( bytes );
this.allocatedBytes += bytes;
}

@Override
public void deallocated( long bytes )
{
GlobalMemoryTracker.INSTANCE.deallocated( bytes );
this.allocatedBytes -= bytes;
}

/**
* @return number of locally used bytes.
* @return number of used bytes.
*/
@Override
public long usedDirectMemory()
Expand Down
Expand Up @@ -48,21 +48,4 @@ void trackMemoryDeallocations()
memoryTracker.deallocated( 40 );
assertEquals( 40, memoryTracker.usedDirectMemory() );
}

@Test
void localMemoryTrackerPropagatesAllocationsToGlobalTracker()
{
GlobalMemoryTracker globalMemoryTracker = GlobalMemoryTracker.INSTANCE;
long initialGlobalUsage = globalMemoryTracker.usedDirectMemory();
LocalMemoryTracker memoryTracker = new LocalMemoryTracker();

memoryTracker.allocated( 100 );
assertEquals( 100, memoryTracker.usedDirectMemory() );
assertEquals( 100, globalMemoryTracker.usedDirectMemory() - initialGlobalUsage );

memoryTracker.deallocated( 50 );
assertEquals( 50, memoryTracker.usedDirectMemory() );
assertEquals( 50, globalMemoryTracker.usedDirectMemory() - initialGlobalUsage );
}

}
Expand Up @@ -385,15 +385,16 @@ public static long allocateMemory( long sizeInBytes )
* The memory is aligned such that it can be used for any data type.
* The memory is uninitialised, so it may contain random garbage, or it may not.
*/
public static long allocateMemory( long sizeInBytes, MemoryAllocationTracker allocationTracker )
public static long allocateMemory( long bytes, MemoryAllocationTracker allocationTracker )
{
final long pointer = unsafe.allocateMemory( sizeInBytes );
final long pointer = unsafe.allocateMemory( bytes );
if ( DIRTY_MEMORY )
{
setMemory( pointer, sizeInBytes, (byte) 0xA5 );
setMemory( pointer, bytes, (byte) 0xA5 );
}
addAllocatedPointer( pointer, sizeInBytes );
allocationTracker.allocated( sizeInBytes );
addAllocatedPointer( pointer, bytes );
allocationTracker.allocated( bytes );
GlobalMemoryTracker.INSTANCE.allocated( bytes );
return pointer;
}

Expand Down Expand Up @@ -432,6 +433,7 @@ public static void free( long pointer, long bytes, MemoryAllocationTracker alloc
checkFree( pointer );
unsafe.freeMemory( pointer );
allocationTracker.deallocated( bytes );
GlobalMemoryTracker.INSTANCE.deallocated( bytes );
}

private static final class FreeTrace extends Throwable implements Comparable<FreeTrace>
Expand Down

0 comments on commit 5799fd6

Please sign in to comment.