UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
GarbageCollection.cpp File Reference
#include "UObject/GarbageCollection.h"
#include "Algo/Find.h"
#include "Async/UniqueLock.h"
#include "AutoRTFM.h"
#include "Containers/ConsumeAllMpmcQueue.h"
#include "Containers/Deque.h"
#include "Containers/StaticBitArray.h"
#include "HAL/MallocLeakDetection.h"
#include "HAL/ThreadSafeBool.h"
#include "Misc/TimeGuard.h"
#include "HAL/IConsoleManager.h"
#include "Misc/App.h"
#include "Misc/CoreDelegates.h"
#include "Tasks/Task.h"
#include "UObject/ScriptInterface.h"
#include "UObject/UObjectAllocator.h"
#include "UObject/UObjectBase.h"
#include "UObject/Object.h"
#include "UObject/ObjectHandle.h"
#include "UObject/ObjectPtr.h"
#include "UObject/Class.h"
#include "UObject/UObjectIterator.h"
#include "UObject/UnrealType.h"
#include "UObject/LinkerLoad.h"
#include "UObject/GCObject.h"
#include "UObject/GCScopeLock.h"
#include "HAL/ExceptionHandling.h"
#include "UObject/UObjectClusters.h"
#include "HAL/LowLevelMemTracker.h"
#include "UObject/GarbageCollectionVerification.h"
#include "UObject/Package.h"
#include "Async/ParallelFor.h"
#include "ProfilingDebugging/CsvProfiler.h"
#include "HAL/Runnable.h"
#include "HAL/RunnableThread.h"
#include "HAL/ThreadHeartBeat.h"
#include "UObject/FieldPathProperty.h"
#include "UObject/GarbageCollectionHistory.h"
#include "UObject/GarbageCollectionInternalFlags.h"
#include "UObject/GarbageCollectionTesting.h"
#include "UObject/InstanceDataObjectUtils.h"
#include "UObject/PropertyBagRepository.h"
#include "UObject/PropertyOptional.h"
#include "UObject/ExpandingChunkedList.h"
#include "UObject/ReachabilityAnalysisState.h"
#include "UObject/ReachabilityAnalysis.h"
#include "UObject/RemoteObjectPrivate.h"
#include <atomic>
#include "UObject/FastReferenceCollector.h"

Classes

struct  FGCTimingInfo
 
class  FGCHashTableScopeLock
 
struct  UE::GC::FDetailedClassStats
 
class  FObjectPurge
 
class  UE::GC::FContextPool
 
class  UE::GC::FContextPoolScope
 
struct  UE::GC::TAddAdapter< Options >
 
struct  UE::GC::FMutableReference
 
struct  UE::GC::FImmutableReference
 
struct  UE::GC::FReferenceArray
 
struct  UE::GC::FResolvedMutableReference
 
struct  UE::GC::FReferenceMetadata
 
class  UE::GC::FPageAllocator
 
class  UE::GC::FBatcherBase
 
struct  UE::GC::FBatcherBase::TBatchQueue< EntryType, Capacity, PrefetchCapacity >
 
struct  UE::GC::FBatcherBase::FValidatedBitmask
 
class  UE::GC::TReferenceBatcher< UnvalidatedReferenceType, ValidatedReferenceType, ProcessorType >
 
struct  UE::GC::FStructArrayBlock
 
class  UE::GC::FStructBlockifier
 
struct  UE::GC::FSparseStructArray
 
struct  UE::GC::FStructArrayWithoutStride
 
class  UE::GC::FStructBatcher
 
class  UE::GC::FWorkerIndexAllocator
 
class  UE::GC::FBoundedWorkstealingQueue
 
class  UE::GC::FWorkstealingQueue
 
struct  UE::GC::FWorkstealingManager
 
struct  UE::GC::FAROBlock
 
class  UE::GC::FAROQueue
 
struct  UE::GC::FAROQueue::FFew
 
struct  UE::GC::FAROQueue::FMany
 
class  UE::GC::FAROQueueStore
 
class  UE::GC::FSlowAROManager
 
class  UE::GC::TSingleton< T >
 
class  UE::GC::TReachabilityProcessor< InOptions >
 
struct  UE::GC::TBatchDispatcher< ProcessorType >
 
class  UE::GC::TReachabilityCollectorBase< Options >
 
class  UE::GC::TReachabilityCollector< Options >
 
class  UE::GC::TDebugReachabilityProcessor< InOptions >
 
class  UE::GC::TDebugReachabilityCollector< Options >
 
class  UE::GC::FRealtimeGC
 
struct  UE::GC::FRealtimeGC::FMarkClustersArrays
 
class  UE::GC::FWeakReferenceEliminator
 
class  UE::GC::FWorkCoordinator
 UE_BUILD_SHIPPING. More...
 

Namespaces

namespace  UE
 
namespace  UE::GC
 
namespace  UE::GC::Private
 
namespace  UE::GC::Test
 

Macros

#define PERF_DETAILED_PER_CLASS_GC_STATS   (LOOKING_FOR_PERF_ISSUES || 0)
 
#define UE_LOG_FGCScopeGuard_LockAsync_Time   0
 

Typedefs

using UE::GC::Private::FGatherUnreachableObjectsState = TThreadedGather< TArray< UE::GC::FUnreachableObject > >
 
using UE::GC::FStridedReferenceArray = Private::FStridedReferenceArray
 
using UE::GC::FStridedReferenceView = Private::FStridedReferenceView
 

Enumerations

enum class  UE::GC::EKillable { UE::GC::No , UE::GC::Yes }
 

Functions

 DEFINE_LOG_CATEGORY (LogGarbage)
 
 CSV_DEFINE_CATEGORY_MODULE (COREUOBJECT_API, GC, true)
 
 TSAN_ATOMIC (bool) GIsGarbageCollecting
 
void NotifyUnreachableObjects (const TArrayView< FUObjectItem * > &UnreachableObjects)
 
bool IsGarbageCollectionLocked ()
 
void UE::GC::MarkAsReachable (const UObject *Obj)
 
EGCOptions UE::GC::GetReferenceCollectorOptions (bool bPerformFullPurge)
 
EGatherOptions UE::GC::GetObjectGatherOptions ()
 
void UE::GC::Private::SetSkipDestroy (bool bSkip)
 
void UE::GC::Private::ProcessDirtyRootNoLock (int32 Index)
 
void UE::GC::Private::ProcessDirtyRootsNoLock ()
 
void UE::GC::Private::MarkRootAsDirty (int32 Index)
 
COREUOBJECT_API bool UE::GC::Test::IsReachable (const UObject *Object)
 
COREUOBJECT_API bool UE::GC::Test::IsMaybeUnreachable (const UObject *Object)
 
COREUOBJECT_API void UE::GC::Test::SetMaybeUnreachable (UObject *Object)
 
void OnDisregardForGCSetDisabled (int32 NumObjects)
 
int32 UE::GC::GetNumSlowAROs ()
 
FORCEINLINE UObjectUE::GC::GetObject (FImmutableReference In)
 
FORCEINLINE UObjectUE::GC::GetObject (FMutableReference In)
 
FORCEINLINE void UE::GC::PrefetchObjectPointer (FImmutableReference)
 
FORCEINLINE void UE::GC::PrefetchObjectPointer (FMutableReference In)
 
FORCEINLINE FImmutableReference UE::GC::ToImmutableReference (FMutableReference In)
 
FORCEINLINE FImmutableReference UE::GC::ToResolvedReference (FImmutableReference In)
 
FORCEINLINE FResolvedMutableReference UE::GC::ToResolvedReference (FMutableReference In)
 
template<class RefType >
RefType UE::GC::ToReference (FMutableReference In)
 
template<>
FORCEINLINE FImmutableReference UE::GC::ToReference (FMutableReference In)
 
template<>
FORCEINLINE FMutableReference UE::GC::ToReference (FMutableReference In)
 
template<class RefType >
RefType UE::GC::MakeReference (UObject *&Object)
 
template<>
FORCEINLINE FImmutableReference UE::GC::MakeReference (UObject *&Object)
 
template<>
FORCEINLINE FMutableReference UE::GC::MakeReference (UObject *&Object)
 
FORCEINLINE_DEBUGGABLE void UE::GC::CheckValid (FSchemaView Schema)
 
void UE::GC::PadObjectArray (TArray< UObject * > &Objects)
 
FORCEINLINE_DEBUGGABLE void UE::GC::PadBlock (FWorkBlock &Block)
 
void UE::GC::RegisterSlowImplementation (ObjectAROFn ARO, EAROFlags Flags)
 
