Hi,
So I’m working on a game that downloads asset bundles and stores them on the iPhone/iPad. Then the game loads these asset bundles into the game. The game crashes due to memory warnings even if I’m only importing something like 10 asset bundles. Also there’s nothing particularly crazy about these asset bundles. They’re all something like 200 - 500 verts. So I’m wondering if there’s anything like memory leaks I have to be concerned about when using asset bundles. Also, I should mention that the game has a button that switches to some Objective-C menu screens and then can be switched back. I know this adds more memory onto the already existing Unity memory, but the developer working on the Objective-C part is saying he’s getting logs that point to memory leaks in Unity. It might be some carelessness on my part, but I feel like I’ve looked through my code pretty thoroughly.
look at the size they have.
if you load them they will basically remain there.
yes there is the unload but at least on the desktop, there is much more than just unload required to get them out of RAM again …
so you can pretty easily bog your ram to hell.
also ios4 + pre 3GS = 20-30mb ram and less, so nope you won’t need much to kill the app
What a tight budget. Thanks again dreamora.
Thought I’d add to this thread…
We are having serious issues with AssetBundles taking up too much memory.
We have run tests, using regular assets and loading assetBundles into memory to instantiate the SAME PREFABS.
Using prefabs compiled with the project causes no memory issues whatsoever.
But using the AssetBundle method causes so much memory to be used up that the app invariably crashes.
Here’s the thing: much more memory is being used than the AssetBundles actually contain. I’d say as much as 4 or 5 times more.
Here’s part of our code:
WWW wwwBundle;
GameObject asset;
wwwBundle = new WWW(assetBundlePath);
yield return wwwBundle;
if(wwwBundle.error == null)
{
AssetBundle loadedAssetBundle = wwwBundle.assetBundle;
asset = (GameObject)loadedAssetBundle.mainAsset;
if(asset != null)
{
AddObjectToScene(asset);
loadedAssets.Add(assetBundlePath, asset);
}
}
where “loadedAssets” is a System Dictionary for us to store references to bundles that are currently in memory (so we don’t have to load them twice).
When we run this block around 15 times (different bundles, ~0.5MB per bundle) we run out of memory and crash our iPads.
Any explanations or help would be greatly appreciated.
that code should not even work because you don’t unload the asset bundle and as such its always present and trying to load the same should actually cause an error that you try to use an asset bundle already loaded
As I mention, we use “loadedAssets” to prevent this. I apologize if I was not sufficiently clear. Here’s the little check:
if(loadedAssets.TryGetValue(assetBundlePath, out asset))
{
AddFish(asset, node);
}
else
{
…etc.
So we never load an assetBundle twice. We also never Unload an assetBundle once it’s been loaded. We were under the impression that it was necessary to keep AssetBundles in memory while their assets are being used in the scene or stored in our dictionary. Is this incorrect? Should we unload the AssetBundle immediately after storing the asset?
I thought I tried this and it didn’t work…
After further testing, we have discovered that the way to properly unload assetBundles while not causing individual assets to get unloaded is this call:
someBundle.Unload(false);
However, using this call while only loading the main asset from the bundle can cause a crash if the main asset is a prefab and the bundle was built to include dependencies. The solution is to first:
someObjectArray = someBundle.LoadAll();
An even better solution would be to use Asynchronous loading, wait till ALL assets are done loading, then use the Unload(false) method. It would be nice if there was a LoadAllAsync() method but there is not.
I should point out that we still find assetBundles to have a large memory overhead. Also, we notice some oddities in what ends up going into an assetBundle when it is built to include dependencies. For instance there appear to be two of some things, but only one of them is the real thing, the other is a ghost file.
It is my hope that this information helps others working with AssetBundles.