diff --git a/src/Apache.Arrow/Memory/ExportedAllocationOwner.cs b/src/Apache.Arrow/Memory/ExportedAllocationOwner.cs index 272841e6..54d92cf9 100644 --- a/src/Apache.Arrow/Memory/ExportedAllocationOwner.cs +++ b/src/Apache.Arrow/Memory/ExportedAllocationOwner.cs @@ -26,7 +26,6 @@ internal sealed class ExportedAllocationOwner : IDisposable private readonly List _pointers = new List(); private readonly List _handles = new List(); private readonly List _sharedHandles = new List(); - private long _allocationSize; private long _referenceCount; private bool _disposed; @@ -37,14 +36,12 @@ internal sealed class ExportedAllocationOwner : IDisposable public IntPtr Allocate(int size) { - GC.AddMemoryPressure(size); return Acquire(Marshal.AllocHGlobal(size), 0, size); } public IntPtr Acquire(IntPtr ptr, int offset, int length) { _pointers.Add(ptr); - _allocationSize += length; return ptr; } @@ -103,7 +100,6 @@ public void Dispose() _sharedHandles[i] = default; } - GC.RemoveMemoryPressure(_allocationSize); GC.SuppressFinalize(this); _disposed = true; } diff --git a/src/Apache.Arrow/Memory/ImportedAllocationOwner.cs b/src/Apache.Arrow/Memory/ImportedAllocationOwner.cs index b29135d5..9bccfa05 100644 --- a/src/Apache.Arrow/Memory/ImportedAllocationOwner.cs +++ b/src/Apache.Arrow/Memory/ImportedAllocationOwner.cs @@ -41,7 +41,6 @@ public IMemoryOwner AddMemory(IntPtr ptr, int offset, int length) if (length > 0) { Interlocked.Add(ref _managedMemory, length); - GC.AddMemoryPressure(length); } return memory; } @@ -55,10 +54,6 @@ public void Release() { if (Interlocked.Decrement(ref _referenceCount) == 0) { - if (_managedMemory > 0) - { - GC.RemoveMemoryPressure(_managedMemory); - } FinalRelease(); } } diff --git a/src/Apache.Arrow/Memory/MemoryPressureAllocationTracker.cs b/src/Apache.Arrow/Memory/MemoryPressureAllocationTracker.cs index 4a556bd5..161d809c 100644 --- a/src/Apache.Arrow/Memory/MemoryPressureAllocationTracker.cs +++ b/src/Apache.Arrow/Memory/MemoryPressureAllocationTracker.cs @@ -20,7 +20,8 @@ namespace Apache.Arrow.Memory /// /// Allows control over the way native allocations interact with the GC. /// - public struct MemoryPressureAllocationTracker : INativeAllocationTracker + [Obsolete("Use NoOpAllocationTracker instead. See https://learn.microsoft.com/en-us/dotnet/api/system.gc.addmemorypressure?view=net-10.0#remarks for more information on why the GC memory pressure APIs are not always necessary.")] + public readonly struct MemoryPressureAllocationTracker : INativeAllocationTracker { public void Track(int count, long bytes) { diff --git a/src/Apache.Arrow/Memory/NativeMemoryAllocator.cs b/src/Apache.Arrow/Memory/NativeMemoryAllocator.cs index d1cbe526..888e12f2 100644 --- a/src/Apache.Arrow/Memory/NativeMemoryAllocator.cs +++ b/src/Apache.Arrow/Memory/NativeMemoryAllocator.cs @@ -31,7 +31,7 @@ protected override IMemoryOwner AllocateInternal(int length, out int bytes // TODO: Ensure memory is released if exception occurs. // TODO: Optimize storage overhead; native memory manager stores a pointer - // to allocated memory, offset, and the allocation size. + // to allocated memory, offset, and the allocation size. // TODO: Should the allocation be moved to NativeMemory? @@ -42,8 +42,6 @@ protected override IMemoryOwner AllocateInternal(int length, out int bytes bytesAllocated = (length + Alignment); - GC.AddMemoryPressure(bytesAllocated); - // Ensure all allocated memory is zeroed. manager.Memory.Span.Fill(0); @@ -55,7 +53,6 @@ private sealed class NativeAllocationOwner : INativeAllocationOwner public void Release(IntPtr ptr, int offset, int length) { Marshal.FreeHGlobal(ptr); - GC.RemoveMemoryPressure(length + DefaultAlignment); // See https://github.com/apache/arrow-dotnet/issues/50 } } } diff --git a/src/Apache.Arrow/Memory/NoOpAllocationTracker.cs b/src/Apache.Arrow/Memory/NoOpAllocationTracker.cs index 905846d7..bc55d26d 100644 --- a/src/Apache.Arrow/Memory/NoOpAllocationTracker.cs +++ b/src/Apache.Arrow/Memory/NoOpAllocationTracker.cs @@ -16,10 +16,9 @@ namespace Apache.Arrow.Memory { /// - /// Avoid informing the GC about native allocations. Consider using for micro-benchmarks where - /// the impact of GC.AddMemoryPressure and GC.RemoveMemoryPressure can skew timing results. + /// Avoid informing the GC about native allocations. /// - public struct NoOpAllocationTracker : INativeAllocationTracker + public readonly struct NoOpAllocationTracker : INativeAllocationTracker { public void Track(int count, long bytes) { } } diff --git a/test/Apache.Arrow.Tests/NativeBufferTests.cs b/test/Apache.Arrow.Tests/NativeBufferTests.cs index 84d050cf..734a1290 100644 --- a/test/Apache.Arrow.Tests/NativeBufferTests.cs +++ b/test/Apache.Arrow.Tests/NativeBufferTests.cs @@ -158,7 +158,10 @@ public void MemoryPressureTrackerNotifiesGC() // Smoke test: just verify it doesn't throw. // We can't directly observe GC memory pressure, but we verify the // alloc/dealloc cycle completes without error. + // Test-only use of obsolete API. +#pragma warning disable CS0618 // Type or member is obsolete var buf = new NativeBuffer(1024); +#pragma warning restore CS0618 // Type or member is obsolete buf.Span[0] = 0xFF; buf.Dispose(); }