- Home /
Downloading big files on iOS ... WWW will give "out of memory" error.
Hello Folks,
i'm currently working on an unity based iOS project. so far i've overcome many problems (event handling for overlapping buttons, overlapping scrollviews, etc. etc.) ... Now there's another nice problem to be solved:
i'm doing a sync between offline SQlite and online mySQL database (working nicely), as well as downloading many, many files. some of them are videos with big file sizes, for example. those files are too big to be kept in memory completely. so even when i'm happily calling WWW.dispose after completing a filedownload (and the memory consumption is going down again, at least according to System.GC.GetTotalMemory(false), ... there is obviously always a point where i'm running into "out of memory" error because the file to be downloaded is bigger than the available memory.
How would you guys implement a file download which is opening a connection, downloading a few kilobytes (and immediately append to the downloaded file, until it's completed). I was checking out some c# stream based download functions, some of them were using System.Uri which was not available in unity (for both subset and full 2.0 player settings) ...
Any advice ?
You would need to consider doing something directly with System.NET - check out this
Yep this is what i've done now. - Works! Using sockets to create a HTTP request and a BinaryReader to save buffer-by-buffer to disk. - Prototype is working.
Answer by franky303 · Sep 25, 2012 at 05:39 PM
// Based on this:
// http://stackoverflow.com/questions/4768443/downloading-file-via-tcpclient-from-http-server
public class main : MonoBehaviour
{
string host = "www.server.com";
string uri = "/path/to/file/1920x1080.mp4";
uint contentLength;
int n = 0;
int read = 0;
NetworkStream networkStream;
FileStream fileStream;
Socket client;
// Use this for initialization
void Start ()
{
string query = "GET " + uri.Replace(" ", "%20") + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"User-Agent: undefined\r\n" +
"Connection: close\r\n"+
"\r\n";
Debug.Log (query);
client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
client.Connect(host, 80);
networkStream = new NetworkStream(client);
var bytes = Encoding.Default.GetBytes(query);
networkStream.Write(bytes, 0, bytes.Length);
var bReader = new BinaryReader(networkStream, Encoding.Default);
string response = "";
string line;
char c;
do
{
line = "";
c = '\u0000';
while (true)
{
c = bReader.ReadChar();
if (c == '\r')
break;
line += c;
}
c = bReader.ReadChar();
response += line + "\r\n";
}
while (line.Length > 0);
Debug.Log ( response );
Regex reContentLength = new Regex(@"(?<=Content-Length:\s)\d+", RegexOptions.IgnoreCase);
contentLength = uint.Parse(reContentLength.Match(response).Value);
fileStream = new FileStream( Application.persistentDataPath + "/download.mp4", FileMode.Create);
}
void Update()
{
byte[] buffer = new byte[4 * 1024];
if (n < contentLength)
{
if (networkStream.DataAvailable)
{
read = networkStream.Read(buffer, 0, buffer.Length);
n += read;
fileStream.Write(buffer, 0, read);
}
Debug.Log ( "Downloaded: " + n + " of " + contentLength + " bytes ..." );
}
else
{
fileStream.Flush();
fileStream.Close();
client.Close();
}
}
}
Tried to implement this - and it does work... if you're not using a proxy. Currently Im searching for a solution that will work if a proxy server is set
Using this script (which is great btw)... what happens if the datastream is interrupted?
During the statement if (n < contentLength) there doesn't appear to be a check to see if the the stream stops before contentLength is met.
Or am I wrong to think this.. because the datastream has already been read and therefore contentLength cannot be wrong?
I'm asking because I think I have run into a couple instances of an endless loop here.
How about https which need user name and password? Can we use this method?
Answer by franky303 · Oct 07, 2012 at 12:35 PM
Another alternative: Systme.Net.WebClient
Here is a plugin on assets store to download big files (in GBs), its working on all promised platform without any problem. http://unitydevelopers.blogspot.in/2017/06/large-file-downloader-for-unity3d.html
This is the plugin link
https://assetstore.unity.com/packages/tools/network/large-file-downloader-cross-platform-92128
Answer by MCLD3D · May 21, 2017 at 02:49 AM
If you can explain me how to make this snippet compatible with ipv6, you are a life saver
Your answer
![](https://koobas.hobune.stream/wayback/20220613081438im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to stop a WWW which is in progress? 0 Answers
downloading data in background without halting application 1 Answer
CloudFront statistics show 25% incomplete downloads using Unity3D WWW class 0 Answers
DownloadHandlerAudioClip compressed and streamAudio properties 0 Answers
www.texture memory leak 1 Answer