In the Unity Profiler, it says my meshes are taking over 200 mb memory. Thing is, I don’t have meshes in my scene. The only place they exist is in my list. Would that be it? Do prefabs on a list take memory?

I tried clearing the list, turning it to null, doing = new List. Yet they still exist in the profiler. My list derives from another class, which is serializable if that matters. Like List();

I hope I can get some help!

  • \$\begingroup\$ Why are you storing meshes in a list if you don't want them in memory? Presumably they're in that list because you need them to perform some action you want in your game, no? \$\endgroup\$ – DMGregory Jan 5 at 14:34
  • \$\begingroup\$ Note that they won’t be deleted immediately after setting the list to null, and so they will continue to take up memory. You can unload them manually using Resources.Unload on each one before emptying the list, or using Resources.UnloadUnusedAssets to clean up everything at once after emptying the list. \$\endgroup\$ – Ed Marty Jan 5 at 16:28
  • \$\begingroup\$ @DMGregory I forgot to mention that I pick one random prefab from the list and instantiate at Start. That is it yeah \$\endgroup\$ – user122760 Jan 5 at 16:33
  • \$\begingroup\$ @EdMarty Mind writing an answer? \$\endgroup\$ – user122760 Jan 5 at 16:33

You can avoid using up the memory by loading only the one mesh you want from an AssetBundle or Resources by name. This will keep the meshes you are not using from ever taking up memory.

Alternatively, if you don’t mind taking up large amounts of memory on scene load and clear it up after picking one, you can just instantiate the one you want from your list, then unload all the other ones using Resources.UnloadAsset. Note that you should not call this on the instantiated mesh, as it may remove the one used in the scene.

You can also try using Resources.UnloadUnusedAssets after emptying the list, although that may take some time to look through the entire GameObject hierarchy.

  • \$\begingroup\$ So Ed you are saying store all my prefabs in the AssetBundle then pick one right? \$\endgroup\$ – user122760 Jan 5 at 17:22
  • \$\begingroup\$ Yes, you can put them all into one asset bundle and then load the correct one by name. That would save memory. \$\endgroup\$ – Ed Marty Jan 5 at 22:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.