Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Work with memory: load and unload prefabs

Discussion in 'iOS and tvOS' started by Padonakkk, Jun 6, 2014.

  1. Padonakkk

    Padonakkk

    Joined:
    Nov 10, 2013
    Posts:
    2
    Hi all!
    I'm really need help!
    I can't understand how to work with prefabs and unloading.

    I have the next situation.

    I run my application on IPad from Xcode.
    Unity version 4.3.1 Pro.
    Watch memory using with:
    Xcode Debug Navigator
    Xcode Instruments
    Unity Profiler

    I do next steps:

    0) Unity starts from Empty scene.
    Used memory XCODE Debug Navigator: 11 mb Instruments: 26.4 mb Unity Profiler: used total 13.2 reserved total 13.7
    1) Load prefab from Resources folder prefab = Resources.Load("Room1");
    Used memory XCODE Debug Navigator: 30.8 mb Instruments: 49.54 mb Unity Profiler: used total 19.8 reserved total 20.4
    2) Instantiate prefab
    go = (GameObject)Instantiate(prefab);
    go.name = "Room1";
    XCODE Debug Navigator: 31.2 mb Instruments: 50 mb Unity Profiler: used total 20 reserved total 20.7
    3) Destroy all objects on scene - memory not changed
    Transform[] tr = FindObjectsOfType<Transform>();
    for (int i = 0; i < tr.Length; i++)
    {
    GameObject goo = tr.gameObject;
    if (goo.name != "Main Camera")
    {Destroy(goo);
    goo = null;}
    }
    XCODE Debug Navigator: 31.1 mb Instruments: 49.93 mb Unity Profiler: used total 19.8 reserved total 20.4
    4) Resources.UnloadUnusedAssets(); - memory not changed
    5) System.GC.Collect(); - memory not changed
    6) Call UnloadAsset of prefab Resources.UnloadAsset(prefab); - memory not changed
    7) Call Resources.UnloadUnusedAssets(); - memory partially cleaned
    XCODE Debug Navigator: 21.9 mb Instruments: 40.79 mb Unity Profiler: used total 13.2 reserved total 13.9
    I profiler i see, that deleted all texures, that used in prefab
    8) System.GC.Collect(); - memory not changed
    9) Load empty scene - memory not changed

    Here is another interesting moment:

    When application goes to background and i start another application, size of used RAM greatly decreases, and when i call unity app - memory goes to first size with empty scene.

    I have next questions:

    1) Why memory not cleaned fully after delete prefab and call UnloadUnusedAssets - we can see it in Instruments and Xcode - but in Profiler we see, that memory practically fully free?
    2) It's real to clean memory to initial size?
    3) Do i all steps right or i do something wrong?

    You can download test project here:
    http://gfile.ru/aa5on

    Big thanks for your replies.
     
  2. toto007

    toto007

    Joined:
    Jul 18, 2014
    Posts:
    33
    I am having the exact same doubt .... can anyone give more explanation please?
     
  3. unimechanic

    unimechanic

    Joined:
    Jan 9, 2013
    Posts:
    155
    A few things:

    In order to unload unused assets call Resources.UnloadUnusedAssets:

    http://docs.unity3d.com/Documentation/ScriptReference/Resources.UnloadUnusedAssets.html

    This will examine the game object hierarchy and remove any unused assets from memory.

    The Garbage Collector will keep memory allocated until it decides releasing it to the system, even if you call System.GC.Collect(), Mono will do it only when it considers necessary.

    And you can use DestroyImmediate to remove instances in the same frame:

    http://docs.unity3d.com/Documentation/ScriptReference/Object.DestroyImmediate.html

    Now, you should use the built-in profiler to track the mono-memory section:

    http://docs.unity3d.com/Manual/iphone-InternalProfiler.html

    And I have found cases in which XCode throws strange measures. VM Tracker "dirty memory" and ActivityMonitor "Real memory" are the most reliable counters.
     
  4. yuewahchan

    yuewahchan

    Joined:
    Jul 2, 2012
    Posts:
    309
    I have Unity Pro, when using Profiler, and take the sample for memory, the Assets group shows the loaded asset for (GameObject)Instantiate(Resources.Load("Prefab")).

    Texture2D, Mesh, Material, Shader can be unloaded using Resources.UnloadAsset.
    Transform, GameObject, MeshRenderer, MeshFilter and BoxCollider can not be unloaded.

    I don't want to use Resources.UnloadUnusedAssets because it is very slow for a big scene.