Search Unity

How to free Memory Mono after WriteAllBytes?

Discussion in 'Scripting' started by samjoly, May 1, 2016.

  1. samjoly

    samjoly

    Joined:
    Apr 23, 2013
    Posts:
    30
    Hi team,
    On android, I'm downloading and saving several videos, unfortunately sometime (it's not consistent) the Memory Mono, start to grow and doesn't get free, even after:
    www.Dispose();
    www = null;
    Resources.UnloadUnusedAssets();
    GC.Collect();


    I'm removing the previous video file and transition to an empty scene before loading a new video, but it doesn't help the Memory Mono, so after a couple of videos loaded, I still run out of memory.

    Any idea? Could it be a limitation on how much data can it be written per session?
     
    Last edited: May 1, 2016
  2. tonemcbride

    tonemcbride

    Joined:
    Sep 7, 2010
    Posts:
    1,089
    Not sure if this will work but you could try using the 'using' functionality of C# which automatically calls the Dispose functionality on your objects. It also keeps it within local scope.

    http://www.dotnetperls.com/using
     
  3. samjoly

    samjoly

    Joined:
    Apr 23, 2013
    Posts:
    30
    Thanks for the idea. I end up using WebClient.
     
  4. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,532
  5. samjoly

    samjoly

    Joined:
    Apr 23, 2013
    Posts:
    30
    Interesting. I haven't get yet, how to implement it, and what would be the advantage.

    Currently I'm downloading using:

    Code (CSharp):
    1. client = new WebClient();
    2. client.DownloadFileCompleted += DownloadFileCompletedHandler;
    3. client.DownloadFileAsync(new Uri(strURL), write_path);
    Then call my video player with:
    Code (CSharp):
    1.  player.Load("file://" + write_path);
    And delete the file when needed:
    Code (CSharp):
    1.   System.IO.File.Delete(write_path);
    It seems to work fine. But of course I'm curious about your "system.io.stream" idea, so please feel free to guide me a bit more, and I'll be happy to do some test.
    Thanks a lot!
     
  6. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,532
    Well WebClient.DownloadFileAsync(string,string) that you call is streaming the file to the file location you supply. So the memory overhead is not an issue there. It works like a stream would.

    File.Delete is not memory intensive at all, so it's of no concern.

    I don't know what the 'player.Load' actually is, I have no idea what type 'player' is, so I couldn't say anything about it.

    But as I said in my previous post, WebClient is a viable option because it tends to work with streams for memory conservance.
     
  7. samjoly

    samjoly

    Joined:
    Apr 23, 2013
    Posts:
    30
    Thanks Lordofduct for sharing your impressive expertise. It's good to know that I'm on the right path, and will do some research on my end to have a better understanding on how the webclient and stream work.
    For clarification, the 'player' is just a video player that load the file from the persistentDataPath - so all good here.


    To extend the topic a bit, I'm wondering if it would be possible to have the video downloading to the device, even if the app is closed, like they do in some video app such as Vrse or MilkVr.

    Thanks again!