int32 UE::GC::FindSlowImplementation (ObjectAROFn ARO)
 
FORCEINLINE_DEBUGGABLE void UE::GC::KillReference (UObject *&Object)
 
template<EGCOptions Options>
constexpr FORCEINLINE EKillable UE::GC::MayKill (EOrigin Origin, bool bAllowKill)
 
void ShutdownGarbageCollection ()
 
void AcquireGCLock ()
 
void ReleaseGCLock ()
 
void IncrementalPurgeGarbage (bool bUseTimeLimit, double TimeLimit)
 
bool IsIncrementalPurgePending ()
 
bool IsGarbageCollectingAndLockingUObjectHashTables ()
 
void DissolveUnreachableClusters (UE::GC::EGatherOptions Options)
 
void GatherUnreachableObjects (bool bForceSingleThreaded)
 
template<bool bPerformFullPurge>
FORCENOINLINE void UE::GC::CollectGarbageImpl (EObjectFlags KeepFlags)
 
FORCEINLINE void UE::GC::CollectGarbageInternal (EObjectFlags KeepFlags, bool bPerformFullPurge)
 
template<bool bPerformFullPurge>
void UE::GC::PreCollectGarbageImpl (EObjectFlags KeepFlags)
 
template<bool bPerformFullPurge>
void UE::GC::CollectGarbageImpl (EObjectFlags KeepFlags)
 
template<bool bPerformFullPurge>
void UE::GC::PostCollectGarbageImpl (EObjectFlags KeepFlags)
 
void SetIncrementalReachabilityAnalysisEnabled (bool bEnabled)
 
bool GetIncrementalReachabilityAnalysisEnabled ()
 
void SetReachabilityAnalysisTimeLimit (float TimeLimitSeconds)
 
float GetReachabilityAnalysisTimeLimit ()
 
bool IsIncrementalReachabilityAnalysisPending ()
 
void PerformIncrementalReachabilityAnalysis (double TimeLimit)
 
void FinalizeIncrementalReachabilityAnalysis ()
 
UE::GC::Private::FStats UE::GC::Private::GetGarbageCollectionStats ()
 
double GetLastGCTime ()
 
double GetLastGCDuration ()
 
bool IsIncrementalUnhashPending ()
 
bool UnhashUnreachableObjects (bool bUseTimeLimit, double TimeLimit)
 
void CollectGarbage (EObjectFlags KeepFlags, bool bPerformFullPurge)
 
bool TryCollectGarbage (EObjectFlags KeepFlags, bool bPerformFullPurge)
 
void PurgeAllUObjectsOnExit ()
 
void UE::GC::SuspendWork (FWorkerContext &Context)
 
ELoot UE::GC::StealWork (FWorkerContext &Context, FReferenceCollector &Collector, FWorkBlock *&OutBlock, EGCOptions Options)
 
TArrayView< FWorkerContext * > UE::GC::InitializeAsyncProcessingContexts (FWorkerContext &InContext)
 
void UE::GC::ReleaseAsyncProcessingContexts (FWorkerContext &InContext, TArrayView< FWorkerContext * > Contexts)
 
void UE::GC::ProcessAsync (void(*ProcessSync)(void *, FWorkerContext &), void *Processor, FWorkerContext &InContext)
 
int32 GetNumCollectReferenceWorkers ()
 

Variables

COREUOBJECT_API bool GShouldVerifyGCAssumptionsOnFullPurge
 
COREUOBJECT_API float GVerifyGCAssumptionsChance
 
FThreadSafeCounter GObjectCountDuringLastMarkPhase
 
std::atomic< boolGIsGarbageCollectingAndLockingUObjectHashTables = false
 
std::atomic< boolGObjIncrementalPurgeIsInProgress = false
 
std::atomic< boolGObjUnhashUnreachableIsInProgress = false
 
double GCStartTime = 0.
 
FGCCSyncObjectGGCSingleton
 
int32 GGarbageReferenceTrackingEnabled = 0
 
int32 GDelayReachabilityIterations = 0
 
EInternalObjectFlags UE::GC::GReachableObjectFlag = EInternalObjectFlags::ReachabilityFlag0
 
EInternalObjectFlags UE::GC::GUnreachableObjectFlag = EInternalObjectFlags::Unreachable
 
EInternalObjectFlags UE::GC::GMaybeUnreachableObjectFlag = EInternalObjectFlags::ReachabilityFlag2
 
