Chris McCole

View Original

UE4 Memory Optimization Profiling and Techniques

When porting our UE4 game onto Switch, we had to make sure to utilize less RAM than on other platforms. Switch being limited to a small 3GB of RAM for the game developers to take advantage of. It was really important that we limited our memory consumption, as well as understood how much we were using, and where it was coming from.

The process for minimizing and debugging your memory usage should follow in three steps:

  1. Analyze your memory usage finding crucial assets

  2. Reduce or remove the bloated assets

  3. Reanalyze your memory usage to determine the improvement

To break down the process of where memory goes in your Unreal project, there are a couple places that you can look.

  1. Statistics Window (Window->Statistics)

  2. Memreport -full (debug command)

The statistics window is more for quick statistics pertaining to your assets. Typically sorted by their total size. This can be really helpful to at least see what assets are huge, and if they could be cut or reduced.

The more impactful, accurate and platform-specific way would be to use the command Memreport -full.

To activate this command in the Editor, you can click on the ` key to bring up the command line interface, then you can type in “Memreport -full” and click the “Enter” key. It will take a few seconds to run, and once it’s done it should output all of your game’s memory contents into a text file in your Project directory. It should be located at {PROJECTDIR}\Saved\Profiling\MemReports\UEDPIE\_0__{MAPNAME}-{PLATFORM}-{DATETIME}\UEDPIE\_0__{MAPNAME}-{PLATFORM}-{DATETIME}.memreport.

You can open this up in your preferred text editor, however, this IDE will ideally have text diff support so that you can easily look at the differences between your pre-optimization and post-optimization to get an idea of how much you really improved and where those improvements come from.

The contents of the *.memreport file are as follows:

  1. Total Platform Memory Statistics

  2. Texture Memory and Memory Groups Statistics (in bytes)

  3. List of all loaded objects, their number of instances in the scene, as well as their size in KB

  4. RHI resouece memory

  5. Levels currently loaded

  6. Spawned Actors in persistent

  7. Particle Stats

  8. Config Cache

  9. Pooled Render Targets

  10. All textures

  11. Particle Systems

  12. Sound Objects

  13. Skeletal Mesh Objects

  14. Static Mesh Objects

  15. Levels

  16. Static Mesh Components

  17. Total Objects

In my experience, I have typically found that most of my memory goes to textures, which makes sense, if you have one 4k uncompressed texture at 32-bit (4 byte) colors. You would have one texture that accounts for 67,108,864 bytes (67 MB) of memory 46 of these and you’d be over 3 GiB. Realistically you probably won’t have any or many uncompressed textures, and most textures won’t need to be 4k, but you’d be surprised at how many textures are accidentally larger than you would want them to be. Or uncompressed when they should be compressed, this typically seems to occur with non-power-of-two textures, especially if they are UI pieces.

On the other hand, you might notice that something, a texture, or skeletal mesh is loaded that is not used in your project, or if it is used, it shouldn’t be loaded currently! Occasionally, this seems to occur from these assets being loaded in Unreal, but not your currently loaded level exactly. Closing the engine, reopening and restarting can help mitigate these instances. However, the best gauge of how large these assets are and how many there are would be to test in a development build outside of the editor. This will give more accurate results, however, the Editor is pretty much fine for a good overview of your memory usage.

Once you have your list of assets that shouldn’t be loaded or need to be reduced in size, go back to your editor and start looking for these assets, sometimes searching for them in the project browser is the best way to find these assets. If it’s being loaded when it shouldn’t in these cases, you’ll want to use the reference viewer to see where the asset is being used. For instance, I can see what material is referencing this texture.

When the assets ARE being loaded but they are much too large, you might want to look at reducing their size, for textures, you can change the compression settings, update the maximum resolution, or change the MIP Gen Settings. If there are lot’s of things that you want to use a similar size configuration, such as all character clothing, you can assign them all to a texture group, such as “Character”.

If you have the Texture Group set, then you can change the maximum texture size, LOD bias, MinMag Filter, Mip Filter, or Mip Gen Settings for all textures of this group at once. Even more, you can specify these settings per platform, so that on mobile/Switch the resolution is lower than when building for PC, or Xbox Series, or PS5.

For things such as Skeletal Meshes, you could try using the Auto-LOD system to reduce the base LOD triangle count, or make more LODs, and on the specified platform, set the maximum LOD to use a lower LOD than 0, such as 1.

Otherwise, you could offload more assets into different levels if things don’t always need to be loaded at once.

To specify different Texture Group settings, or set other Console variables, such as scalability group values, of rendering configuration settings, you can use the “Device Profiles” window to easily set these values per-platform. You can find this window via: “Window->Developer Tools->Device Profiles”.

This way you can easily use more RAM and have higher quality assets on higher end platforms, and then only reduce where you need to on lower-end platforms with less RAM or processing power.

Conclusion

Unreal provides many tools and a clean, simple breakdown of all of your assets, and their usage. With these tools at your disposal, you should be able to easily cut down on much of your memory usage!


If you found this tutorial helpful and want to help support me in creating this content that I host and publish for free, please consider contributing to my Patreon or Ko-fi!

See this form in the original post