Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only.

    Please, do not make any changes to your username or email addresses at id.unity.com during this transition time.

    On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live.


    Read our full announcement for more information and let us know if you have any questions.

WWW memory leak (iOS)?

Discussion in 'Editor & General Support' started by kalonw, Feb 11, 2014.

  1. kalonw

    kalonw

    Joined:
    Feb 11, 2014
    Posts:
    4
    I noticed in issue using the latest version of Unity. One of our applications is WWW heavy and it had started to crash when I upgraded it from a previous version. I noticed it was getting a lot more memory warnings and that memory use was actually increasing over time. If I had a guess, it'd be around the time that WWW on iOS was generated with the project.

    So, given code snipped below:
    Code (csharp):
    1.    
    2.     IEnumerator Start()
    3.     {
    4.         while(true)
    5.         {
    6.             Debug.Log("Start DL");
    7.             WWW www = new WWW("http://www.dhxmedia.com/images/banners/shows/banner_03.jpg");
    8.             {
    9.                 yield return www;
    10.             }
    11.             www.Dispose();
    12.             yield return new WaitForSeconds(1.0f);
    13.             Debug.Log("Finish DL");
    14.         }
    15.     }
    16.  

    - Expected behaviour: run indefinitely. Downloading the image every 1 second.
    - Actual behaviour: Runs out of memory, doesn't seem to clean itself up.

    Sample project is posted at https://github.com/dhxmedia/WWWCrash

    Allocation screenshot at https://github.com/dhxmedia/WWWCrash/blob/master/ios-4.3.4f1.png

    In addition, memory tracking on Android seems to have the expected behaviour and does not run out.
     
  2. Graham-Dunnett

    Graham-Dunnett

    Administrator

    Joined:
    Jun 2, 2009
    Posts:
    4,287
    Didn't 4.3.3 pull the WWW implementation out into the trampoline code? I suspect something different is now happening in code that you have access to.
     
  3. kalonw

    kalonw

    Joined:
    Feb 11, 2014
    Posts:
    4
    You're right, this is something that we can fix in the generated Objective-C code, and that will suffice as an interim solution. However, I must stress the importance of this bug: if it is reproducable it will likely affect a large number of developers when updating apps that use WWW. Please run the tests that I have attached. A leak in WWW is a pretty big deal.
     
  4. jcarpay

    jcarpay

    Joined:
    Aug 15, 2008
    Posts:
    561
    Pretty big deal indeed. Has this bug been confirmed yet?
     
  5. Alexey

    Alexey

    Unity Technologies

    Joined:
    May 10, 2010
    Posts:
    1,631
    4.5 will have the fix.
    Essentially you need:
    search for
    extern "C" void UnityDestroyWWWConnection(void* connection)
    in WWWConnection.mm
    [delegate.connection cancel];
    delegate.connection = nil;
    [delegate.data release]; // <-- ADD THIS
    [delegate release];
     
  6. TedHoward

    TedHoward

    Joined:
    Nov 19, 2013
    Posts:
    21
    This is likely the nail in the coffin for my company's use of Unity. Not that a bug exists. Bugs always exist. But a known massive memory leak (*) going unfixed for over 3 months. Not okay.

    ps.
    I still can't load a .wav with WWW then play it on iOS, can I? This got fixed for Android at some point.
     
  7. Ataru04

    Ataru04

    Joined:
    Apr 22, 2015
    Posts:
    5
    In Unity 5.0.1f1, the error still exists. When www ends, it doesn't release the memory in iOS and the solution from Alexey doesn't work due to iOS ARC.
     
  8. timsk

    timsk

    Joined:
    May 27, 2011
    Posts:
    177
    Issue is present in Unity 5.0.1p1 too.

    This is a pretty serious issue. Could someone from Unity chime in?

    The cleanup method is being called, and is correctly setting data to nil. I'm gonna have a prod and see if I can find what is holding the memory...
     
  9. Alexey

    Alexey

    Unity Technologies

    Joined:
    May 10, 2010
    Posts:
    1,631
    for people who refer to unity 5.x - the thread was about 4.x originally (or even ONE exact version)
    so if you insist on using this thread at least provide bug case numbers (with repro projects attached to bug, sure)
     
  10. timsk

    timsk

    Joined:
    May 27, 2011
    Posts:
    177
    For a bug that takes < 15 lines of code to reproduce, you want us to upload an entire test project?

    Also, we mentioned the version number in our posts. Same bug = same thread.

    Don't fob us off!
     
  11. Alexey

    Alexey

    Unity Technologies

    Joined:
    May 10, 2010
    Posts:
    1,631
    >> For a bug that takes < 15 lines of code to reproduce, you want us to upload an entire test project?
    I think we misunderstood each other: i am asking EXACTLY for a small project (if it is JUST 15 lines of code - i can promise to look at it even today) that reproduce the issue cleanly. But yes - we usually want the actual project (again: not YOUR project with your game, small one that you created simply to show us the issue)
    >>Also, we mentioned the version number in our posts. Same bug = same thread.
    i am fine with using whatever thread - i am just saying that initial bug was in 4.x and was fixed in 4.x. So if there are some issues with that still - that means it is a bit different - so we need to have a project with repro case (actually in original case graham poked me with case number so it wasnt explicitly stated in thread itself).
    >>Don't fob us off!
    i am not - i am just saying that without seeing repro project i cannot help you
     
  12. timsk

    timsk

    Joined:
    May 27, 2011
    Posts:
    177
    What I mean is, <15 lines is the minimum required to reproduce the bug.

    A Unity project, including those lines of code, is the absolute maximum required.

    Good support usually requires your users to do the minimum required.

    I'm sorting out a project now, give me a min.

    Literally typed this and theres a fire down our street.... gonna be a few mins lol
     
  13. Alexey

    Alexey

    Unity Technologies

    Joined:
    May 10, 2010
    Posts:
    1,631
    >>Good support usually requires your users to do the minimum required.
    too bad we switched to "unity technologies" badge instead of developer/support etc - i am developer not support - as you see actual support guys just ignore this thread (for some reason) ;-). I'm always saying that creating small repro and give case number in forum post is just a shortcut to get dev to look at it right away skipping any interim steps 8)
     
  14. timsk

    timsk

    Joined:
    May 27, 2011
    Posts:
    177
    Ok, so just spent a bit of time testing this to find the issue.

    The memory leak is actually when you use www.assetBundle

    Code (CSharp):
    1.     IEnumerator RunTest()
    2.     {
    3.         print ("Starting download...");
    4.         WWW www = new WWW("http://INSERTLARGEASSETBUNDLEURLHERE");
    5.         yield return www;
    6.  
    7.         var assetBundle = www.assetBundle;
    8.         var names = assetBundle.GetAllAssetNames();
    9.  
    10.         print ("Download finished");
    11.         www.Dispose();
    12.  
    13.         assetBundle.Unload(true);
    14.     }
    Startup: memory floating around 15MB

    Button press 1: memory goes to 23-25MB. Never comes back down to 15MB.

    All subsequent presses: Memory jumps by size of downloaded file, then reduces down to 24.6 again.

    So not as large an issue as we first thought, but we are still losing 8-10MB for no reason. I have no idea if the memory lost is tied to the size of the asset. I can't really spend more time debugging this. Especially as a fix seems out of my hands.

    Bug No.:
    695581
     
  15. Alexey

    Alexey

    Unity Technologies

    Joined:
    May 10, 2010
    Posts:
    1,631
    ok so now i see what you are describing. Though i must say that xcode shows only half of the story: if you check the actual instruments you will see that actual allocations go back to prev level (so there are no leaks), and xcode shows VirtualMemory usage and cleanup happens (now) when ios feels like it. We will check what can be done (maybe it is simply memory fragmentation or smth), but for now keep calm and carry on - if you put more "memory" pressure this "freed but not given back to OS" memory will be properly reused
     
  16. timsk

    timsk

    Joined:
    May 27, 2011
    Posts:
    177
    Your probably not wrong.

    I'm actually having some un-related memory issues with asset-bundles in my real use-case that are making it hard to properly isolate the above issue.

    Totally unrelated but creating a prefab uses very little memory, however. Loading an asset bundle from file, creating the prefab, then calling assetBundle.Unload(false) is causing S*** loads of memory to just hang around. I can even switch scenes and it's still there. I'm talking 20MB + each time. I'll probably end up creating a separate thread for this as I haven't really isolated it yet...
     
  17. mcampbell

    mcampbell

    Joined:
    Sep 16, 2013
    Posts:
    3
  18. ewenske

    ewenske

    Joined:
    Sep 2, 2014
    Posts:
    1
    I am having this issue as well. The memory steadily climbs as more www calls are made, and putting more memory pressure just crashes the app as it runs out of memory.
     
    Last edited: May 20, 2015
  19. nhimmisha

    nhimmisha

    Joined:
    Nov 26, 2013
    Posts:
    5
    So, do you guys have the solution? Any patch?
     
  20. kalonw

    kalonw

    Joined:
    Feb 11, 2014
    Posts:
    4
    What I ended up doing at the time (and still is my solution..) was to switch to BestHTTP. You get better control over requests.
     
  21. Maybe_memory

    Maybe_memory

    Joined:
    Sep 15, 2012
    Posts:
    8
    I do some test on ios. I find that when there is a network error(example: st ios to Flight mode), memory leak about 300k every request. When the network is ok, it doesn't leak some much. but the memory still keep growing up.... anyone can help?
     
  22. Devriak

    Devriak

    Joined:
    Mar 6, 2015
    Posts:
    3
    Older thread here, if someone runs into this issue though for me the fix was in WWWConnection.mm the

    -(void)cleanup{}

    call. Add

    _data = null;

    to the end of it and the data seems to be releasing memory properly on dispose. I've only looked into this a little though so if it's creating other side effects please do post them here so others and myself know.
     
    playsidepaul likes this.
  23. playsidepaul

    playsidepaul

    Joined:
    Sep 23, 2015
    Posts:
    11
    Thank you!! Been trying for the last 6 days to figure out a memory spike that was crashing low end devices (and only showing up in Xcode not Unity profiler), and this fixed it.

    (btw on recent versions of Swift it looks like you need to use
    _data = nil;
    instead of 'null')

    Thanks again for posting this :)
     
  24. the-forty-seven

    the-forty-seven

    Joined:
    Nov 12, 2014
    Posts:
    8
    This memory leaking issue is reproducing in 5.4.5p5.(Both of www and unitywebrequest have same issue) I downgrade unity to 5.4.2p3 and it has been solved.