bool UE::GC::GIsIncrementalReachabilityPending = false
 
class FObjectPurge GUObjectPurge
 
FReachabilityAnalysisState UE::GC::GReachabilityState
 

Macro Definition Documentation

◆ PERF_DETAILED_PER_CLASS_GC_STATS

#define PERF_DETAILED_PER_CLASS_GC_STATS   (LOOKING_FOR_PERF_ISSUES || 0)

◆ UE_LOG_FGCScopeGuard_LockAsync_Time

#define UE_LOG_FGCScopeGuard_LockAsync_Time   0

Function Documentation

◆ AcquireGCLock()

void AcquireGCLock ( )

◆ CollectGarbage()

void CollectGarbage ( EObjectFlags  KeepFlags,
bool  bPerformFullPurge = true 
)

Deletes all unreferenced objects, keeping objects that have any of the passed in KeepFlags set. Will wait for other threads to unlock GC.

Parameters
KeepFlagsobjects with those flags will be kept regardless of being referenced or not
bPerformFullPurgeif true, perform a full purge after the mark pass

◆ CSV_DEFINE_CATEGORY_MODULE()

CSV_DEFINE_CATEGORY_MODULE ( COREUOBJECT_API  ,
GC  ,
true   
)

◆ DEFINE_LOG_CATEGORY()

DEFINE_LOG_CATEGORY ( LogGarbage  )

◆ DissolveUnreachableClusters()

void DissolveUnreachableClusters ( UE::GC::EGatherOptions  Options)

◆ FinalizeIncrementalReachabilityAnalysis()

void FinalizeIncrementalReachabilityAnalysis ( )

Finalizes incremental reachability analysis (if currently running) without any time limit

◆ GatherUnreachableObjects()

void GatherUnreachableObjects ( bool  bForceSingleThreaded)

Gathers unreachable objects for IncrementalPurgeGarbage.

Parameters
bForceSingleThreadedtrue to force the process to just one thread

◆ GetIncrementalReachabilityAnalysisEnabled()

bool GetIncrementalReachabilityAnalysisEnabled ( )

Returns true if incremental reachability analysis is enabled

◆ GetLastGCDuration()

double GetLastGCDuration ( )

Gets the duration of the last GC run.

Returns
Returns the last GC duration, -1 if GC has never run.

◆ GetLastGCTime()

double GetLastGCTime ( )

Gets the last time that the GC was run.

Returns
Returns the FPlatformTime::Seconds() for the last garbage collection, 0 if GC has never run.

◆ GetNumCollectReferenceWorkers()

int32 GetNumCollectReferenceWorkers ( )

◆ GetReachabilityAnalysisTimeLimit()

float GetReachabilityAnalysisTimeLimit ( )

Returns the time limit for incremental rachability analysis.

Returns
time limit (in seconds) for incremental rachability analysis.

◆ IncrementalPurgeGarbage()

void IncrementalPurgeGarbage ( bool  bUseTimeLimit,
double  TimeLimit 
)

Incrementally purge garbage by deleting all unreferenced objects after routing Destroy.

Calling code needs to be EXTREMELY careful when and how to call this function as RF_Unreachable cannot change on any objects unless any pending purge has completed!

Parameters
bUseTimeLimitwhether the time limit parameter should be used
TimeLimitsoft time limit for this function call

◆ IsGarbageCollectingAndLockingUObjectHashTables()

bool IsGarbageCollectingAndLockingUObjectHashTables ( )

Whether garbage collection is locking the global uobject hash tables

◆ IsGarbageCollectionLocked()

bool IsGarbageCollectionLocked ( )

Returns true if we're inside a FGCScopeGuard

◆ IsIncrementalPurgePending()

bool IsIncrementalPurgePending ( )

Returns whether an incremental purge is still pending/ in progress.

Returns
true if incremental purge needs to be kicked off or is currently in progress, false othwerise.

◆ IsIncrementalReachabilityAnalysisPending()

bool IsIncrementalReachabilityAnalysisPending ( )

Returns whether an incremental reachability analysis is still pending/ in progress.

Returns
true if incremental reachability analysis needs to be kicked off or is currently in progress, false othwerise.

◆ IsIncrementalUnhashPending()

bool IsIncrementalUnhashPending ( )

Checks if there's objects pending to be unhashed when running incremental purge

