Skip to content

Instantly share code, notes, and snippets.

@mdh1418
Created July 9, 2024 20:19
Show Gist options
  • Save mdh1418/3bb38bbb120ab99e6c3f2225aaca041f to your computer and use it in GitHub Desktop.
Save mdh1418/3bb38bbb120ab99e6c3f2225aaca041f to your computer and use it in GitHub Desktop.
// C#
public static int EnumerateGCHeapObjectsInBackgroundThreadWithRuntimeSuspension()
{
_rootObjects.Add(new CustomGCHeapObject());
EnumerateHeapObjectsInBackgroundThread();
GC.Collect();
return 100;
}
// C++ P/Invoke
extern "C" EXPORT void STDMETHODCALLTYPE EnumerateHeapObjectsInBackgroundThread()
{
GCHeapEnumerationProfiler* instance = static_cast<GCHeapEnumerationProfiler*>(GCHeapEnumerationProfiler::Instance);
if (instance == nullptr) {
printf("Error: profiler instance is null.\n");
return;
}
std::thread([instance]()
{
std::this_thread::sleep_for(std::chrono::seconds(1));
instance->ValidateEnumerateGCHeapObjects(0x80131388);
}).detach();
}
// C++ During GC.Collect, sleep long enough for EnumerateGCHeapObjects to begin
HRESULT STDMETHODCALLTYPE GCHeapEnumerationProfiler::GarbageCollectionStarted(int cGenerations, BOOL generationCollected[], COR_PRF_GC_REASON reason)
{
SHUTDOWNGUARD();
printf("GCHeapEnumerationProfiler::GarbageCollectionStarted\nSleeping for 10 seconds\n");
std::this_thread::sleep_for(std::chrono::seconds(10));
return S_OK;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment