diff --git a/src/Sentry/Internal/ThreadsafeCounterDictionary.cs b/src/Sentry/Internal/ThreadsafeCounterDictionary.cs
index 0265fba7b6..0f10e6a244 100644
--- a/src/Sentry/Internal/ThreadsafeCounterDictionary.cs
+++ b/src/Sentry/Internal/ThreadsafeCounterDictionary.cs
@@ -24,13 +24,13 @@ private class CounterItem
///
/// The key of the counter to increment.
/// The amount to add to the counter.
- public void Add(TKey key, int quantity) => _items.GetOrAdd(key, new CounterItem()).Add(quantity);
+ public void Add(TKey key, int quantity) => _items.GetOrAdd(key, static _ => new CounterItem()).Add(quantity);
///
/// Atomically increments a counter based on the key provided, creating the counter if necessary.
///
/// The key of the counter to increment.
- public void Increment(TKey key) => _items.GetOrAdd(key, new CounterItem()).Increment();
+ public void Increment(TKey key) => _items.GetOrAdd(key, static _ => new CounterItem()).Increment();
///
/// Gets a single counter's value while atomically resetting it to zero.
diff --git a/test/Sentry.Tests/Internals/ThreadsafeCounterDictionaryTests.cs b/test/Sentry.Tests/Internals/ThreadsafeCounterDictionaryTests.cs
index fec7004088..09735f9940 100644
--- a/test/Sentry.Tests/Internals/ThreadsafeCounterDictionaryTests.cs
+++ b/test/Sentry.Tests/Internals/ThreadsafeCounterDictionaryTests.cs
@@ -275,4 +275,23 @@ void ReadAndAddToGrandTotal()
const int expectedGrandTotal = numCounters * numThreadsPerCounter * numIterationsPerThread;
Assert.Equal(expectedGrandTotal, grandTotal);
}
+
+#if NET5_0_OR_GREATER && !__MOBILE__
+ [Fact]
+ public void AddAndIncrement_ExistingCounter_DoNotAllocate()
+ {
+ var key = DiscardReason.CallbackError.WithCategory(DataCategory.LogItem);
+ var counters = new ThreadsafeCounterDictionary();
+ counters.Add(key, 1);
+ counters.Increment(key);
+
+ var before = GC.GetAllocatedBytesForCurrentThread();
+ counters.Add(key, 1);
+ counters.Increment(key);
+ var allocated = GC.GetAllocatedBytesForCurrentThread() - before;
+
+ Assert.Equal(0, allocated);
+ Assert.Equal(4, counters[key]);
+ }
+#endif
}