Returns
true if the time limit passed and there's still objects pending to be unhashed

◆ NotifyUnreachableObjects()

void NotifyUnreachableObjects ( const TArrayView< FUObjectItem * > &  UnreachableObjects)

Call back into the async loading code to inform of the destruction of serialized objects

◆ OnDisregardForGCSetDisabled()

void OnDisregardForGCSetDisabled ( int32  NumObjects)

◆ PerformIncrementalReachabilityAnalysis()

void PerformIncrementalReachabilityAnalysis ( double  TimeLimit)

Incrementally perform reachability analysis

Parameters
TimeLimitTime limit (in seconds) for this function call. 0.0 results in no time limit being used.

◆ PurgeAllUObjectsOnExit()

void PurgeAllUObjectsOnExit ( )

Destroys all existing UObjects when the engine shuts down. Does NOT perform reachability analysis to do so.

◆ ReleaseGCLock()

void ReleaseGCLock ( )

◆ SetIncrementalReachabilityAnalysisEnabled()

void SetIncrementalReachabilityAnalysisEnabled ( bool  bEnabled)

Enables or disables incremental reachability analysis.

Parameters
bEnabledTrue if incremental reachability analysis is to be enabled

◆ SetReachabilityAnalysisTimeLimit()

void SetReachabilityAnalysisTimeLimit ( float  TimeLimitSeconds)

Sets time limit for incremental rachability analysis (if enabled).

Parameters
TimeLimitSecondsTime limit (in seconds) for incremental raachability analysis

◆ ShutdownGarbageCollection()

void ShutdownGarbageCollection ( )

◆ TryCollectGarbage()

bool TryCollectGarbage ( EObjectFlags  KeepFlags,
bool  bPerformFullPurge = true 
)

Performs garbage collection only if no other thread holds a lock on GC

Parameters
KeepFlagsobjects with those flags will be kept regardless of being referenced or not
bPerformFullPurgeif true, perform a full purge after the mark pass
Returns
true if an actual attempt at garbage collection occurred

◆ TSAN_ATOMIC()

TSAN_ATOMIC ( bool  )

◆ UnhashUnreachableObjects()

bool UnhashUnreachableObjects ( bool  bUseTimeLimit,
double  TimeLimit = 0.0 
)

Calls ConditionalBeginDestroy on unreachable objects

Parameters
bUseTimeLimitwhether the time limit parameter should be used
TimeLimitsoft time limit for this function call
Returns
true if the time limit passed and there's still objects pending to be unhashed

Variable Documentation

◆ GCStartTime

double GCStartTime = 0.

Time the GC started, needs to be reset on return from being in the background on some OSs

◆ GDelayReachabilityIterations

int32 GDelayReachabilityIterations = 0

◆ GGarbageReferenceTrackingEnabled

int32 GGarbageReferenceTrackingEnabled = 0

◆ GGCSingleton

FGCCSyncObject* GGCSingleton

◆ GIsGarbageCollectingAndLockingUObjectHashTables

std::atomic<bool> GIsGarbageCollectingAndLockingUObjectHashTables = false

Whether UObject hash tables are locked by GC

◆ GObjectCountDuringLastMarkPhase

FThreadSafeCounter GObjectCountDuringLastMarkPhase

Object count during last mark phase

◆ GObjIncrementalPurgeIsInProgress

std::atomic<bool> GObjIncrementalPurgeIsInProgress = false

Whether incremental object purge is in progress

◆ GObjUnhashUnreachableIsInProgress

std::atomic<bool> GObjUnhashUnreachableIsInProgress = false

Whether GC is currently routing BeginDestroy to objects

◆ GShouldVerifyGCAssumptionsOnFullPurge

COREUOBJECT_API bool GShouldVerifyGCAssumptionsOnFullPurge
extern

Allows release builds to override not verifying GC assumptions. Useful for profiling as it's hitchy.

If set and VERIFY_DISREGARD_GC_ASSUMPTIONS is set, we verify GC assumptions when performing a full (blocking) purge

◆ GUObjectPurge

class FObjectPurge GUObjectPurge

◆ GVerifyGCAssumptionsChance

COREUOBJECT_API float GVerifyGCAssumptionsChance
extern

If > 0 and VERIFY_DISREGARD_GC_ASSUMPTIONS is set, we verify GC assumptions on that fraction of GCs.