Skip to content

Commit

Permalink
[NativeAOT] collect garbage when memory is low (#103857)
Browse files Browse the repository at this point in the history
  • Loading branch information
AustinWise authored Jul 2, 2024
1 parent 7294674 commit c07a81f
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public static void ProcessFinalizers()
{
// RhpWaitForFinalizerRequest() returned false and indicated that memory is low. We help
// out by initiating a garbage collection and then go back to waiting for another request.
InternalCalls.RhCollect(-1, InternalGCCollectionMode.Blocking);
InternalCalls.RhCollect(0, InternalGCCollectionMode.Blocking, lowMemoryP: true);
}
}
}
Expand Down
12 changes: 5 additions & 7 deletions src/coreclr/nativeaot/Runtime/FinalizerHelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ GPTR_DECL(Thread, g_pFinalizerThread);
CLREventStatic g_FinalizerEvent;
CLREventStatic g_FinalizerDoneEvent;

static HANDLE g_lowMemoryNotification = NULL;

EXTERN_C void QCALLTYPE ProcessFinalizers();

// Unmanaged front-end to the finalizer thread. We require this because at the point the GC creates the
Expand Down Expand Up @@ -76,6 +78,7 @@ bool RhInitializeFinalization()
return false;
if (!g_FinalizerDoneEvent.CreateManualEventNoThrow(false))
return false;
g_lowMemoryNotification = PalCreateLowMemoryResourceNotification();

// Create the finalizer thread itself.
if (!PalStartFinalizerThread(FinalizerStart, (void*)g_FinalizerEvent.GetOSEvent()))
Expand Down Expand Up @@ -132,17 +135,12 @@ EXTERN_C UInt32_BOOL QCALLTYPE RhpWaitForFinalizerRequest()
// two second timeout expires.
do
{
HANDLE lowMemEvent = NULL;
#if 0 // TODO: hook up low memory notification
lowMemEvent = pHeap->GetLowMemoryNotificationEvent();
HANDLE lowMemEvent = g_lowMemoryNotification;
HANDLE rgWaitHandles[] = { g_FinalizerEvent.GetOSEvent(), lowMemEvent };
uint32_t cWaitHandles = (fLastEventWasLowMemory || (lowMemEvent == NULL)) ? 1 : 2;
uint32_t uTimeout = fLastEventWasLowMemory ? 2000 : INFINITE;

uint32_t uResult = PalWaitForMultipleObjectsEx(cWaitHandles, rgWaitHandles, FALSE, uTimeout, FALSE);
#else
uint32_t uResult = PalWaitForSingleObjectEx(g_FinalizerEvent.GetOSEvent(), INFINITE, FALSE);
#endif
uint32_t uResult = PalCompatibleWaitAny(/*alertable=*/ FALSE, uTimeout, cWaitHandles, rgWaitHandles, /*allowReentrantWait=*/ FALSE);

switch (uResult)
{
Expand Down
2 changes: 2 additions & 0 deletions src/coreclr/nativeaot/Runtime/PalRedhawk.h
Original file line number Diff line number Diff line change
Expand Up @@ -706,6 +706,8 @@ REDHAWK_PALIMPORT UInt32_BOOL REDHAWK_PALAPI PalMarkThunksAsValidCallTargets(

REDHAWK_PALIMPORT uint32_t REDHAWK_PALAPI PalCompatibleWaitAny(UInt32_BOOL alertable, uint32_t timeout, uint32_t count, HANDLE* pHandles, UInt32_BOOL allowReentrantWait);

REDHAWK_PALIMPORT HANDLE PalCreateLowMemoryResourceNotification();

REDHAWK_PALIMPORT void REDHAWK_PALAPI PalAttachThread(void* thread);
REDHAWK_PALIMPORT bool REDHAWK_PALAPI PalDetachThread(void* thread);

Expand Down
5 changes: 5 additions & 0 deletions src/coreclr/nativeaot/Runtime/unix/PalRedhawkUnix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1126,6 +1126,11 @@ REDHAWK_PALEXPORT uint32_t REDHAWK_PALAPI PalCompatibleWaitAny(UInt32_BOOL alert
return WaitForSingleObjectEx(pHandles[0], timeout, alertable);
}

REDHAWK_PALEXPORT HANDLE PalCreateLowMemoryResourceNotification()
{
return NULL;
}

#if !__has_builtin(_mm_pause)
extern "C" void _mm_pause()
// Defined for implementing PalYieldProcessor in PalRedhawk.h
Expand Down
5 changes: 5 additions & 0 deletions src/coreclr/nativeaot/Runtime/windows/PalRedhawkMinWin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,11 @@ REDHAWK_PALEXPORT uint32_t REDHAWK_PALAPI PalCompatibleWaitAny(UInt32_BOOL alert
}
}

REDHAWK_PALEXPORT HANDLE PalCreateLowMemoryResourceNotification()
{
return CreateMemoryResourceNotification(LowMemoryResourceNotification);
}

REDHAWK_PALEXPORT void REDHAWK_PALAPI PalSleep(uint32_t milliseconds)
{
return Sleep(milliseconds);
Expand Down

0 comments on commit c07a81f

Please sign in to comment.