Unreal Memory Management

The surprising benefits of a simple stroll

Published

Sep 20, 2025

Topic

Thoughts

Overview

Efficient memory management is essential for high-performance Unreal Engine projects. Unreal uses a mix of custom allocators, garbage collection, and smart pointer systems to balance performance and memory safety.

Memory Heaps & Allocation

Memory Heaps

Unreal Engine organizes memory into large blocks called heaps. These heaps are used during runtime to dynamically allocate memory for:

  • Textures

  • Meshes

  • Objects

  • Other runtime data

Different heaps exist for different object sizes to reduce fragmentation and improve performance.

Engine Memory Allocation

Unreal uses its own custom memory allocator, tailored for:

  • Low fragmentation

  • High-speed allocation/deallocation

  • Game-specific requirements

Custom Allocators

Developers can also define their own allocators for specific systems (e.g., AI, physics). This is especially useful when frequent small allocations are expected.

Garbage Collection (GC)

How GC Works

Unreal Engine uses a Mark-and-Sweep garbage collection mechanism:

  1. Mark Phase: Identifies all reachable (referenced) objects.

  2. Sweep Phase: Deletes unreferenced (unreachable) objects.

GC Controls

You can customize garbage collection behavior:

GEngine->ForceGarbageCollection(true);
GEngine->SetMaxMemoryAllowanceForGC(256 * 1024 * 1024); // 256 MB

Adjust GC frequency, thresholds, and manual triggers to optimize performance.

Reference Counting

Mechanism

Unreal implements reference counting through smart pointers to manage object lifecycles. When the reference count hits zero, the object is destroyed.

TSharedPtr & TWeakPtr

  • TSharedPtr: Increases ref count, destroys object when count = 0.

  • TWeakPtr: Does not increase ref count. Used to check if an object still exists.

TSharedPtr<MyObject> Shared = MakeShareable(new MyObject());
TWeakPtr<MyObject> Weak = Shared;

if (TSharedPtr<MyObject> Locked = Weak.Pin()) {
    Locked->DoSomething();
}

Smart Pointers in Unreal Engine

Types

  1. TSharedPtr - Shared ownership

  2. TWeakPtr - Weak (non-owning) reference

  3. TUniquePtr - Exclusive ownership

Benefits

Type

Ownership

Ref Count

GC Safe

Thread Safe

TSharedPtr

Shared

Yes

No

Yes

TWeakPtr

Non-owning

No

No

Yes

TUniquePtr

Exclusive

No

No

Yes

TSharedPtr Example

TSharedPtr<MyObject> MyPtr = MakeShareable(new MyObject());
MyPtr->DoSomething()

TWeakPtr Example

TWeakPtr<MyObject> Weak = MyPtr;
if (TSharedPtr<MyObject> Locked = Weak.Pin()) {
    Locked->DoSomething();
}

TUniquePtr Example

TUniquePtr<MyObject> Unique = MakeUnique<MyObject>();
Unique->DoSomething()

Managing UObject

Key Concepts

  • UObject is the base class for GC-managed objects.

  • Memory is automatically tracked by Unreal's GC system.

  • Objects are created via NewObject<T>().

UMyObject* Obj = NewObject<UMyObject>(this);
Obj->MarkPendingKill()

TWeakObjectPtr

Acts like TWeakPtr, but specifically for UObject. Useful to prevent dangling references:

TWeakObjectPtr<UMyObject> WeakPtr = Obj;
if (WeakPtr.IsValid()) {
    WeakPtr->SomeFunction();
}

TSoftObjectPtr

Path-based reference. Does not keep the object in memory and allows async loading:

TSoftObjectPtr<UMyObject> SoftPtr = FSoftObjectPath("/Game/MyObject.MyObject");
SoftPtr.LoadAsync([](UMyObject* Loaded) {
    if (Loaded) Loaded->DoSomething();
})

Summary

Feature

Use Case

Memory Heaps

Bulk memory pools for various object types

Custom Allocators

High-performance needs (e.g., physics/AI)

Garbage Collection

Automatic cleanup for engine objects

Smart Pointers

Safe and efficient manual memory management

UObject System

Engine-integrated memory and lifecycle mgmt

TSoftObjectPtr

Async loading of content by path

TWeakObjectPtr

Safe non-owning reference to UObject

Outfit

Make better experiences!

© All right reserved

Make better experiences!

© All right reserved

Make better experiences!

© All right reserved

Make better experiences!

© All right reserved