Unreal memory management c++
1. Understanding unreal engine memory management
Memory Heaps and Allocation
Memory Heaps In Unreal Engine, memory heaps are large blocks of memory reserved for dynamic allocation. The engine uses these heaps to manage memory for objects, textures, meshes, and other resources during runtime. Unreal Engine typically maintains multiple heaps to separate different types of memory allocations, such as those for small objects versus large objects, to optimize performance and reduce fragmentation.
Memory Allocation Memory allocation in Unreal Engine is primarily handled by the engine's allocator, which is responsible for managing the memory heaps and servicing allocation requests. Unreal Engine uses a custom memory allocator that is optimized for game development, balancing the needs for speed, low overhead, and minimizing fragmentation.
Custom Allocators Developers can also implement custom allocators for specialized memory management needs. These custom allocators can be fine-tuned for specific use cases, such as high-frequency allocation and deallocation, which might occur in specific subsystems like AI or physics.
Garbage collection
Garbage Collection Mechanism Unreal Engine uses a garbage collection (GC) mechanism to automatically manage the lifecycle of objects. Garbage collection helps to reclaim memory that is no longer in use, preventing memory leaks and ensuring that the game does not consume more memory than necessary over time.
Mark and Sweep Algorithm The garbage collector in Unreal Engine primarily uses the mark-and-sweep algorithm. During the mark phase, the GC traverses all active objects starting from the root set (objects that are directly referenced) and marks them as reachable. In the sweep phase, the GC scans through all objects and reclaims the memory of those that are not marked as reachable.
Controlling Garbage Collection Developers can control and tune the garbage collection process to balance performance and memory usage. Unreal Engine provides settings to adjust the frequency and duration of garbage collection cycles.
Reference counting
Reference Counting Mechanism Reference counting is a technique used to manage the lifecycle of objects by keeping track of how many references point to a particular object. In Unreal Engine, reference counting is used extensively, especially with smart pointers like
TSharedPtr
andTWeakPtr
.TSharedPtr and TWeakPtr
TSharedPtr
is a smart pointer that maintains a reference count for the object it points to. When the reference count drops to zero, the object is automatically destroyed.TWeakPtr
is a weak reference to an object managed byTSharedPtr
, which does not affect the reference count and can be used to check if the object still exists without keeping it alive.
2. Smart pointers
Smart pointers are a powerful feature in C++ that help manage memory by automatically handling the allocation and deallocation of objects. Unreal Engine provides its own implementations of smart pointers that are tailored to the needs of game development. These include TSharedPtr
, TWeakPtr
, and TUniquePtr
.
Types of Smart Pointers in Unreal Engine
TSharedPtr
TWeakPtr
TUniquePtr
T Shared Ptr
TSharedPtr
is a reference-counted smart pointer that ensures an object is destroyed when there are no more references to it. This is particularly useful for objects that are shared across multiple parts of the program.
Benefits:
Automatic Memory Management: Automatically destroys the object when the last reference is removed.
Thread Safety: Safe to use in multi-threaded environments.
Shared Ownership: Multiple parts of the code can share ownership of an object.
T Weak Ptr
TWeakPtr
is a non-owning smart pointer that holds a weak reference to an object managed by TSharedPtr
. It does not affect the reference count and is used to break circular references or to reference objects that may be destroyed.
Benefits:
Avoid Circular References: Helps in breaking circular references that could lead to memory leaks.
Conditional Access: Allows checking if the object is still valid without preventing its destruction
T Unique ptr
TUniquePtr
is a smart pointer that provides exclusive ownership of an object. It is not reference-counted and cannot be copied, only moved. This is useful for objects that should have a single owner.
Benefits:
Exclusive Ownership: Ensures that an object has only one owner at a time.
Efficiency: No reference counting overhead, making it more efficient than
TSharedPtr
for unique ownership.
Example Usage
3. Managing uobjects in unreal
In Unreal Engine, UObject
is the base class for all objects that can be managed by the engine's garbage collection system. This includes actors, components, and other gameplay-related objects. Understanding how UObjects
are managed in memory is crucial for efficient game development.
Key Points:
Garbage Collection:
UObjects
are automatically managed by Unreal Engine's garbage collector, which uses a mark-and-sweep algorithm to clean up unused objects.Reference Counting: Although
UObjects
do not use traditional reference counting, Unreal Engine tracks references through properties and collections that holdUObjects
.Lifecycle Management: The engine ensures that
UObjects
are created and destroyed in a controlled manner, preventing memory leaks and dangling pointers.
Creating and destroying UObjects
UObjects can be created using the NewObject
function, which handles initialization and registration with the garbage collector.
UObjects are generally destroyed by the garbage collector. However, you can manually mark them for deletion using the MarkPendingKill
function.
When an object is marked for deletion, it will be cleaned up during the next garbage collection cycle. Directly deleting a UObject is not recommended, as it can lead to instability.
T-Weak Objects Ptr
TWeakObjectPtr
is a weak pointer to a UObject
. It does not prevent the referenced object from being garbage collected. This is useful for references that should not extend the lifetime of the object.
Benefits:
Avoids Circular References: Helps prevent memory leaks caused by circular references.
Efficient Checking: Allows checking if an object is still valid without extending its lifetime.
T Soft Object Ptr
TSoftObjectPtr
is a soft reference to a UObject
. It stores the object's reference by path, allowing the object to be garbage collected and reloaded as needed.
Benefits:
Memory Optimization: Allows objects to be unloaded and reloaded, reducing memory usage.
Flexibility: Useful for managing large numbers of assets that may not be needed simultaneously.