WWW

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:

using System;
using System.Collections;
using System.IO;
using UnityEngine;
using UnityEngine.Networking;

public class Upload_Video : MonoBehaviour {
      public static Texture2D qrcode;
      public static Action OnCancel = null;
      public static Action OnComplete = null;
      public static IEnumerator run (string file_full_path = null) {
            if (file_full_path == null || !File.Exists (file_full_path) || !file_full_path.ToLower ().EndsWith ("mp4")) {
                  Debug.Log (" Invalid File Path \nPath\t<" + file_full_path + ">");
                  OnCancel.Invoke ();
                  yield break;
            }
//     load video
            WWW wwwReadFile = new WWW ("file://" + file_full_path);
            yield return wwwReadFile;
            if (wwwReadFile.error != null) {
                  Debug.Log (" Read File error \nError\t:\t" + wwwReadFile.error);
                  OnCancel.Invoke ();
                  yield break;
            } else {
                  Debug.Log (" File Readed \nFile Size\t:\t" + wwwReadFile.bytes.Length);
            }

            string url_Upload = "192.168.16.161/_test/upload-video/index.php",
                  //       url_Watch = "192.168.16.161/_test/upload-video/videos/",
                  filename = file_full_path.Substring (1 + Mathf.Max (file_full_path.LastIndexOf ("\\"), file_full_path.LastIndexOf ("/")));

//     Create Form
            WWWForm _wwwForm = new WWWForm ();
            _wwwForm.AddBinaryData ("upload_video", wwwReadFile.bytes, filename, "video/mp4");
            _wwwForm.AddField ("Field 1", "Field 1 data");
            _wwwForm.AddField ("Field 2", "Field 2 data");
            _wwwForm.AddField ("Field 3", "Field 3 data");

//    Upload
            UnityWebRequest _UnityWebRequest = UnityWebRequest.Post (url_Upload, _wwwForm);

            yield return _UnityWebRequest.Send ();

            if (_UnityWebRequest.isError) {
                  Debug.Log (" Upload File error \nError\t:\t" + _UnityWebRequest.error);
                  OnCancel.Invoke ();
            } else {
                  Debug.Log (" Upload Complete \n" + _UnityWebRequest.downloadHandler.text);
                  OnComplete.Invoke ();
            }

//    release memory
            Debug.Log ("Caching.CleanCache() " + Caching.CleanCache ());;

            _wwwForm = null;
            wwwReadFile.Dispose ();
            _UnityWebRequest.Dispose ();

            yield return null;

            wwwReadFile = null;
            _UnityWebRequest = null;
            GC.Collect ();
            Resources.UnloadUnusedAssets ();
      }
}

Test record=> (Full.mp4 - Google Drive)
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

1 Like

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?

In Task Manager. Check the video on google drive.

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.

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.
4682516--441194--memory final.PNG