- Home /
Texture2d www.texture Memory leak in coroutine?
I am getting WWW images onto some cubes from a google image search and the memory goes above 1gb after 20 pics, and when i stop Play the memory only goes back to 900mb until i restart the Unity Editor.
I have checked all the forums for solutions and have tried about 10 different solutions.
Can you find a logical reason why www.textures are being lost inside memory and i can't delete them?
var tex2d[] = new Textre2D[100];
var ZZ =0;
var foundpics = 0;
function imgcube( imgURL : String , cubename : String ){
//function called inside loop for 10 imgURL's
var loopbreak = 0;
var www = new WWW(imgurl);
yield www;
ZZ ++;
tex2d[ZZ] = www.textureNonReadable;
while( loopbreak < 10 )
{
yield WaitForFixedUpdate();
loopbreak ++;
if( www.isDone == true )
if(tex2d[ZZ].width < 256 || tex2d[ZZ].height < 256)
{break;}
else
{
foundpics +=1;
var cube = GameObject.CreatePrimitive( PrimitiveType.Cube ) ;
cube.GetComponent(Renderer).material.mainTexture = new Texture2D(4, 4, TextureFormat.DXT1, false);
cube.transform.position = Vector3(-4+foundpics%10,Mathf.Floor(foundpics/10),-20);
www.LoadImageIntoTexture(cube.GetComponent(Renderer).material.mainTexture);
//cube.renderer.material.mainTexture = tex2d[ZZ];
//Destroy(cube);
(cube.renderer.material.mainTexture);
//Destroy(tex2d[ZZ]);
tex2d[ZZ] = null;
Resources.UnloadAsset(tex2d[ZZ]);
DestroyImmediate(www.texture);
DestroyImmediate(tex2d[ZZ]);
var op : AsyncOperation = Resources.UnloadUnusedAssets();
while (!op.isDone) yield WaitForFixedUpdate();
System.GC.Collect();
www.Dispose();
Resources.UnloadUnusedAssets();
break;
}
if (loopbreak >10) break;
}
}
I checked this out on unity 4.6 and 5 for the same test, 4.6 goes up to 600$$anonymous$$B after 25 images. 5 goes well beyond 1GB and crashes, using the same code.
Answer by theylovegames · Feb 01, 2017 at 09:07 AM
Memory seems to hold steady with this.
// Use this for initialization
IEnumerator Start()
{
Texture2D oldTexture = null;
while (true)
{
WWW www = new WWW(_mUrl);
yield return www;
Texture2D newTexture = www.texture;
_mMaterialInstance.mainTexture = newTexture;
www.Dispose();
if (oldTexture)
{
DestroyImmediate(oldTexture);
}
oldTexture = newTexture;
yield return null;
}
}
I used Destroy ins$$anonymous$$d of DestroyImmediate but that fixed the issue. Was struggling on this for quite some time. Thanks for your answer!
This worked for me as well! Pretty amazing. This is actually giving me near real-time imagery from an IP camera. For my needs I added
while (true) { yield return new WaitForSeconds(delayTime); ...
just as a mechanism for reducing network bandwidth if needed.
Thanks!
Answer by Dave-Carlile · Dec 14, 2015 at 05:06 PM
From some research on similar problems, it seems that you don't want to ever reference WWW.texture since that creates a resource behind the scenes that never goes away.
http://answers.unity3d.com/questions/474421/wwwtexture-dispose-didnt-work-causing-memory-leak.html
http://answers.unity3d.com/questions/892703/wwwtexture-vs-loadimageintotexture.html
Your call to DestroyImmediate(www.texture)
is actually creating the instance it then tries to destroy.
Also, how big are your textures? They are uncompressed in memory when you load them, so for example a jpeg file might be small, but uncompressed into a full sized uncompressed image could be very large.
Thanks! I read that option and i obviously didnt implement the fix properly first time.
The memory has a totally different performance using LoadIntoTexture and not even reading from www.texture.
Cool.
So www.texture is actually redundant, as it should always be the other option and set to readable in another option if necessary.
Lots of thanks!
Your answer
Follow this Question
Related Questions
Problem streaming large movie 1 Answer
How do I add an event listener for the WWW class? 1 Answer
Coroutine needed here? 1 Answer
Download a file to project 0 Answers
Question about screenshots and facebook integration 1 Answer