Skip to content

Memory Budgeting & Resource Unloading

Vitaly Popuzin edited this page Dec 8, 2023 · 6 revisions

Memory Budgeting

Memory budgeting in Unity is essential for maintaining system stability and preventing memory overflow and crashes by keeping memory usage within set limits. It ensures smooth application performance across various platforms with different hardware capacities. This approach also aids in efficient resource management, allowing for strategic asset loading and unloading decisions.

In the project there are several components related to it:

  1. IProfilingProvider provides information about current memory usage by reading it from respective Unity built-in ProfilerRecorder
  2. ISystemMemory provides information about total available system memory. Polymorphism should be used to access data on different devices.
  3. MemoryBudgetProvider is core component of the memory budgeting system. It implements IConcurrentBudgetProvider and uses data from IProfilingProvider to understand its relation to the total system memory from ISystemMemory and relates it to the respective threshold. It will return false for TrySpendBudget() call if the memory usage is in the Full threshold (defined by settings as a certain percentage of total system memory).

Similarly to other budget providers, this MemoryBudgetProvider then is used by different systems that are potentially can lead to memory consumption. Such systems prevent loading flow when the budget cannot be spend. Examples could be found in CreateGltfAssetFromAssetBundleSystem and DeferredLoadingSystem.

---
title: Memory Budgeting for Loading
---
classDiagram

class ClientSystem["System that utilize memory budget"]
class IConcurrentBudgetProvider {+TrySpendBudget() bool }    

ClientSystem --> IConcurrentBudgetProvider : Uses TrySpendBudget() in its own loading logic
direction LR
IConcurrentBudgetProvider <|-- MemoryBudgetProvider  : implements
MemoryBudgetProvider --> IProfilingProvider : get currently used memory
MemoryBudgetProvider --> ISystemMomery : get total available memory
Loading

Resources Unloading

Chains of Referencing and Dereferencing

Cache Clearing

Debug and Profiling

Profiling Counters and Markers

Debug view

Tests

Clone this wiki locally