Search Unity

  1. unity_EDdq8x7gw4afZg

    unity_EDdq8x7gw4afZg

    Joined:
    Apr 30, 2019
    Posts:
    4
    Hi everyone,
    I recently used WWW, UnityWebRequest and WWWForm to upload a video from local to server. Everything worked fine except after upload the file, memory was not released.

    Code:
    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.IO;
    4. using UnityEngine;
    5. using UnityEngine.Networking;
    6.  
    7. public class Upload_Video : MonoBehaviour {
    8.       public static Texture2D qrcode;
    9.       public static Action OnCancel = null;
    10.       public static Action OnComplete = null;
    11.       public static IEnumerator run (string file_full_path = null) {
    12.             if (file_full_path == null || !File.Exists (file_full_path) || !file_full_path.ToLower ().EndsWith ("mp4")) {
    13.                   Debug.Log (" Invalid File Path \nPath\t<" + file_full_path + ">");
    14.                   OnCancel.Invoke ();
    15.                   yield break;
    16.             }
    17. //     load video
    18.             WWW wwwReadFile = new WWW ("file://" + file_full_path);
    19.             yield return wwwReadFile;
    20.             if (wwwReadFile.error != null) {
    21.                   Debug.Log (" Read File error \nError\t:\t" + wwwReadFile.error);
    22.                   OnCancel.Invoke ();
    23.                   yield break;
    24.             } else {
    25.                   Debug.Log (" File Readed \nFile Size\t:\t" + wwwReadFile.bytes.Length);
    26.             }
    27.  
    28.             string url_Upload = "192.168.16.161/_test/upload-video/index.php",
    29.                   //       url_Watch = "192.168.16.161/_test/upload-video/videos/",
    30.                   filename = file_full_path.Substring (1 + Mathf.Max (file_full_path.LastIndexOf ("\\"), file_full_path.LastIndexOf ("/")));
    31.  
    32. //     Create Form
    33.             WWWForm _wwwForm = new WWWForm ();
    34.             _wwwForm.AddBinaryData ("upload_video", wwwReadFile.bytes, filename, "video/mp4");
    35.             _wwwForm.AddField ("Field 1", "Field 1 data");
    36.             _wwwForm.AddField ("Field 2", "Field 2 data");
    37.             _wwwForm.AddField ("Field 3", "Field 3 data");
    38.  
    39. //    Upload
    40.             UnityWebRequest _UnityWebRequest = UnityWebRequest.Post (url_Upload, _wwwForm);
    41.  
    42.             yield return _UnityWebRequest.Send ();
    43.  
    44.             if (_UnityWebRequest.isError) {
    45.                   Debug.Log (" Upload File error \nError\t:\t" + _UnityWebRequest.error);
    46.                   OnCancel.Invoke ();
    47.             } else {
    48.                   Debug.Log (" Upload Complete \n" + _UnityWebRequest.downloadHandler.text);
    49.                   OnComplete.Invoke ();
    50.             }
    51.  
    52. //    release memory
    53.             Debug.Log ("Caching.CleanCache() " + Caching.CleanCache ());;
    54.  
    55.             _wwwForm = null;
    56.             wwwReadFile.Dispose ();
    57.             _UnityWebRequest.Dispose ();
    58.  
    59.             yield return null;
    60.  
    61.             wwwReadFile = null;
    62.             _UnityWebRequest = null;
    63.             GC.Collect ();
    64.             Resources.UnloadUnusedAssets ();
    65.       }
    66. }
    Test record=> (https://drive.google.com/open?id=1-Fx_nJmKnZ7-ovf-Bnn4R4KjxJpDbhdn)
    In this record, I upload the same video several times (the video size is 252MB) , it use 10 times more memory (300,000KB to >4,000,000KB). Also, after upload the video, the memory will not be released even stop playing.

    Devices:
    Windows 7 64bit
    Unity Editor 5.6.6f2

    I have tried to fix this by:
    1.) Dispose the WWW and UnityWebRequest at the end of coroutine
    2.) Calling GC.Collect() and Resources.UnloadUnusedAssets() at the end of coroutine

    All of the above attempt did not work!
    Did anyone meet this problem? Please show me how to fix it, I really appreciate
     
    MD_Reptile likes this.
  2. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,732
    Try doing garbage collection in a different coroutine, started at the end of this one and yield return null a couple times before collecting. In general your code looks fine and it should release the memory, at least most of it.
    BTW, how do you measure memory usage? Unity profiler?
     
  3. unity_EDdq8x7gw4afZg

    unity_EDdq8x7gw4afZg

    Joined:
    Apr 30, 2019
    Posts:
    4
    In Task Manager. Check the video on google drive.
     
  4. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,732
    That's not a good way to measure memory. GC.Collect() and others make the memory unused and available for new allocations, but not necessarily return the memory back to OS. You should be using Unity profiler instead.
     
  5. unity_EDdq8x7gw4afZg

    unity_EDdq8x7gw4afZg

    Joined:
    Apr 30, 2019
    Posts:
    4
    Hello,
    After call "Resources.UnloadUnusedAssets ();" only "Used Total" down to 0.87GB but "Reserved Total" show it holding not of memory.

    Moreover, if I player keep uploading video, "OutOfMemoryException: Out of memory" will happen.
    memory final.PNG