Hello,
I am building a tool to verify my materials and textures. In my script I am using the method
UnityEngine.Profiling.Profiler.GetRuntimeMemorySizeLong to get the size of all the textures used by the selected material.
This method returns exactly 2x the size displayed in the editor when you preview your texture.
How is this size calculated, what does it represent exactly ?
If I want to get the texture memory usage, do I have to divide the result by 2 manually or is this returned size accurate ?
Hi @AurelienChab ! GetRuntimeMemorySizeLong will include the impact on the CPU side and GPU side, so may appear to be double what you might expect.
In Editor, both CPU and GPU copies are always loaded, but in players, marking the texture as Read-Only should mean you only have the GPU copy.
2 Likes
Hi there!
Sorry to resurrect this thread. I’m making a tool to evaluate the VRAM of all the textures in a scene
I’ve found that GetRuntimeMemorySizeLong() returns a different value if crunch compression is enabled on a texture
I was curious as to how this could possibly be the case based on your post. So I did some testing in a scene that just contained 50 cubes each with unique 4k textures, and measured the impact of compression and crunch compression
These were my results from measuring my system’s VRAM changes:
Uncompresseed Textures: ~4.2 GB VRAM
Compressed Textures: ~0.6GB VRAM
Crunch Compressed Textures: ~0.6GB VRAM
However, based on adding up the values that Unity spits out from GetRuntimeMemorySizeLong(), I get the following:
Uncompressed Textures: ~7.3GB VRAM
Compressed Textures: ~1.4GB VRAM
Crunch Compressed Textures: ~0.58GB VRAM
So some pretty big inconsistencies there!
Either the function isn’t returning what I think it is, or it’s inaccurate
Is the function returning the value in the editor or in the player? If the former, would there be a convenient way to preview that the player-side impact would be instead?
(I should note that in all tests, all the textures had read/write disabled, and mipmaps enabled, but with both mipmap and texture streaming disabled)
Did you measure and log out the sizes in the Player or in an Editor? It returns the size for whatever the code is currently running in.
No. At least not at the moment. We’ve considered trying to make the size estimation calculations available in the Editor as a prediction for a specified target but there’s a lot that needs to be figured out for that and that hasn’t happened yet.
As for VRAM sizes: the Memory Profiler and this API doesn’t know about the precise VRAM usage, that’s kind of up to the graphics driver internals. The graphics sizes for non-unified memory architecture platforms are calculated sizes for the amount of RAM that the graphics driver will need to allocate, both to then send it to VRAM but also as a backup in case the OS decides to swap the app off of the GPU and flush out the VRAM when you e.g. you tab out, and then the graphics driver needs to reupload that memory once your app resumes, using that backup. This is also explained in this 2023 Unite Talk.
Yes I was measuring in editor
Understood, thankyou for the info!
1 Like