- Home /
Memory leak with WWW on iOS
Hello,
I have an image sensor hooked to a local server that takes roughly 10 images per second. With my Unity iOS app, I'm trying to display that camera feed.
It works well when I run the Unity app on my computer but on the iOS app, although the image is displayed and updated, the memory goes high very fast until it stops my app.
When I analyse the app memory allocation with Instruments "GFX Texture Level" and "Malloc 228.0 KB" are getting high very fast.
I saw that other users had similar issue with the WWW class on iOS not releasing correctly objects... I wanted to know if that still the case with the current Unity version (5.3.2)? If yes how could I fix this?
Here is my code:
void Update () {
StartCoroutine (DownloadImage ());
}
IEnumerator DownloadImage(){
WWW www = new WWW("http://192.168.2.188/tele/image.jpg");
yield return www;
if (string.IsNullOrEmpty(www.error)) {
Texture2D texture = new Texture2D (www.texture.width, www.texture.height, TextureFormat.PVRTC_RGB2, false);
www.LoadImageIntoTexture (texture as Texture2D);
GetComponent<SpriteRenderer> ().sprite = Sprite.Create (texture, new Rect (0, 0, www.texture.width, www.texture.height), Vector2.zero);
texture = null;
}
www.Dispose ();
www = null;
}
Try: make Texture2D texture
a global variable, create the new texture only once (in Start()
) and re-use the same texture
for www.LoadImageIntoTexture
.
Thanks for your answer! That was indeed one of the issue, now the memory still get higher but slower. However the issue is still there.
I did the same for the Sprite (put it as a general var).
I could isolate the problem:
www.LoadImageIntoTexture (texture as Texture2D);
sprite = Sprite.Create(texture, new Rect (0, 0, www.texture.width, www.texture.height), Vector2.zero);
GetComponent ().sprite = sprite;
If I comment the line with "Sprite.Create" then I do not have a memory leak anymore. Not sure how Unity iOS manages the memory.
I don't know if this works, but can't you just set
GetComponent<SpriteRenderer>().material.mainTexture = texture;
?
Here is the great plugin to download any small/big files without memory leak.
https://www.assetstore.unity3d.com/#!/content/92128?aid=1101l34jr
For more details, visit this blog
Answer by dzz · Mar 22, 2016 at 02:07 PM
Finally made it worked by adding Texture2D as a general variable as suggested by @saschandroid .
Also have to add Resources.UnloadUnusedAssets() at the end and the memory stay stable!
Final code, in case someone has the same issue:
public bool displayLive = false;
private Texture2D texture;
// Use this for initialization
void Start () {
texture = new Texture2D (1,1);
}
// Update is called once per frame
void Update () {
if(displayLive)
StartCoroutine (DownloadImage ());
}
IEnumerator DownloadImage(){
WWW www = new WWW("http://192.168.2.188/tele/image.jpg");
yield return www;
//Ignore the texture if it isn't at the right size
//(Download issue)
if (string.IsNullOrEmpty(www.error)) {
www.LoadImageIntoTexture (texture as Texture2D);
GetComponent<SpriteRenderer>().sprite = Sprite.Create (texture, new Rect (0, 0, www.texture.width, www.texture.height), Vector2.zero);
DestroyImmediate(www.texture);
}
www.Dispose ();
Resources.UnloadUnusedAssets();
}
Your answer
Follow this Question
Related Questions
WWW not working to load image on iOS from google static maps. 0 Answers
iOS ignores html error headers? 0 Answers
unity3d www download of large texture mobile 0 Answers
can I assign a name to WWW.texture? 0 Answers