- Home /
Unloading asset bundles doesn't clear memory on iOS
Hello.
I'm developing a game with large amount of graphical resources, and I'm using asset bundles to download those resources after the game is installed.
After the start of the game user sees the preloading scene, where asset bundles are downloaded and cached, in order to use them during the game.
The following code is used for caching asset bundles:
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
public class AssetBundleLoader : MonoBehaviour {
const string BaseURL = "http://192.168.1.46/AssetBundles/";
static List<string> assetBundleNames = new List<string>
{
// Asset bundle names
};
// Use this for initialization
IEnumerator Start () {
foreach (string assetBundleName in assetBundleNames)
{
string fullAssetBundlePath = BaseURL + assetBundleName;
using (
WWW download = WWW.LoadFromCacheOrDownload(fullAssetBundlePath, 1))
{
yield return download;
if (!string.IsNullOrEmpty(download.error))
continue;
AssetBundle bundle = download.assetBundle;
bundle.Unload(true);
}
}
yield return Resources.UnloadUnusedAssets ();
GC.Collect ();
}
}
The problem is that bundle.Unload(true)
doesn't clear the memory. Thus, when I try to cache a lot of asset bundles in my scene, the game crashes due to Memory Pressure on iPad 2.
Any ideas on how to handle this would be useful. Thank you.
Answer by StormTeam · Aug 12, 2014 at 06:57 PM
I believe it is actually the WWW that is leaking, not the asset bundle itself. Thus, unloading the asset bundle won't help. We ran into a similar issue in our app. We were loading lots of textures from WWW and noticing iOS being the only platform to have a memory leak from it. We eventually found our solution here http://forum.unity3d.com/threads/www-memory-leak-ios.227753/. Basically, there is a known issue in unity 4.3 that leaks the data from www calls. This SHOULD be fixed in unity 4.5. In the meantime, you can follow Alexey's suggestion to modify the code in the generate xcode project or update to 4.5:
Essentially you need: search for extern "C" void UnityDestroyWWWConnection(void* connection) in WWWConnection.mm [delegate.connection cancel]; delegate.connection = nil; [delegate.data release]; // <-- ADD THIS [delegate release];will have the fix.
Just figured I'd confirm. This is fixed in Unity 4.5. I am running 4.5.1 and the UnityWWWConnectionDelegate class has a 'cleanup' method which now releases the _data inside.
Your answer
Follow this Question
Related Questions
Does assetbundle work well with mobile devices? 1 Answer
Memory Allocation Continues to Grow? iOS. 4.6.3 0 Answers
Memory leak using bundles. 1 Answer
AssetBundle.Unload(False) in IOS Save Reference ? 0 Answers
mp3 files giving ios memory warning 0 Answers