Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions src/Apache.Arrow/Memory/ExportedAllocationOwner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ internal sealed class ExportedAllocationOwner : IDisposable
private readonly List<IntPtr> _pointers = new List<IntPtr>();
private readonly List<MemoryHandle> _handles = new List<MemoryHandle>();
private readonly List<SharedMemoryHandle> _sharedHandles = new List<SharedMemoryHandle>();
private long _allocationSize;
private long _referenceCount;
private bool _disposed;

Expand All @@ -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;
}

Expand Down Expand Up @@ -103,7 +100,6 @@ public void Dispose()
_sharedHandles[i] = default;
}

GC.RemoveMemoryPressure(_allocationSize);
GC.SuppressFinalize(this);
_disposed = true;
}
Expand Down
5 changes: 0 additions & 5 deletions src/Apache.Arrow/Memory/ImportedAllocationOwner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ public IMemoryOwner<byte> AddMemory(IntPtr ptr, int offset, int length)
if (length > 0)
{
Interlocked.Add(ref _managedMemory, length);
GC.AddMemoryPressure(length);
}
return memory;
}
Expand All @@ -55,10 +54,6 @@ public void Release()
{
if (Interlocked.Decrement(ref _referenceCount) == 0)
{
if (_managedMemory > 0)
{
GC.RemoveMemoryPressure(_managedMemory);
}
FinalRelease();
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/Apache.Arrow/Memory/MemoryPressureAllocationTracker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ namespace Apache.Arrow.Memory
/// <summary>
/// Allows control over the way native allocations interact with the GC.
/// </summary>
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)
{
Expand Down
5 changes: 1 addition & 4 deletions src/Apache.Arrow/Memory/NativeMemoryAllocator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ protected override IMemoryOwner<byte> 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?

Expand All @@ -42,8 +42,6 @@ protected override IMemoryOwner<byte> AllocateInternal(int length, out int bytes

bytesAllocated = (length + Alignment);

GC.AddMemoryPressure(bytesAllocated);

// Ensure all allocated memory is zeroed.
manager.Memory.Span.Fill(0);

Expand All @@ -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
}
}
}
Expand Down
5 changes: 2 additions & 3 deletions src/Apache.Arrow/Memory/NoOpAllocationTracker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@
namespace Apache.Arrow.Memory
{
/// <summary>
/// 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.
/// </summary>
public struct NoOpAllocationTracker : INativeAllocationTracker
public readonly struct NoOpAllocationTracker : INativeAllocationTracker
{
public void Track(int count, long bytes) { }
}
Expand Down
3 changes: 3 additions & 0 deletions test/Apache.Arrow.Tests/NativeBufferTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<byte, MemoryPressureAllocationTracker>(1024);
#pragma warning restore CS0618 // Type or member is obsolete
buf.Span[0] = 0xFF;
buf.Dispose();
}
Expand Down