Search Unity

Memory question!

Discussion in 'General Discussion' started by JerbearGames1004, Nov 21, 2022.

  1. JerbearGames1004

    JerbearGames1004

    Joined:
    Dec 20, 2021
    Posts:
    17
    So I need to make a very simple item database. I have a general idea of how to make it, but there's one basic nagging concern. Say for example I have a monobehaviour class slapped on an object in the scene, and that class has perhaps an array of "Item" scriptable objects for every type of item in the game. In this example, is everything in that simple database loaded all the time and thus taking up memory? And if so, what are best practices here?

    Thanks in advance!
     
  2. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,569
    Unless you have a billion of items in the database, chances are it will all fit into the RAM on modern PC, and the PC won't notice it.

    To put it into a perspective. A single book can be bet stored in something between 250 and 500 kilobytes (just text, no images). So 1 megabyte is two books. Modern PC can easily have 32 gigabytes of RAM, and that would be 64 thousand books* (if we ignore OS overhead), stored in memory alone
     
    DevDunk likes this.
  3. JerbearGames1004

    JerbearGames1004

    Joined:
    Dec 20, 2021
    Posts:
    17
    ok so I don't generally need to worry about it. cool!

    I would still like to understand the behind the scenes workings of things best I can, so.... I assume by your reply it IS all loaded in the previously mentioned example. Would deactivating the GameObject that the database class is attached to in any way unload it all? I recall reading that inactive GameObjects arent held in memory fully somehow is why I ask.

    I still want to know the deeper workings of this in case I may want to use somewhat higher cost images or meshes or anything attached to the item SO classes....
     
  4. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,620
    No, because it's still in the Scene, and you could activate it at any time and expect it to be 100% ready to go on the next tick. It's also common to activate/deactivate GameObjects for "pooling", so unloading them would be counter-productive.

    Even removing something from the scene doesn't guarantee that it's unloaded. Unloading stuff takes effort, and you might want it again later anyway, so it generally makes sense to just leave stuff alone until there's a reason or a good time to clean it up.

    If you want to specifically control when stuff is loaded and unloaded then you should look into Asset Bundles and Addressables. This way you can have a list of asset addresses somewhere, and load/unload what you want from those addresses as needed.

    Prefabs may also be worth looking into. I'm unsure of whether or not their contents are loaded prior to being instantiated.

    Another option may be the StreamingAssets folder. I've not used it for some time so I don't remember the details, but I believe the contents are just copied into your game builds as-is, and can be accessed via any method which can accept a file path - including WWW.
     
    Last edited: Nov 22, 2022
  5. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,569
    It won't, every object in the scene is loaded into RAM, doesn't matter if it is active or not.