Added System.GC.GetTotalAllocatedBytes to the ref assembly (dotnet/corefx#37635)
authorVladimir Sadov <vsadov@microsoft.com>
Thu, 16 May 2019 00:13:00 +0000 (17:13 -0700)
committerGitHub <noreply@github.com>
Thu, 16 May 2019 00:13:00 +0000 (17:13 -0700)
* Update dependencies from https://github.com/dotnet/coreclr build 20190514.72

- Microsoft.NET.Sdk.IL - 3.0.0-preview6-27714-72
- Microsoft.NETCore.ILAsm - 3.0.0-preview6-27714-72
- Microsoft.NETCore.Runtime.CoreCLR - 3.0.0-preview6-27714-72

* Added GetTotalAllocatedBytes to ref

* Added a test for GC.GetTotalAllocatedBytes

* SImplified the test.

Commit migrated from https://github.com/dotnet/corefx/commit/d0d6a734fc877b76f766548d5e79a63aba5160cf

src/libraries/System.Runtime/ref/System.Runtime.cs
src/libraries/System.Runtime/src/ApiCompatBaseline.uapaot.txt
src/libraries/System.Runtime/tests/System/GCTests.netcoreapp.cs

index 6947594..b26e572 100644 (file)
@@ -1273,6 +1273,7 @@ namespace System
         public static GCMemoryInfo GetGCMemoryInfo() { throw null; }
         public static int GetGeneration(object obj) { throw null; }
         public static int GetGeneration(System.WeakReference wo) { throw null; }
+        public static long GetTotalAllocatedBytes(bool precise = false) { throw null; }
         public static long GetTotalMemory(bool forceFullCollection) { throw null; }
         public static void KeepAlive(object obj) { }
         public static void RegisterForFullGCNotification(int maxGenerationThreshold, int largeObjectHeapThreshold) { }
index 5abd07b..4ecf26a 100644 (file)
@@ -18,3 +18,4 @@ CannotRemoveBaseTypeOrInterface : Type 'System.Memory<T>' does not implement int
 CannotRemoveBaseTypeOrInterface : Type 'System.ReadOnlyMemory<T>' does not implement interface 'System.IEquatable<System.ReadOnlyMemory<T>>' in the implementation but it does in the contract.
 MembersMustExist : Member 'System.GC.GetGCMemoryInfo()' does not exist in the implementation but it does exist in the contract.
 TypesMustExist : Type 'System.GCMemoryInfo' does not exist in the implementation but it does exist in the contract.
+MembersMustExist : Member 'System.GC.GetTotalAllocatedBytes(System.Boolean)' does not exist in the implementation but it does exist in the contract.
index 13cafec..e6eea7f 100644 (file)
@@ -64,5 +64,52 @@ namespace System.Tests
                 Assert.InRange(memoryInfo2.FragmentedBytes, memoryInfo1.FragmentedBytes + 1, long.MaxValue);
             }).Dispose();
         }
+
+        [Fact]
+        public static void GetTotalAllocatedBytes()
+        {
+            byte[] stash;
+
+            long CallGetTotalAllocatedBytesAndCheck(long previous, out long differenceBetweenPreciseAndImprecise)
+            {
+                long precise = GC.GetTotalAllocatedBytes(true);
+                long imprecise = GC.GetTotalAllocatedBytes(false);
+
+                if (precise <= 0)
+                {
+                    throw new Exception($"Bytes allocated is not positive, this is unlikely. precise = {precise}");
+                }
+
+                if (imprecise < precise)
+                {
+                    throw new Exception($"Imprecise total bytes allocated less than precise, imprecise is required to be a conservative estimate (that estimates high). imprecise = {imprecise}, precise = {precise}");
+                }
+
+                if (previous > precise)
+                {
+                    throw new Exception($"Expected more memory to be allocated. previous = {previous}, precise = {precise}, difference = {previous - precise}");
+                }
+
+                differenceBetweenPreciseAndImprecise = imprecise - precise;
+                return precise;
+            }
+
+            long CallGetTotalAllocatedBytes(long previous)
+            {
+                long differenceBetweenPreciseAndImprecise;
+                previous = CallGetTotalAllocatedBytesAndCheck(previous, out differenceBetweenPreciseAndImprecise);
+                stash = new byte[differenceBetweenPreciseAndImprecise];
+                previous = CallGetTotalAllocatedBytesAndCheck(previous, out differenceBetweenPreciseAndImprecise);
+                return previous;
+            }
+
+            long previous = 0;
+
+            for (int i = 0; i < 1000; ++i)
+            {
+                stash = new byte[1234];
+                previous = CallGetTotalAllocatedBytes(previous);
+            }
+        }
     }
 }