Search Unity

Unused Textures do not trigger Garbage Collector

Discussion in 'General Graphics' started by NerdyOrc, Jun 14, 2017.

  1. NerdyOrc

    NerdyOrc

    Joined:
    Aug 6, 2013
    Posts:
    17
    Hi, I am leaving this post here both as a request for clarification, and in case someone may be having a similar issue, My game was crashing due to memory leak (partial logs below). In my game was constantly creating a temp Textures2D assuming that the GC would collect them, it wouldn't, which wound up crashing due to lack of memory.

    I solved it by simply storing a Texture and resizing it as necessary. My question here is why does the GC not collect it?




     
    Last edited: Jun 15, 2017
  2. samizzo

    samizzo

    Joined:
    Sep 7, 2011
    Posts:
    487
    Textures also allocate memory outside of the managed Mono heap, and to free that memory you need to call one of the destroy functions (e.g. Object.Destroy(myTexture) or Object.DestroyImmediate(myTexture)) or call Resources.UnloadUnusedAssets() (this is slow though).
     
  3. ina

    ina

    Joined:
    Nov 15, 2010
    Posts:
    1,085
    is this still true?
     
  4. TJHeuvel-net

    TJHeuvel-net

    Joined:
    Jul 31, 2012
    Posts:
    838
    Yes by design. See @samizzo s response.