Where is Actually The Prefabs Stored Before Instantiation? RAM or Storage(Internal Memory)

What I wanted to know is where is the prefabs actually stored before it is used or called. I was looking to reduce the Memory usage of my game. I don’t know how the Prefabs are retrieved when instantiating them from the game. Are they called from the Storage at the time of the requirement ? Or Are they already stored in the memory even though they are not in use? Thanks.

Generally there are two cases:

  • Prefabs (or more generally any asset) that is referenced from a script / component that is in the scene are loaded into memory when the scene is loaded. Such prefabs are in memory but not inside the scene.
  • Assets which are located in a Resources folder and are not directly referenced by a script / component in the scene is initially not in memory but is stored on “disk”. Those assets are loaded into memory when you use Resources.Load. A prefab might even reference other assets such as Materials, Textures, Meshes, … which are also loaded into memory. Keep in mind that the objects loaded with Resources.Load will stay in memory. When you use Resources.Load again it just returns the reference to the instance in memory.

Both cases just describe the state of the prefab in memory. Instantiate will simply duplicate any asset. For this the source object that should be cloned has to be in memory. For prefabs Instantiate will also move the clone into the scene so it’s actually visible / active in the scene.

You can call Resources.UnloadUnusedAssets (which is a quite heavy method) which will wipe out any asset in memory that currently isn’t “in use”. Keep in mind any asset which is referenced from the scene is in use. So for example if you use Resources.Load to load a big prefab. Now you instantiate the prefab into the scene. The prefab consists of several models which uses several materials and textures which are all loaded along with the prefab. If you call UnloadUnusedAssets after you instantiated the object, only the prefab instance will be wiped out of the memory. However since the prefab-instance is still using the Meshes, Materials and Textures they will stay in memory. If all gameobjects which reference the same Mesh, Material or Texture are destroyed, once you use UnloadUnusedAssets those assets will be cleared from memory as well.

Since the real memory filling assets (Textures and Meshes) are shared between all instances there isn’t much memory saved when you unload only the prefab source. It’s more likely that constantly using UnloadUnusedAssets and Resources.Load is much worse for performance.

Why are you concerned about the memory usage of your game? Do you have allocation problems? In other words does your game crash with an OutOfMemory exception? If not you shouldn’t really care about the memory footprint. Having a lot objects in memory will actually increase the performance as the objects are available when needed.

If you are using Resources.Load they are loaded from Disk. If you are Instantiating using a reference in a scene’s GameObject script then the prefab is in memory when the scene is loaded.