gc.ForceCollectGarbageEveryFrame
gc.ForceCollectGarbageEveryFrame
#Overview
name: gc.ForceCollectGarbageEveryFrame
This variable is created as a Console Variable (cvar).
- type:
Var
- help:
If set to 1, the engine will force GC each frame.
It is referenced in 3
C++ source files.
#Summary
#Usage in the C++ source code
The purpose of gc.ForceCollectGarbageEveryFrame is to control the garbage collection behavior in Unreal Engine 5. Specifically, it forces the engine to perform garbage collection every frame when enabled.
This setting variable is primarily used by the Unreal Engine’s garbage collection system, which is a core component of the engine’s memory management. Based on the callsites, it appears to be utilized within the Engine module, particularly in the UnrealEngine.cpp file.
The value of this variable is set through a console variable (CVar) system. It’s initialized with a default value of 0, meaning it’s disabled by default. Developers can change this value at runtime using console commands or through configuration files.
The associated variable CVarForceCollectGarbageEveryFrame interacts directly with gc.ForceCollectGarbageEveryFrame. They share the same value and purpose, with CVarForceCollectGarbageEveryFrame being the actual console variable object used in the code.
Developers must be aware that enabling this variable (setting it to 1) will force garbage collection to occur every frame, which can have significant performance implications. This should typically only be used for debugging or profiling purposes, not in production builds.
Best practices when using this variable include:
- Use it sparingly and only when investigating memory-related issues.
- Be aware of the performance impact when enabled.
- Always disable it (set to 0) for production builds.
- Consider using it in conjunction with other memory profiling tools to get a comprehensive view of memory usage.
Regarding the associated variable CVarForceCollectGarbageEveryFrame:
The purpose of CVarForceCollectGarbageEveryFrame is to provide a programmatic interface to the gc.ForceCollectGarbageEveryFrame setting. It’s an instance of TAutoConsoleVariable
This variable is used within the Engine module, specifically in the UEngine::ConditionalCollectGarbage function, to determine whether to force garbage collection every frame.
The value of CVarForceCollectGarbageEveryFrame is set when the gc.ForceCollectGarbageEveryFrame console variable is set. They are effectively two interfaces to the same setting.
Developers should be aware that this variable is checked on the game thread (GetValueOnGameThread()), which means changes to this value will only take effect on the next frame.
Best practices for using CVarForceCollectGarbageEveryFrame are similar to those for gc.ForceCollectGarbageEveryFrame. Additionally, developers should be cautious about changing this value during performance-critical sections of code, as it could lead to unexpected frame time spikes.
#References in C++ code
#Callsites
This variable is referenced in the following C++ source code:
#Loc: <Workspace>/Engine/Source/Runtime/Engine/Private/UnrealEngine.cpp:1575
Scope: file
Source code excerpt:
TEXT("If set to 1, the engine will attempt to trigger GC each frame while async loading."));
static TAutoConsoleVariable<int32> CVarForceCollectGarbageEveryFrame(
TEXT("gc.ForceCollectGarbageEveryFrame"),
0,
TEXT("If set to 1, the engine will force GC each frame."));
#endif
int32 GPerformGCWhileAsyncLoading = 0;
static FAutoConsoleVariableRef CVarPerformGCWhileAsyncLoading(
#Associated Variable and Callsites
This variable is associated with another variable named CVarForceCollectGarbageEveryFrame
. They share the same value. See the following C++ source code.
#Loc: <Workspace>/Engine/Source/Runtime/Engine/Private/UnrealEngine.cpp:1574
Scope: file
Source code excerpt:
0,
TEXT("If set to 1, the engine will attempt to trigger GC each frame while async loading."));
static TAutoConsoleVariable<int32> CVarForceCollectGarbageEveryFrame(
TEXT("gc.ForceCollectGarbageEveryFrame"),
0,
TEXT("If set to 1, the engine will force GC each frame."));
#endif
int32 GPerformGCWhileAsyncLoading = 0;
#Loc: <Workspace>/Engine/Source/Runtime/Engine/Private/UnrealEngine.cpp:1791
Scope (from outer to inner):
file
function void UEngine::ConditionalCollectGarbage
Source code excerpt:
CollectGarbage(GARBAGE_COLLECTION_KEEPFLAGS, true);
}
else if (CVarForceCollectGarbageEveryFrame.GetValueOnGameThread())
{
CollectGarbage(GARBAGE_COLLECTION_KEEPFLAGS, true);
}
else
#endif
{