Search Unity

Prefabs and Memory

Discussion in 'Getting Started' started by Green11001, Oct 21, 2021.

  1. Green11001

    Green11001

    Joined:
    Apr 14, 2018
    Posts:
    397
    Hi, I would like some advice when it comes to saving memory and using prefabs.
    Context: I was trying to make every single ability the player/enemy can have a prefab, and then stick it onto a giant array. Since the player will only have a few abilities at a time, I would, when loading a level or when needed, grab the prefab from the array and instantiate it to create an ability object to use.

    However, I have recently read that prefabs use up memory if they are referenced in any way (This would mean that as I add more abilities, my game would use up more and more memory). How exactly does this work? I also read that this can be overwritten and you can load up assets when you want to. How would you go about this? Would I need to change my array somehow? And before I do that, since Unity automatically loads up prefabs, I assume there is a reason for such, so would it actually be better to just find an alternate solution? ( I was thinking that maybe loading prefabs up uses a ton of CPU, or something)

    I am trying to build to mobile
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Anything referenced in a scene is loaded into memory when the scene loads, before the scene starts its first frame. That includes anything referenced by those references, etc, etc, down the tree. For example, if you have a prefab that references 10 other prefabs, and your scene references that first prefab, when the scene loads all 11 prefabs are loaded into memory.

    The only exception I'm aware of is you can set sound clips to load in the background after the scene loads. I find this useful for example when I have a prefab which references a music library, so I have it load the first song when the scene starts, with all the other songs loading in the background.

    Whether this is a problem depends on how big these prefabs really are as far as memory footprint. If you don't need them you can split them up across different scenes you additively load as needed (I like to use this strategy for large terrains, or other content I know I either will or won't need ahead of time). You can also put stuff in a Resources folder, and use Resources.Load at runtime.

    https://docs.unity3d.com/ScriptReference/SceneManagement.LoadSceneMode.Additive.html
    https://docs.unity3d.com/ScriptReference/Resources.Load.html
     
  3. Green11001

    Green11001

    Joined:
    Apr 14, 2018
    Posts:
    397
    Hm, well the prefabs themselves aren't too big, but there will be a lot of abilities with more constantly being added as I work on it more. Would it be a good idea to load the scene in, find a gameobject in the scene containing the references, instantiate or copy them as needed, and then unload the scene (occuring during some kind of loading screen)?
     
  4. Green11001

    Green11001

    Joined:
    Apr 14, 2018
    Posts:
    397
    Hello again. I have been messing with Scene loading, references, and unloading in terms of memory usage. I made an array with a length of several million (empty) either strings or integers. I tried first with strings, and did not get either scene loading or removing the reference to get memory back. Is there a reason why? I then tried with integers, and the profiler showed that memory used was being returned when the scene was unloaded, but only sometimes, and also not the full amount of memory (understandable). I also tried to use "Resources.UnloadUnusedAssets();" in many different situations, but I have never seen it do anything. This leaves me with many questions. Where is my memory sometimes being lost?