- Home /
When I load 200+ asset bundles, memory leak will happen.
Hello everyone! I'm having a terrible trouble now, help me please!~~
In our project, every asset like texture, material, model has been bundled as a asset bundle. When they are be building, use BuildPipeline.PushAssetDependencies() and BuildPipeline.PopAssetDependencies() so that every bundle will be very small. I call BuildPipeline.BuildAssetBundle() use BuildAssetBundleOptions.CollectDependencies argument so that dependencies will be tracked. And then I encrypt all the asset bundles.
All assets in the scene is made into a prefab named A. Before I instantiate A , I need to do these things:
1.Decide which asset bundles are using by A, get the sets.
2.Read bytes from file, and X byte buffers to contain dencrpyted bytes. X is equal to the number of the asset bundles.
3.Call AssetBundle.CreateFromMemory to build asset bundle for every byte buffer.
4.Call AssetBundle.Unload(false) to free memory for every asset bundles. Get A's reference at the same time. Object instance = assetbundlePool[A].assetBundle.mainAsset ;
I do this time and again, memory leak will happen, but not every time.Leak a huge memory oh my god!
5.Instantiate instance, then the all the assets will be present.
6.When I want to destory all the assets, I call GameObject.Destory(instance). Because all the assets is made into one game object, so if I destory it will give up all the referencies, so the assets' memory will be freed when garbage collection happens I think.
That's all, I don't know how unity achieve asset bundle.I hate this feeling.
Welcome to follow up! Thanks ahead! Merry Christmas! Ha ha, a cold joke...
I'v done all I can do I think, but I can't control the memory leak. I'm a user, not a programmer I think, this make me sad.
Answer by RalphTrickey · Jun 09, 2013 at 04:21 AM
Memory leaks in C# aren't true leaks, they are seen as being in use and not collected. This is usually caused by objects being assigned to a variable somewhere, possibly in a member variable, or somewhere else. If you've got an array or GameObjects or or a List somewhere that's holding them, or something else like that, it can cause memory to grow. Event handlers can cause the same issue. Make sure to null out any objects and disconnect any event handlers that aren't used. That's the best suggestion I can give. I also found this link. http://wiki.unity3d.com/index.php?title=DetectLeaks
BTW, how do you know you have a memory leak? Managed languages like Unity are notorious for allocating all available memory and not freeing it until they need to. There are some options to for a garbage collection, etc. for normal .Net, I'm not sure about Mono. I wouldn't recommend calling them though except under very unusual circumstances by an expert.
Answer by ChenMo2 · Jun 13, 2013 at 02:20 AM
Thank you very much!
I use task manager of windows to detect memory variousness. When the program I built was running, it loaded and unloaded asset bundles repeatedly. Along with it was running, the memory it used growed. I'm sure I've nulled out all the asset bundles' reference.
Now, I've decreased the amount of asset bundles, and memory leak won't happen. I've test it use task manager. I don't know how AssetBundle works, it's really a trouble when too many asset bundles are used.
Your answer
![](https://koobas.hobune.stream/wayback/20220613103655im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to import the object from server to unity 2 Answers
AssetDatabase.LoadAllAssetsAtPath(...) Yields Mysterious Nameless Asset 1 Answer
DontDestroyOnLoad() assetbundle 1 Answer
Streaming Tutorial anywhere? 2 Answers
Runtime loaded assemblies 1 Answer