Search Unity

Storing prefab references out of memory

Discussion in 'Scripting' started by Sendatsu_Yoshimitsu, Aug 26, 2019.

  1. Sendatsu_Yoshimitsu

    Sendatsu_Yoshimitsu

    Joined:
    May 19, 2014
    Posts:
    673
    The way I've always stored references to prefabs (to spawn enemies, change displayed armor/weapons, and so forth) has been by using a singleton object manager that has a manually-assigned reference to each prefab or model and a corresponding string ID, so I can always generate a new instance on-demand. This works great for prototyping, but it also means that I'm keeping each of those prefabs loaded in memory. Is there a more memory-efficient alternative I'm unaware of, or is this one of those issues where the added complexity and difficulty of wrangling a more memory-efficient storage solution wouldn't be worth the performance gain?
     
  2. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    768
    It's probably possible, but if you did not store the prefab in memory (ram), then you'd store it on your hard drive. So each time you access it, that's where you would have to load it from, which (in computer terms) takes forever. I cant really imagine that this is something you would want to do in any given game scenario, unless it's a very big game. Maybe if you are changing scenes and dont care about higher loading times, then you could only load in the necessary data for the scene, but in most situations i'd probably prefer faster loading times.

    How large are your models? How many? Or in other words; how much memory is used up? As a rule of thumb, people got enough memory nowadays, so unless this is a very specific case i would not worry about it.

    So to summ it up, it's less about the added complexity than about the resulting difference in loading times. Loading from RAM is at least 100 times faster than loading from a HDD. And even then the CPU idles for like hundreds of cycles.
    (Which is also why good memory layout speeds up programms like crazy)
     
    Last edited: Aug 26, 2019
  3. Sendatsu_Yoshimitsu

    Sendatsu_Yoshimitsu

    Joined:
    May 19, 2014
    Posts:
    673
    Thank makes sense, thank you for the writeup! My planned scope isn't giving me pause (30-50 character models < 5mb, under 100 static models <1mb), I mainly wanted to float the idea to make sure that this wasn't an approach that worked fine in a vertical slice but was going to bite me in the rear end or grow into a giant antipattern at scale. ^^
     
unityunity