Question about texture memory usage

Hi everyone. I have a few questions about memory usage.

1: When I did
“SceneManager.UnloadSceneAsync(scene_name)”.

I noticed in the “Memory Profiler” that the baked light textures were still in memory even though the scene was already unloaded. How do I unload unnecessary lightmaps from memory after unloading the scene?

2: Can you please tell me, I see through “Memory Profiler” that the baked light texture (4k) takes 16mb. Although through the preview, viewing the asset shows 2.5 MB. I use crunch compression, if I disable compression, then the preview will also show 16mb. Do you know what is the reason for this? My target platform - Android.


As far as i know (correct me if iam wrong cuz i am too lazy to check docs)
UnloadSceneAsync does not unload resources
You must do it manually after it unloaded by calling https://docs.unity3d.com/ScriptReference/Resources.UnloadUnusedAssets.html

1 Like

Hi, I tried this, it didn’t help. Lightmap textures from previous scene are still in memory.

You’d probably want to pass UnloadSceneOptions.UnloadAllEmbeddedSceneObjects to the UnloadSceneAsync call.

As for the size discrepancy: the size in the Texture Preview is the size on disk, not in RAM. After selecting it in the table you can also check the right hand panel in the Memory Profiler to get additional info on the parameters of the texture that might contribute to its memory impact.

Hello again! I just realized that when I unload a scene and load a new one. The lightmap remains and therefore it is not unloaded. I do not understand how this works. All lightmaps are unloaded except for the one under index 0. And when a new scene is loaded, new lightmaps are simply added to the one under index 0. I need to somehow forcefully unload it from there.

I saw it through “Lighting → Baked Lightmaps” and there I saw that it remained.

P.S. I tried “UnloadSceneOptions.UnloadAllEmbeddedSceneObjects”. It didn’t help.

does the one under index 0 stay exactly the same or does it get replaced with a new one from the new scene?

If you load to an empty scene in between and take a capture there, does it unload all lightmaps?

The memory Profiler, unless opening a capture from the current Editor session (and memory profiling the Editor can show its own odd Editor-Only side effects on memory) doesn’t know specifically which texture in your project relates to which texture in a build. The Preview and Select In Editor functionality are based on doing a search in the currently opened project using the name and type of the asset, and if there is only one match, assuming that that is the one. So it might be misleading in this case?

Hi. No, it wasn’t replaced. It was left over from the previous scene. In the new scene I had 30 lightmaps, but because the previous lightmap wasn’t unloaded, I ended up with 31.

If I load the next scene immediately 30 ligtmaps succesfull loaded, without one from previus.

I solved the problem the following way.
After the scene was unloaded, I used:

LightmapSettings.lightmaps = null;
AsyncOperation unloadAssetsOperation = Resources.UnloadUnusedAssets();

And then everything was successfully unloaded from memory. In the next scene these lightmaps are not used, so they should have been unloaded.

Could you please file a bug for this with a repro project? This seems like something that we should investigate :slight_smile: