Author Topic: Not releasing Atlases/Textures on Scene Change  (Read 7312 times)

mcarriere

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 25
    • View Profile
Not releasing Atlases/Textures on Scene Change
« on: September 22, 2015, 07:23:54 AM »
We're in the process of trying to optimize our memory usage, and some of the textures that are being reported in the memory profiler in 5.2 are making me wonder if I'm misunderstanding something.

We have a couple textures in our MainMenu scene that are either NGUI Atlases, or textures referenced by UITexture.

Thing is, after we switch scenes to our Game scene (which is a combination of a LoadLevelAsync for our environment, and a LoadLevelAdditiveAsync for our main game logic) some of those textures are still sticking around when we do another memory profiler sample well into that scene.

Here's a screenshot of the profiler:



The highlighted texture, DeckBuilderAtlas, is *only* ever referenced in the MainMenu. Same goes for changelog_background, whats_new_board_game, upgrade_screen_congrats, and a few others listed on there. This is adding up to a significant memory usage on our end.

Interestingly, this doesn't always seem to happen, but often enough to the point where I'm worried that I'm doing something wrong here.

For what it's worth, at the end of our loading coroutine, we are waiting on UnloadUnusedAssets before we consider ourselves done loading.

  1.                 AsyncOperation unload = Resources.UnloadUnusedAssets();
  2.                 while (!unload.isDone) {
  3.                         yield return null;
  4.                 }
  5.  

Any direction in this would be greatly appreciated!
« Last Edit: September 23, 2015, 10:00:35 AM by mcarriere »

shaunpeoples

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 6
    • View Profile
Re: Optimizing Memory Usage
« Reply #1 on: September 22, 2015, 11:48:02 AM »
What are you doing to unload assets from MainMenu that use the DeckBuilderAtlas? Simply using LoadLevelAsync has rarely seemed to work for me. I manually destroy a root
object in the loaded scene with DestroyImmediate(whateverObject, true);  //true allows for destroying of assets

If you double click on the DeckBuilderAtlas, it should take you to the object referencing the atlas.
Beyond that, it looks like you have editor items referencing it, so your reference count is probably artificially high. Haven't seen that, before.

Also, try moving your Resources.UnloadUnusedAssets() into a coroutine that lives on an object that you don't destroy between loads. Then run it to have it kick off later.

  1.   IEnumerator DeferredUnload()
  2.     {
  3.         yield return new WaitForSeconds(3); //or whatever works for you project
  4.  
  5.         Resources.UnloadUnusedAssets();
  6.         System.GC.Collect(System.GC.MaxGeneration, System.GCCollectionMode.Optimized);
  7.  
  8.         yield return null;
  9.     }
  10.  
  11.  

Additionally, not sure if you're using ARGB 32 bit for the atlas format, but you can save some memory/storage if you change to something DXT 5 (Windows).


mcarriere

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 25
    • View Profile
Re: Optimizing Memory Usage
« Reply #2 on: September 22, 2015, 12:46:59 PM »
Yes, currently the only thing that we're doing to unload those objects are changing scenes. Our UnloadUnusedAssets is on a coroutine that doesn't get destroyed between loads.

One small tidbit, it's suggested to never use Object.DestroyImmediate at runtime, ever.


shaunpeoples

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 6
    • View Profile
Re: Optimizing Memory Usage
« Reply #3 on: September 23, 2015, 07:26:40 AM »
It's not something you want to use in a performance-critical area of your code, there is an impact. But, like all things, understanding it's impact and use. Unity suggests a lot of things...

mcarriere

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 25
    • View Profile
Re: Not releasing Atlases on Scene Change
« Reply #4 on: September 23, 2015, 10:00:00 AM »
I've changed the topic to hopefully get the attention of ArenMook, and hopefully describe the problem I'm facing right now.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Not releasing Atlases/Textures on Scene Change
« Reply #5 on: September 26, 2015, 04:26:56 PM »
Use UIDrawCall.ReleaseInactive() before unloading assets. This will clear all inactive draw calls used by the UI, releasing references to textures.