• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by dzz · Mar 22, 2016 at 08:17 AM · ioswwwwww.texture

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;
 }
Comment
Add comment · Show 5
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image saschandroid · Mar 22, 2016 at 08:32 AM 0
Share

Try: make Texture2D texture a global variable, create the new texture only once (in Start()) and re-use the same texture for www.LoadImageIntoTexture.

avatar image dzz saschandroid · Mar 22, 2016 at 09:29 AM 0
Share

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.

avatar image saschandroid dzz · Mar 22, 2016 at 09:41 AM 0
Share

I don't know if this works, but can't you just set

 GetComponent<SpriteRenderer>().material.mainTexture = texture;

?

Show more comments
avatar image DeveshPandey · Jan 14, 2018 at 08:47 AM 0
Share

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

http://unitydevelopers.blogspot.in/p/blog-page_13.html

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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();
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

WWW not working to load image on iOS from google static maps. 0 Answers

can I assign a name to WWW.texture? 0 Answers

WWW in iOS does not set user agent header 1 Answer

Unity www class crashes on iOS 64 bit 2 Answers

The name 'Joystick' does not denote a valid type ('not found') 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges