Search Unity

WWW and bytes issue with iOS (memory leak)?

Discussion in 'iOS and tvOS' started by tswalk, Jun 13, 2016.

  1. tswalk

    tswalk

    Joined:
    Jul 27, 2013
    Posts:
    1,109
    I'm streaming data and using WWW to get the data... using the WWW.bytes data to FileStream to a file after it has completed downloading.

    It seems that on iOS it is having some real issues with large files that are being streamed from the server... as long as the files are small (say, under a MB or so) it seems to cleanup fine with a .Dispose() and UnloadUnusedAssets()...

    However, on larger files (say 40MB or larger).. I see in Xcode some memory warnings and even though I'm calling for release in code.. it is not happening.

    sample..

    Code (CSharp):
    1.  
    2.  
    3. request = new WWW(url, null, headers);
    4. yield return request;
    5.  
    6. if (request.bytes != null)
    7. {
    8.  byte[] file = request.bytes;
    9.  request.Dispose();
    10.  request = null;
    11.  var filePath = Path.Combine(dataPath, fileName);
    12.  FileStream output = File.Create(filePath);
    13.  output.Write(file, 0, file.Length);
    14.  output.Flush();
    15.  output.Close();
    16.  output.Dispose();
    17.  output = null;
    18.  filePath = null;
    19.  file = null;
    20. }
    21.  
    22. ...
    23. //cleanup with Resources.UnloadUnusedAssets();
    24. GC.Collect();
    25.  
    I'm currently using Unity 5.3.2p4

    is this a known bug and fixed in a later patch?

    I found this from a while ago...:

    http://answers.unity3d.com/questions/871119/creating-a-memorystream-from-wwwbytes-leaks-memory.html

    as well as:

    http://answers.unity3d.com/questions/300841/ios-download-files-and-store-to-local-drive.html
    (which was ancient)

    ???
     
  2. tswalk

    tswalk

    Joined:
    Jul 27, 2013
    Posts:
    1,109
    well.. I changed it to use just File.WriteAllBytes(filePath,request.bytes);

    with a dispose on WWW , UnloadUnusedAssets(), as well as a GC.Collect() and it still does not release... >.<
     
  3. tswalk

    tswalk

    Joined:
    Jul 27, 2013
    Posts:
    1,109
    so, anyone else that has a problem with this.. just DO NOT use WWW any longer.

    I refactored to use WebClient and now things are working great...

    and if anyone needs a reference, I found this to help with the exception handling (as I use an injected header for authorization checks): http://stackoverflow.com/questions/...ng-the-right-way-for-webclient-downloadstring

    and this lead me in the right direction:
    http://stackoverflow.com/questions/29707709/downloading-large-file-in-unity3d-using-webclient
    (Thank You Kimberly!)

    why this is still buggy after a year or more of being reported is odd to me...
     
  4. DeveshPandey

    DeveshPandey

    Joined:
    Sep 30, 2012
    Posts:
    221
    Last edited: Jan 14, 2018