From 67bc5d41903646edf657eeab5d5c014825797f56 Mon Sep 17 00:00:00 2001 From: Jan Kotas Date: Sat, 8 Oct 2016 21:47:46 -0700 Subject: [PATCH] Expose GC.GetAllocatedBytesForCurrentThread in netcoreapp1.1 --- src/System.Runtime/ref/System.Runtime.cs | 3 +++ .../tests/System.Runtime.Tests.csproj | 1 + src/System.Runtime/tests/System/GCTests.cs | 2 +- .../tests/System/GCTests.netcoreapp1.1.cs | 27 +++++++++++++++++++ 4 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 src/System.Runtime/tests/System/GCTests.netcoreapp1.1.cs diff --git a/src/System.Runtime/ref/System.Runtime.cs b/src/System.Runtime/ref/System.Runtime.cs index 37583cb3ca0c..c0f6003eff9f 100644 --- a/src/System.Runtime/ref/System.Runtime.cs +++ b/src/System.Runtime/ref/System.Runtime.cs @@ -1152,6 +1152,9 @@ public static void RemoveMemoryPressure(long bytesAllocated) { } public static void ReRegisterForFinalize(object obj) { } public static void SuppressFinalize(object obj) { } public static void WaitForPendingFinalizers() { } +#if netcoreapp11 + public static long GetAllocatedBytesForCurrentThread() { return default(long); } +#endif } public enum GCCollectionMode { diff --git a/src/System.Runtime/tests/System.Runtime.Tests.csproj b/src/System.Runtime/tests/System.Runtime.Tests.csproj index 1cddfd015e23..e5d64cd8f6f7 100644 --- a/src/System.Runtime/tests/System.Runtime.Tests.csproj +++ b/src/System.Runtime/tests/System.Runtime.Tests.csproj @@ -103,6 +103,7 @@ + diff --git a/src/System.Runtime/tests/System/GCTests.cs b/src/System.Runtime/tests/System/GCTests.cs index 800ae6e3c4d7..545c631f803f 100644 --- a/src/System.Runtime/tests/System/GCTests.cs +++ b/src/System.Runtime/tests/System/GCTests.cs @@ -8,7 +8,7 @@ namespace System.Tests { - public static class GCTests + public static partial class GCTests { private static bool s_is32Bits = IntPtr.Size == 4; // Skip IntPtr tests on 32-bit platforms diff --git a/src/System.Runtime/tests/System/GCTests.netcoreapp1.1.cs b/src/System.Runtime/tests/System/GCTests.netcoreapp1.1.cs new file mode 100644 index 000000000000..67035d202213 --- /dev/null +++ b/src/System.Runtime/tests/System/GCTests.netcoreapp1.1.cs @@ -0,0 +1,27 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using Xunit; + +namespace System.Tests +{ + public static partial class GCTests + { + [Theory] + [InlineData(1000)] + [InlineData(100000)] + public static void GetAllocatedBytesForCurrentThread(int size) + { + long start = GC.GetAllocatedBytesForCurrentThread(); + + GC.KeepAlive(new String('a', size)); + + long end = GC.GetAllocatedBytesForCurrentThread(); + + Assert.True((end - start) > size, $"Allocated too little: start: {start} end: {end} size: {size}"); + Assert.True((end - start) < 5 * size, $"Allocated too much: start: {start} end: {end} size: {size}"); + } + } +}