Hello,
I am currently trying to build an app with a new feature for mobile platforms (iOS and Android). The added feature consists of downloading a list of movies (up to 60). At first glance, using the www class to download movies seemed to work fine on all platforms but unfortunately, an OutOfMemoryException occurs after downloading several movies. On iOS this occurs after downloading approximately 6-16 movies (the file size varies between 10 and 50 MB). Since I have read about other people having similar problems with downloading “large” files, the size may be relevant.
I am using Unity 4.6.2p1 and testing the app on an iPad3 and 4 (iOS 8.1.3) as well as on a Samsung Note 10.1 (2014 Edition) (Android 4.3). Please see the relevant code below:
public class MovieController{
WWW www;
List<string> downloadableMovies;
public void StartDownload(List<string> movies){
downloadableMovies = movies;
StartCoroutine("DownloadMovies");
}
IEnumerator DownloadMovies() {
int i = 1;
foreach(string fileName in downloadableMovies){
www = new WWW(url+"/"+videoInfos[id].basename+movieExtension);
yield return www;
if (www != null && string.IsNullOrEmpty(www.error) && www.isDone) {
FileStream stream = new FileStream(Application.persistentDataPath+ "/"+ fileName, FileMode.Create);
try{
stream.Write(www.bytes, 0, www.bytes.Length);
}
catch(Exception ex){
DeleteMovie(fileName); // delete incomplete file
yield break;
}
finally{
stream.Close();
stream.Dispose();
stream = null;
}
}
www.Dispose();
www = null;
i++;
}
}
}
Obviously there has been a known memory leak issue on iOS prior to Unity 4.6.1 (see the issue tracker) but since I am using a newer version, this should have been resolved. I have already tried using a “using” statement and have added Dispose-calls for both the www and Stream instances. After several time-consuming rebuilds and tests on iOS, I even tried implementing another download solution using the System.Net.Sockets library based on this Unity Answer but that doesn’t seem to work with the 64-bit version for iOS (required since Feb 1st).
Are there any mistakes in my code that I am oblivious of? Or can anybody confirm that using the www class for downloading files causes memory problems?
If the alternative socket solution does currently not work with 64-bit iOS builds, what else could?
Any help would be much appreciated.
EDIT: I uploaded the iOS log, in case that may prove helpful.
EDIT 2: I also tried downloading the movies via the WebClient API which is currently not functioning, see the Unity blog.