Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented C# GC get heap size/ get used size #208

Merged
merged 8 commits into from
Jul 26, 2023
11 changes: 6 additions & 5 deletions src/coreclr/vm/mono/mono_coreclr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ struct HostStruct
gboolean (*class_is_valuetype)(MonoClass* klass);
MonoException* (*exception_from_name_msg)(MonoImage* image, const char* name_space, const char* name, const char* msg);
MonoReflectionField* (*field_get_object)(MonoDomain* domain, MonoClass* klass, MonoClassField* field);
gint64 (*gc_get_heap_size)();
gint64 (*gc_get_used_size)();
intptr_t (*gchandle_get_target_v2)(intptr_t handleIn);
uintptr_t (*gchandle_new_v2)(MonoObject* obj, gboolean pinned);
uintptr_t (*gchandle_new_weakref_v2)(MonoObject* obj, gboolean track_resurrection);
Expand Down Expand Up @@ -1292,11 +1294,10 @@ extern "C" EXPORT_API int EXPORT_CC mono_gc_collect_a_little ()
return 0;
}

// Generated by UnityEmbedHost.Generator - Commit these changes
extern "C" EXPORT_API gint64 EXPORT_CC mono_gc_get_heap_size()
{
FCALL_CONTRACT;
// NOT CORRECT
return GCHeapUtilities::GetGCHeap()->GetTotalBytesInUse();
return g_HostStruct->gc_get_heap_size();
}

extern "C" EXPORT_API gint64 EXPORT_CC mono_gc_get_max_time_slice_ns ()
Expand All @@ -1305,10 +1306,10 @@ extern "C" EXPORT_API gint64 EXPORT_CC mono_gc_get_max_time_slice_ns ()
return 0;
}

// Generated by UnityEmbedHost.Generator - Commit these changes
extern "C" EXPORT_API gint64 EXPORT_CC mono_gc_get_used_size()
{
FCALL_CONTRACT;
return GCHeapUtilities::GetGCHeap()->GetTotalBytesInUse();
return g_HostStruct->gc_get_used_size();
}

extern "C" EXPORT_API gboolean EXPORT_CC mono_gc_is_incremental ()
Expand Down
24 changes: 24 additions & 0 deletions unity/UnityEmbedHost.Tests/BaseEmbeddingApiTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -783,6 +783,30 @@ public void UnityMethodIsGenericReturnsProperValue(Type objType, string methodNa
Assert.That(isGeneric, Is.EqualTo(expectedResult));
}

[Test]
public void GcGetHeapSizeReturnsProperValue()
{
GC.Collect();
long heapSize = ClrHost.gc_get_heap_size();
Assert.NotZero(heapSize);
int[] data = new int[1024 * 1024 * 100];
GC.Collect();
long heapSizeAfterBigAlloc = ClrHost.gc_get_heap_size();
Assert.Greater(heapSizeAfterBigAlloc, heapSize);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Try putting GC.KeepAlive(data); at the end of the method.

}

[Test]
public void GcGetUsedSizeReturnsProperValue()
{
GC.Collect();
long usedSize = ClrHost.gc_get_used_size();
Assert.NotZero(usedSize);
int[] data = new int[1024 * 1024 * 100];
GC.Collect();
long usedSizeAfterBigAlloc = ClrHost.gc_get_used_size();
Assert.Greater(usedSizeAfterBigAlloc, usedSize);
}

static List<object?> FlattenedArray(Array arr)
{
var result = new List<object?>();
Expand Down
13 changes: 13 additions & 0 deletions unity/unity-embed-host/CoreCLRHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -671,6 +671,19 @@ public static bool unity_mono_method_is_generic(
return metBase.IsGenericMethodDefinition;
}

[return: NativeCallbackType("gint64")]
public static long gc_get_heap_size()
{
var info = GC.GetGCMemoryInfo();
return info.HeapSizeBytes;
}

[return: NativeCallbackType("gint64")]
public static long gc_get_used_size()
{
return GC.GetTotalMemory(false);
}

static void Log(string message)
{
var bytes = System.Text.Encoding.UTF8.GetBytes(message);
Expand Down