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