Search Unity

Resources.UnloadUnusedAssets vs. GC.Collect

Discussion in 'Editor & General Support' started by NightmareTFD, Oct 1, 2015.

  1. NightmareTFD

    NightmareTFD

    Joined:
    Feb 11, 2015
    Posts:
    39
    Right now in my game i have set up my UI as prefabs that i user Resources.Load() on and instance as i need them.

    when the user is no longer using that UI i would like to release those resources back.
    Can i simply call GC.Collect() in the destroy of my script for my UI?
    should i call Resources.UnloadUnusedAssets() in the destroy of my script for my UI?
    should i do both?

    any suggestions would be greatly appreciated.
     
  2. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,504
    Thesexare not the same thing.

    GC.Collect instructs the Mono garbage collector to perform a collection. This will look for objects and will clean up those that have nothing referencing them (note that this is a Mono feature that knows nothing about the existence of Unity. The objects that are collected are Mono heap objects, and not Unity game objects).

    Resources.UnloadUnusedAssets is Unity's API for looking up any unneeded Unity objects that are currently loaded, and unload them to free up memory. Maybe this calls GC.Collect internally, who knows.

    This is run automatically by Unity when loading new scenes. Not sure about the case where you dynamically load and destroy objects from Resources... (but it's always good to profile and see the results).
     
    Dan2013 likes this.
  3. sloopidoopi

    sloopidoopi

    Joined:
    Jan 2, 2010
    Posts:
    244
    Do both. :D
    From my experience it is important to call Resources.UnloadUnusedAssets() . Only calling GC.Collect() for example will not destroy a Texture2D even if it is no longer referenced.
     
    Agent0023 and guneyozsan like this.
  4. Tomas1856

    Tomas1856

    Unity Technologies

    Joined:
    Sep 21, 2012
    Posts:
    2,716
    liortal's guess is correct, Resources.UnloadUnusedAssets is indeed calling GC.Collect inside. So if you already calling Resources.UnloadUnusedAssets, you shouldn't call GC.Collect
     
  5. NightmareTFD

    NightmareTFD

    Joined:
    Feb 11, 2015
    Posts:
    39
    Thank you very much everyone.
     
    Agent0023 likes this.
  6. sevensails

    sevensails

    Joined:
    Aug 22, 2013
    Posts:
    483
    I have a loading screen, and I don't want to it be unloaded when loading a new scene, so I can switch back to loading screen very fast when needed. Is this possible?
     
  7. Pimpace

    Pimpace

    Joined:
    Sep 21, 2017
    Posts:
    41
    Maybe you can use DontDestroyOnLoad on your object like:
    Code (CSharp):
    1.     private void Awake()
    2.     {
    3.         if (_instance == null)
    4.         {
    5.             _instance = Instance;
    6.             DontDestroyOnLoad(gameObject);
    7.         }
    8.         else
    9.         {
    10.             Destroy(gameObject); // prevent duplicates
    11.         }
    12.  
    13.     }
    ...or check THIS out.
     
unityunity