Jump to content

Memory Allocated Not Clearing After Unloading Scenes


Roi

Recommended Posts

Hi
I try to test the ability to create a big terrain game.
I generated a 40X40 1024m tile based terrain using the Terrain Loader Manager, and the loading/unloading terrains and impostors works fine.
on the build the fps is reasonable and I think I can live with the small lagging which happens at loading new terrains or impostors while moving the player around.
my question is about the Memory Allocated data which appears on the editor under the terrain loader manager.
as of my understanding is that loaded scenes with terrains or impostors consume memory.
the ones that are disabled in the hierarchy are kept for a defined time period and when that times runs out the terrain and it's scene are being unloaded therefore clearing some memory.
the field property has a green or red background color indicating if the memory allocated is in the cache memory threshold limit.
if it exceeds the limit then the scenes which are loaded but disabled will be unloaded to clear memory even if their time has not run up yet.
have I understood it correctly ?
In my case on runtime in the editor I see that at the beginning the color is green meaning that all the terrains which were loaded on start is consuming that memory amount and there is enough free memory to use caching.
but after moving the player to some distance while most of the starting terrains are now unloaded the memory allocated keep increasing.
I think when scenes are unloaded the memory allocated decrease only by a small value (1-2) but loading new terrains consumes a lot more (200) so that value will only go up and eventually crash the game.
can you please explain how to monitor that memory to prevent crashes ?
Thank you 

Link to comment
Share on other sites

Hi @Roi, you understood the purpose of the memory display in the Terrain Loader Manager correctly. The Terrain Loading process of Gaia does not control Unity's memory cleanup directly, we unload the scenes when they are not in scope of any loader anymore, but this does not necessarily immediately bring the memory usage down, as it is up to unity when to clean up unused assets from memory.
There is a call to the unity api that forces the memory cleanup called Resources.UnloadUnusedAssets(), we did call this in the past if I remember correctly, but removed it after users ran into issues with it.
It looks like we need to review this however, as I found two direct recommendations from Unity to DO call this when unloading a scene here (see "Scene Unloading") and here (see Notes at the bottom).
We need to test if this causes any issues especially with the latests 2022.2 release of unity, as we just removed calls to this function for this version as they were causing crashes and other issues (in a different context though).

Link to comment
Share on other sites

Hi thank you for answering.

from the unity docs about unloading scene :

"Note: Assets are currently not unloaded. In order to free up asset memory call Resources.UnloadUnusedAssets."

https://docs.unity3d.com/ScriptReference/SceneManagement.SceneManager.UnloadSceneAsync.html

(at the bottom of the page)

I don't know what issues other people had with this function but if those unloaded scene wont free memory then all of the terrain streaming idea kind of lose it's basic purpose to be able to run big terrains, if the memory usage will only increase then at some point it will run out, so if I would like to try using that Resources.UnloadUnusedAssets function myself, where and when should I call it ?

Thank you

 

 

 

Link to comment
Share on other sites

Hi @Roi, the issues that users had with it was a performance impact on unloading the terrains, and in the other case we had basic functionalities of Gaia not working anymore or crashing the editor even when Resources.UnloadUnusedAssets was called in Unity 2022.2 - that was an entirely different context though that had nothing to do with Terrain Loading.

The code would need to be added in the TerrainScene.cs script when the terrains are being unloaded. The unloading is an asynchronous operation though and you would need to wait until the unload is completed.
I went and created a small patch for that, this will add two options in the Terrain Loader Manager to control whether you want this to be called during runtime and in Editor Mode:


image.png

From first tests this did not cause issues for me both in runtime and in Editor mode, and you can indeed see memory usage going down on terrains being unloaded (they need to go out of cache as well of course so that they are fully removed from the scene).
Here is the patch, it is an .unitypackage that can be installed in the Editor via Assets > Import Package > Custom Package
UnloadUnusedAssetsPatch.zip

Link to comment
Share on other sites

Hi Thank you very much for the patch, I will try it out.

I did some more tests meanwhile and called the Resources.UnloadUnusedAssets function myself by a normal script and indeed the memory usage drops to acceptable values after unloading scenes.

and about that memory value I have one more small question.

the total system memory is the RAM, right ?

so if I develop a game that suppose to support minimum 8G RAM I need to make sure that the memory allocated will never exceed 8000 therefore select 6G or less in the Cache memory threshold ?

and by the way from which API do you get the memory allocated value ?

I can read the totalReservedMemoryRecorder.LastValue from the Unity.Profiler

but it shows different value than yours.

Thanks again for your support.

 

Link to comment
Share on other sites

Hi i imported the package you attached and i get this error while trying to create a test build :

 

Assets\Procedural Worlds\Gaia\Scripts\MultiTerrainSystem\TerrainScene.cs(490,21): error CS0103: The name 'asyncLoadOp' does not exist in the current context
 

Assets\Procedural Worlds\Gaia\Scripts\MultiTerrainSystem\TerrainScene.cs(494,25): error CS0103: The name 'asyncLoadOp' does not exist in the current context

 

Assets\Procedural Worlds\Gaia\Scripts\MultiTerrainSystem\TerrainScene.cs(498,25): error CS0103: The name 'asyncLoadOp' does not exist in the current context

 

Error building Player because scripts had compiler errors

 

Build completed with a result of 'Failed' in 12 seconds (12346 ms)

 

UnityEditor.BuildPlayerWindow+BuildMethodException: 4 errors

 

thank you

Link to comment
Share on other sites

7 minutes ago, Roi said:

Assets\Procedural Worlds\Gaia\Scripts\MultiTerrainSystem\TerrainScene.cs(490,21): error CS0103: The name 'asyncLoadOp' does not exist in the current context
 

maybe it should be : m_asyncLoadOp

instead of asyncLoadOp ?

Link to comment
Share on other sites

Hi @Roi I'm just looking at this and pretty sure it should be m_asyncLoadOp - I renamed that field when I created the patch, but the automatic renaming did not happen in parts that were ruled out due to scripting defines, and that parts only became active in a build, I will send you a corrected version in a moment. Sorry about that!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Tell a friend

    Love Canopy - Procedural Worlds? Tell a friend!
  • Need help?

    We work with some of the biggest brands in global gaming, automotive, technology, and government to create environments, games, simulations, and product launches for desktop, mobile, and VR.

    Our unique expertise and technology enable us to deliver solutions that look and run better at a fraction of the time and cost of a typical project.

    Check out some of our non-NDA work in the Gallery, and then Contact Us to accelerate your next project!

×
×
  • Create New...