Search Unity

Meshes are not released from memory

Discussion in 'Windows' started by Raimis, Feb 17, 2015.

  1. Raimis

    Raimis

    Joined:
    Aug 27, 2014
    Posts:
    160
    Hi, we are working on the game that we are willing to publish to windows phone market. We are facing a problem where Resources.UnloadUnusedAssets() doesn't release memory occupied by meshes to the point where app breaks. We have tested on standalone and wp8 builds - on both builds textures unload just fine; meshes are unloaded on standalone, but not in wp8.

    What is even more interesting is how things work... We have object A and object B. On standalone if we show object A we have 1 ref and 2 copy in memory. If we switch to obj B, obj A is disposed and we have 1 ref and 2 copies to obj B.

    Now on wp8 after loading object A, we have 2 references and 2 copies. By switching to obj B, we still have 1 reference and 2 copies of object A and 2 references and 2 copies of object B. Now the twist - if app is sent to background and returned - 1 copy of object A is disposed, however one copy and one reference remains.

    Any hints would be appreciated. Is it a know bug (forum search results in nothing)? Is there a workaround for it? We are using unity 4.6.2p1.

    EDIT: We have tested on Android and it works like on standalone so the problem is with wp8 build and the way resources are handled.
     
    Last edited: Feb 17, 2015
  2. Tomas1856

    Tomas1856

    Unity Technologies

    Joined:
    Sep 21, 2012
    Posts:
    1,797
    It's hard to understand, what exactly is the problem, what is "an object" in your case? And how are meshes referenced? Are they referenced by game objects or scripts?

    As for Rresouces.UnloadUnusedAssets, the behavior is slightly different, because under the hood, on WP8 and Windows Store Apps different .NET framework is used, while on Standalone and Android it's Mono. But I haven't heard about any leaks on WP8. So unless you're keeping a reference somewhere to objects, it's not expected memory leak.
     
  3. Raimis

    Raimis

    Joined:
    Aug 27, 2014
    Posts:
    160
    Sorry for not making myself clear. I was referring to reference count provided by profiler after taking a memory spanshot. Object A and B are prefabs with geometry and textures. These are loaded at runtime (Resources.Load<>()) and later, after removing all the references from our own code, should be unloaded using Resources.UnloadUnusedAssets call. This apparently works well on both android and standalone but not on wp8. How many copies are stored in memory, we assume by looking at memory graph in profiler at different time moments. By saying "switching to obj b" I mean that we display these objects in gallery one at a time. After switching to object B, all references to object A are removed from our code and UnloadUnusedAssets is called. I hope I explained it better this time :)
     
  4. Raimis

    Raimis

    Joined:
    Aug 27, 2014
    Posts:
    160
    Anyone?
     
  5. Tomas1856

    Tomas1856

    Unity Technologies

    Joined:
    Sep 21, 2012
    Posts:
    1,797
  6. Raimis

    Raimis

    Joined:
    Aug 27, 2014
    Posts:
    160
    I tried to reach you via conversation, but that doesn't seem to work well. Since we are using resources to load prefabs, the UnloadAsset doesn't work out of the box. We are unable to find how to use this method to unload mesh within the prefab, because that seems to be what should be passed as a parameter to this method in this particular case. Any guidance would be appreciated.
     
  7. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    6,188
    Hi,

    you're describing it as if it's reproducible at a very small scale too - if that's the case, could you put together a sample illustrating differences between standalone and windows phone so we could look into it?
     
  8. Raimis

    Raimis

    Joined:
    Aug 27, 2014
    Posts:
    160
    While creating a repro project I was able to solve our issue, but still there's a bug with releasing loaded resources.

    This code doesn't leak:
    var car = (Instantiate(Resources.Load<GameObject>("Cars/Bodies/" + name)) as GameObject).GetComponent<VisualCar>();
    Destroy(car.gameObject);

    This code leaks memory (but only on wp8, on standalone and android this works just like code above):
    var car = Instantiate(Resources.Load<VisualCar>("Cars/Bodies/" + name)) as VisualCar;
    Destroy(car.gameObject);

    Basically if we load resource as GameObject and get it's particular component - resource is unloaded successfully, but if we attempt to load object as particular component - the loaded object isn't garbage collected. Hope this helps!
     
  9. rageingnonsense

    rageingnonsense

    Joined:
    Dec 3, 2014
    Posts:
    99
    I have this same issue within the editor. Generated meshes are never freed from memory after they are created, even if they have no references:



    This image shows a small slice of around 7000 meshes that have been accumulated. All gameobjects referencing them have been destroyed.

    The majority of these meshes are created via instantiating from a single prefab that is cached in another script (not statically). all gameobjects referencing these scripts have been destroyed, but the meshes (and materials) memory is never reclaimed.
     
  10. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    6,188
    Can we get a bug report on that?
     
  11. rageingnonsense

    rageingnonsense

    Joined:
    Dec 3, 2014
    Posts:
    99
    I supplied one a few days ago via the unity bug reported and a sample project that reliably reproduces it.

    for mine, is is extremely specific; the issue is due to SkinnedMeshRenderer.BakeMesh() holding onto the the passed Mesh object. you have to explicitly destroy the object yourself or else it gets lost.

    bug report: (Case 739676)
     
    holliebuckets likes this.
  12. MoribitoMT

    MoribitoMT

    Joined:
    Jun 1, 2013
    Posts:
    293
    Is this mesh memory leak any sokution?