Memory leak in UnityWebRequest.uploadHandler with List configured
We are developing at Unity.
The environment will be Unity 2018.4.22 f1.
I have created the following code to upload the file.
// Load file
byte[]l_file_data = File.ReadAllBytes(_path);
stringl_fileName = "hoge.mp4";
var request = new UnityWebRequest(l_url, "POST");
setRequest(request);
// Timeout Settings
request.timeout =300;
List<IMultipartFormSection>requestData=new List<IMultipartFormSection>();;
requestData.Add(new MultipartFormFileSection("file", l_file_data, l_fileName, "video/*"));
if(0<user_id)
requestData.Add(new MultipartFormDataSection("user_id", user_id.ToString()));
if (0<player_id)
requestData.Add(new MultipartFormDataSection("player_id", player_id.ToString()));
if (0<group_id)
requestData.Add(new MultipartFormDataSection("group_id", group_id.ToString()));
if (0<team_id)
requestData.Add(new MultipartFormDataSection("team_id", team_id.ToString()));
if(0<game_id)
requestData.Add(new MultipartFormDataSection("game_id", game_id.ToString()));
if(!title.Equals(""))
requestData.Add(new MultipartFormDataSection("title", System.Text.Encoding.UTF8.GetBytes(title);
if(!comment.Equals(""))
requestData.Add(new MultipartFormDataSection("comment", System.Text.Encoding.UTF8.GetBytes(comment);
byte [ ] boundary = UnityWebRequest.GenerateBoundary();
byte [ ] formSections = UnityWebRequest.SerializeFormSections (requestData, boundary);
// Data Settings
request.uploadHandler=(UploadHandler)newUploadHandlerRaw(formSections);
request.downloadHandler=(DownloadHandler) new DownloadHandlerBuffer();
// Set Header
request.SetRequestHeader("X-Requested-With", "XMLHttpRequest";
request.SetRequestHeader("Content-Type", "multipart/form-data; boundary="+System.Text.Encoding.UTF8.GetString(boundary);
request.SetRequestHeader("Authorization", "Bearer" +_access_token);
// Application name, Ver, Platform
request.SetRequestHeader("X-Escore-Env", "app="+APP_NAME+";ver="+VER+";os="+isOS();
yield return request.Send();
When I uploaded about 120MB of files with this code, there was no abnormal increase in memory on the Windows editor.
Windows editor.
When I checked on my iPad, I found that the memory leaked more than 800MB.
The file itself is 120MB, but why is the memory used so much?
Do you know how to improve this?
I am also thinking about a Unity verification, but I wonder if it can be solved by that.
Thank you for your cooperation.
ios xcode unity2d
There are a few areas where you may think you are wasting memory.
byte[] formSections = UnityWebRequest.SerializeFormSections (requestData, boundary);
This is the process of converting multipart data into data for a single message body.
To sum up, the resulting data (formSections
) should be larger than the sum of the original data, so there will be two byte arrays of 120M or more at this point.
In addition, UnityEngine.Networking.UploadHandlerRaw
, but the manual says:
This subclass copies input data to the native code memory buffer during construction, , and sends the data as it is as HTTP request body data.
To be honest with you, the UploadHandlerRaw
constructor may use the same size of buffers.
Consider using UploadHandlerFile
as a countermeasure.
The manual also says, "You can use it to send a large amount of data to the server with a low memory footprint."
However, only one file will be delayed at a time, and I don't think I can send other data together.
If that doesn't work, you have to think about not using UnityWebRequest.SerializeFormSections
or not using UnityWebRequest
in the first place.
© 2024 OneMinuteCode. All rights reserved.