Author Topic: UITexture Memory Leak  (Read 5730 times)

cheuksh

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 28
    • View Profile
UITexture Memory Leak
« on: January 08, 2014, 08:11:11 PM »
  1. UItextureGO.material.mainTexture = www.texture;
  2.                         ScrollViewGO.GetComponent<UIPanel>().Refresh();

I have a 1000 row each row have a UITexture downloaded from internet.
Of course I wont make a 1000 scroll item, instead, I only made 20, I will reposition them, reuse them, when user rolling the scroll view. so item 1 will beceom item 21...item 2 become 22, when scrolling down. It will fine.
But I find that, when I keep scrolling down the memory keep rising up..to 2.0GB and crashed unity....So I guess the uitexture replaced still inside the memory event a new www.texture is assigned...
I tried Resourced.UnloadAsset..but it seems it cannot unload a texture, it can only unload a gameobject...Since the gameobject is to be reused and cannot be destoyed.....

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UITexture Memory Leak
« Reply #1 on: January 08, 2014, 10:05:17 PM »
UItextureGO? UI Texture Game Object? That makes no sense. Do you mean UITexture.material? If so, then you should not be doing this. Setting the 'material' creates a copy of the material. You should be setting UITexture.mainTexture instead. Even if you were modifying the material at run-time, you should be modifying its dynamic copy -- UIWidget.drawCall.dynamicMaterial.

cheuksh

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 28
    • View Profile
Re: UITexture Memory Leak
« Reply #2 on: January 11, 2014, 05:52:28 AM »
  1.         IEnumerator LoadPic(UITexture uitextureComp, string filename) {
  2.  
  3.                 string url = "file://" + Application.persistentDataPath + "/tips_image/" + filename;   
  4.                 WWW www = new WWW(url);
  5.                 yield return www;
  6.                 if (www.error == null) {               
  7.                         uitextureComp.mainTexture = www.texture;
  8.                         ScrollViewGO.GetComponent<UIPanel>().Refresh();
  9.                 } else {
  10.                         Debug.Log(www.error);
  11.                 }
  12.                  StartCoroutine(LoadPic(uitextureComp,"abc.jpg"));
  13.  
  14.  
  15.         }

 



That is UITexture Component..sorry for my bad naming. But even I do not alter the material but just the mainTexture like the above code, every pic memory and texture count still cumulative.
 To reproduce, keep assigning photo to the same gameObject's uitextureComp.mainTexture, loop for 100 times, look at the memory usage.

Once again I cannot destroy the gameObject and create a new one when user scrolling. the gameOjbect need to be recycled. So I cannot use destroy immediate method..
« Last Edit: January 11, 2014, 05:58:26 AM by cheuksh »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UITexture Memory Leak
« Reply #3 on: January 11, 2014, 06:11:52 PM »
I don't see you unloading the texture anywhere in your code. I don't know what Unity does when you access www.texture. For all I know, it makes a copy every time. You need to unload all dynamically loaded resources, and it's always up to the developer to do so.

In case of NGUI, you need to delay the Unload call until the end of frame -- so yield WaitForEndOfFrame and only then UnloadUnusedAssets().

cheuksh

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 28
    • View Profile
Re: UITexture Memory Leak
« Reply #4 on: January 14, 2014, 12:04:21 PM »
Thx for letting me know it is a common problem of unity texture. It is fixed with the following code:
  1. DestroyImmediate(uitextureComp.mainTexture,true);
Just find out from google that texture will not be dumped by GC even not more reference and must destroy it mannually.