diff --git a/src/RabbitMQ.Next/Buffers/PooledMemoryAccessor.cs b/src/RabbitMQ.Next/Buffers/PooledMemoryAccessor.cs index dfc5394..3af842c 100644 --- a/src/RabbitMQ.Next/Buffers/PooledMemoryAccessor.cs +++ b/src/RabbitMQ.Next/Buffers/PooledMemoryAccessor.cs @@ -37,7 +37,15 @@ public PooledMemoryAccessor(ObjectPool memoryPool, byte[] memory, int of this.Size = size; } + ~PooledMemoryAccessor() => this.ReleaseMemory(); + public void Dispose() + { + GC.SuppressFinalize(this); + this.ReleaseMemory(); + } + + private void ReleaseMemory() { if (this.memory == null) { diff --git a/src/RabbitMQ.Next/Buffers/SharedMemory.cs b/src/RabbitMQ.Next/Buffers/SharedMemory.cs index e300cf1..32ce93c 100644 --- a/src/RabbitMQ.Next/Buffers/SharedMemory.cs +++ b/src/RabbitMQ.Next/Buffers/SharedMemory.cs @@ -22,7 +22,15 @@ public SharedMemory(ObjectPool memoryPool, byte[] memory, int size) this.Size = size; } + ~SharedMemory() => this.ReleaseMemory(); + public void Dispose() + { + GC.SuppressFinalize(this); + this.ReleaseMemory(); + } + + private void ReleaseMemory() { var refsCount = Interlocked.Decrement(ref this.referencesCount); if (refsCount != 0) @@ -114,8 +122,15 @@ public SharedMemoryAccessor(SharedMemory owner, int offset, int size) Interlocked.Increment(ref this.owner.referencesCount); } + ~SharedMemoryAccessor() => this.ReleaseMemory(); public void Dispose() + { + GC.SuppressFinalize(this); + this.ReleaseMemory(); + } + + private void ReleaseMemory() { if (this.owner == null) { diff --git a/tests/RabbitMQ.Next.Benchmarks/Publisher/PublisherBenchmarks.cs b/tests/RabbitMQ.Next.Benchmarks/Publisher/PublisherBenchmarks.cs index c95882c..bc46873 100644 --- a/tests/RabbitMQ.Next.Benchmarks/Publisher/PublisherBenchmarks.cs +++ b/tests/RabbitMQ.Next.Benchmarks/Publisher/PublisherBenchmarks.cs @@ -111,10 +111,10 @@ TestCaseParameters GenerateTestCase(int payloadLen, int count, string name) return new TestCaseParameters(name, messages); } - yield return GenerateTestCase(1024, 10_000, "1024 (1 kB)"); - yield return GenerateTestCase(10240, 10_000, "10240 (10 kB)"); - yield return GenerateTestCase(102400, 10_000, "102400 (100 kB)"); - //yield return GenerateTestCase(204800, 10_000, "204800 (200 kB)"); + yield return GenerateTestCase(1024, 1_000, "1024 (1 kB)"); + yield return GenerateTestCase(10240, 1_000, "10240 (10 kB)"); + yield return GenerateTestCase(102400, 1_000, "102400 (100 kB)"); + yield return GenerateTestCase(204800, 1_000, "204800 (200 kB)"); } public class TestCaseParameters