Created
July 9, 2024 20:19
-
-
Save mdh1418/3bb38bbb120ab99e6c3f2225aaca041f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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