Search Unity

Assetbundle can't unload clear

Discussion in 'Editor & General Support' started by Dream_Iverson, Sep 22, 2012.

  1. Dream_Iverson

    Dream_Iverson

    Joined:
    Sep 22, 2012
    Posts:
    10
    i have load an assetbundle via www,but the memory can't falldown as low as before .
    before i load the assetbundle the memory of the webplayer is 49MB, as i load the assetbundle the memory is 93MB, after i unload the assetbundle the memory is still 84MB.
    the file of the assetbundle is 2.5MB.
    as i have a lot of assetbundle like this,it will cause memory leak...
    :confused:

    Code (csharp):
    1.  
    2.  
    3. int stat = 0;
    4. WWW mWWW;
    5. GameObject obj;
    6.  
    7. void Update()
    8. {
    9.       switch(state)
    10.      {
    11.          case 0:
    12.             if(Input.GetKeyUp(KeyCode.A))
    13.             {
    14.                 mWWW = new WWW("http://10.10.1.120/assetbudle/Scene01.unity3d");
    15.                 state=1;
    16.            }
    17.           break;
    18.  
    19.          case 1:
    20.                    if(mWWW.isDone)
    21.                      {
    22.                            stat=2;
    23.                             obj = (GameObject)GameObject.Instantiate(mWWW.assetBundle.mainAsset);
    24.                      }
    25.          break;
    26.  
    27.          case 2:
    28.                     if(Input.GetKeyUp(KeyCode.B))
    29.                    {
    30.                          Object.Destroy(obj);
    31.                           mWWW.assetBundle.Unload(true);
    32.                           mWWW.Dispose();
    33.                           mWWW = null;
    34.                    }
    35.          break;
    36.      }
    37. }
    38.  
     
  2. Dream_Iverson

    Dream_Iverson

    Joined:
    Sep 22, 2012
    Posts:
    10
    ? nobody else get something like this? Am I doing something wrong?
     
  3. horsman

    horsman

    Joined:
    Jul 19, 2008
    Posts:
    156
    The first thing here is that the size of the bundle has very little relation to the size of the assets in memory after loading the bundle unless the bundle was created with the Uncompressed build option. In other words, your bundle could expand from 2.5 MB disk space to 45MB RAM if the contents have many self-similar parts.

    Example: An all black texture in 32bit mode of size 20 MB could be compressed as small as 100kb on disk.

    Consider going through each of the assets in the bundle and looking at their size in the inspector: What do they total to? Are any of them non-power-of-two images? Write this down.

    However, that's not all of your problem: Unity doesn't always return all of the memory to the system (It shouldn't either, peak memory usage is what you need to worry about, not moment to moment or average). So when you free the bundle and dispose the www object and unload the bundle Unity holds onto that memory and uses it to load the next asset bundle. You can test this by loading and freeing a bundle many times in succession and seeing that the total memory use never passes the first time if everything else is right. I have experimentally verified this.

    So then even when this is taken into account we still have a problem. I made some changes to your code above to fix these:
    So what happened here? Two things:
    1. each time WWW.assetBundle is accessed a copy is made, resulting in another copy of the asset memory being used. Multiple copies in memory at once means a higher memory cap in your program.
    2. Destroy WWW after you first copy the assetBundle data to a reference, Otherwise you have three copies of the data in memory at once (the www bytes, the bundle bytes, the assets themselves when you take em out)

    Well that should do it! Remember to keep your asset bundle files small so that you don't artificially increase your memory cap!
     
    Last edited: Sep 25, 2012