Search Unity

Customize your memory use with Unity 2021 LTS

April 20, 2022 in Engine & platform | 6 min. read
In-editor game footage with a giant frog
In-editor game footage with a giant frog
Share

Is this article helpful for you?

Thank you for your feedback!

One of our priorities is improving Unity for game studios working on larger projects. As a part of this effort, Unity 2021 LTS now lets you customize how memory is allocated.

Read on to find out how memory management works in Unity, how to diagnose performance issues related to memory allocation, what settings are available to fix these issues, and how to use them.

What does Memory Allocator customization mean?

Unity uses three memory management layers to handle memory in your application:

  • Managed memory
  • C# unmanaged memory
  • Native memory

Internally, Unity has different ways to allocate native memory, known as allocators. This blog post will focus on two:

  • The bucket allocator, which groups small allocations of similar sizes into shared blocks of memory. It’s generally very fast and widely used, but it has a hard coded limit to how big it can grow.
  • There is also a dynamic heap allocator, a general-purpose allocator that’s intended for the majority of Unity native memory allocations. It’s slower than the bucket allocator because it has to handle many different cases.

When we request new memory from the memory management system, we check the size first. If it’s smaller than a certain threshold, we ask the bucket allocator for memory. Once the bucket allocator has filled up, we still need to allocate memory from somewhere, so these small allocations will come from the dynamic heap allocator. Falling back to the dynamic heap allocator is slower, and adding lots of small allocations into the dynamic heap can lead to increased fragmentation of the dynamic heap.

How customizing memory helped ship Ori on Nintendo Switch

In summer 2020, Unity provided support for Moon Studios when they were working to port Ori and the Will of the Wisp to Nintendo Switch. You can learn more about how we worked together to scale the project for this target platform in this case study.

Moon Studios and Unity Accelerate Solutions (formerly Unity Professional Services) analyzed sections of code together, finding ways to tweak the particle systems and reduce memory usage. “When you’re targeting 60 fps on Nintendo Switch, you have to make sure every single aspect is optimized, including simulation, streaming, rendering, and graphics,” says studio founder Gennadiy Korol.

Ori and the Will of the Wisps pushes the Nintendo Switch very hard in terms of memory use. The Accelerate Solutions team helped Moon make sure they were getting the best from these layers, focusing on native memory allocation. 

Working with the team from Moon Studios, the Unity team measured the number of allocations from the different allocators. They also looked at the amount of memory allocated from each, which showed that the game was filling up some of the bucket allocators before even reaching the start of gameplay.  

Increasing the size of the bucket allocator allowed extra room for small allocations that happen when playing the game. Allocating these small blocks from the bucket allocator gives the game a performance boost, helping it to hit the 60 fps target. The small allocations no longer come from the dynamic heap allocator, so this allocator contains just large blocks of memory. The allocator is less fragmented, which keeps the game running smoothly for longer.  

Following the collaboration with Moon, the Accelerate Solutions team worked with the Optimization team, who developed the memory allocator customization feature that is now available in 2021 LTS.

Getting started with balancing memory set up

Before 2021 LTS, balancing memory setup has been inaccessible to Unity users, but now you too can make sure you’re getting the best memory utilization for your game. Measure how much memory is used by different allocators, then customize their size to suit your game. Check out the documentation, and let us know how it’s going – we’d love to hear about your experience. Please join us on the Unity forum to share your project performance analysis, improvements, and challenges faced along the way.

April 20, 2022 in Engine & platform | 6 min. read

Is this article helpful for you?

Thank you for your feedback!

Related Posts