Search

Getting Assets’ Runtime Memory Sizes in Unity Editor

Profiler.GetRuntimeMemorySizeLong()

This API returns the runtime memory size of a Unity asset. You can call this API both in the Unity editor and the player. But the returned size in the Unity editor will differ from those in the player.
This is the pain point of profiling. You should build your game and run it on the target devices to get the correct values.
This post will explain how to get the correct runtime memory sizes of major asset types in the Unity editor.

Meshes

It’s easy in the case of meshes. You can trust the returned size of Profiler.GetRuntimeMemorySizeLong() in the Unity editor.
But if you enabled ‘Optimize Mesh Data’ in the Player Settings, Unity will discard unused vertex attributes. In this case, use asset bundles for a more accurate size. Refer to the Animation Clips section.

Textures

You can’t trust the returned size for textures. But as you know, the Unity editor tells you the correct memory size in the texture preview window. I managed to find the API used in the preview window. It is UnityEditor.TextureUtil.GetStorageMemorySizeLong() and UnityEditor.TextureUtil.GetRuntimeMemorySizeLong().
Unity uses these two APIs in various texture inspectors, such as TextureInspector, SpriteAtlasInspector, and Texture2DArrayInspector.
You need to use reflections to call these APIs.
MethodInfo miGetStorageMemorySizeLong; MethodInfo miGetRuntimeMemorySizeLong; foreach( var assembly in AppDomain.CurrentDomain.GetAssemblies()) foreach (var type in assembly.GetTypes()) { if (type.FullName == "UnityEditor.TextureUtil") { miGetStorageMemorySizeLong = type.GetMethod("GetStorageMemorySizeLong", BindingFlags.Static | BindingFlags.Public); miGetRuntimeMemorySizeLong = type.GetMethod("GetRuntimeMemorySizeLong", BindingFlags.Static | BindingFlags.Public); break; } }
C#

Animation Clips

You can’t trust the returned size for animation clips. But, if you loaded an asset from an asset bundle for the target platform and passed it to Profiler.GetRuntimeMemorySizeLong(), the returned size would be pretty much like the value you get from the profiler on the device.