Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

Question Does "reserved" memory matter?

Discussion in 'General Discussion' started by DockAnkh, Jan 26, 2023.

  1. DockAnkh

    DockAnkh

    Joined:
    Nov 25, 2021
    Posts:
    10
    My game sometimes creates a game tree that can be quite large (10's of millions of nodes). Looking at the Profiler, when the tree is overwritten and I call GC.Collect() the amount of memory being used decreases hugely, which is what I expected, but the "reserved" memory stays high. Does this matter? Will my game interfere with other programs if it's holding a lot of memory as "reserved"? Will it give that memory back to the system if another program wants more? How important is "reserved" memory?
     
    duckyfoundco likes this.
  2. r31o

    r31o

    Joined:
    Jul 29, 2021
    Posts:
    460
    AFAIK the reserved memory cant be used by other programs, so even if it doesnt affect directly to your game, the end user will have less memory for other things.
     
  3. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    2,467
    This is true of the way that processes work on most modern OSes. Once an application runs out of its initial heap, it requests a bit more from the OS. But there's no provision to return that memory back to the OS for other applications to use. Why not? Because the parts of memory you "free up" are almost never contiguous, and almost never at the far edges of your address space. There's no good way to give the little free pieces back to the OS and still manage your application as a contiguous address space.

    Let's pretend you were holding a giant festival at one end of a large park (the OS). Families can come at any time and lay down a blanket on any free patch of grass near the stage (your application). At the beginning of the show, all the families have been laying down blankets and enjoying the show in a pretty good tight cluster. As more people show up, they have to set up their blanket a little farther from the stage each time. At mid-show, some families have to go home and take their blankets with them, freeing up some space. MAYBE someone else watching the show takes over some of the little holes that were freed up by those early-bedtime guests. However, there's no way you're going to get ALL those families who want to watch the rest of the show to move in closer, even if they are way out at the middle of the park far from your stage setup. Your application is now taking up more than half of the park area, even as more and more of the early attendees decide to pack up and leave. Nobody will feel comfortable using any of that area for other activities like frisbee or baseball, while there are still some blankets here and there across the field.

    You want to be careful about pre-allocating memory or putting up barriers around the section of grass based on the number of people you THINK might want to attend the festival. If your guess is wrong, you're either wasting all that parkland memory that other people could have used, OR you need to move those barriers around yet again for a surprisingly popular show.
     
  4. DockAnkh

    DockAnkh

    Joined:
    Nov 25, 2021
    Posts:
    10
    Thank you. So if I understand you correctly I should be careful about how much memory my program is using, even for a short period of time, because it will keep blocking holding onto that memory.
     
  5. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    2,467
    It's okay to use memory if there's a gain to it. If you know you're going to have a much-larger-than-typical allocation for a short period, do your best to break up the task to require less memory at once, so that memory usage remains more consistent. Your first example of needing to load up millions of things once, then wash them out of the scene, is the opposite of the desired consistency.
     
    r31o, Ryiah and DockAnkh like this.
  6. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,632
    If it's a real concern, then you could potentially start another process, have it do the memory greedy stuff, and terminate it once you've pulled the results over.