Unloading asset bundles doesn't clear memory on iOS


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 = "";
	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))

				AssetBundle bundle = download.assetBundle;

		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.

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:

4.5 will have the fix.
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];