Search Unity

  1. Get the latest news, tutorials and offers directly to your inbox with our newsletters. Sign up now.
    Dismiss Notice

Help Wanted Infinite voxels but it runs out of memory pretty fast

Discussion in 'Editor & General Support' started by captainspaceman, Mar 19, 2021.

  1. captainspaceman

    captainspaceman

    Joined:
    Jul 14, 2017
    Posts:
    29
    I'm storing the chunks in a dictionary where the key is the chunk's position and the value is the chunk GameObject. Right now I just deactivate the objects that are too far away. But this quickly fills up the dictionary and I get an error saying "The system is running out of memory. Please close applications to free memory or free up space on the partition where the pagefile is located.
    Used memory (physical and paged): 93% (56998MB of 61282MB)."

    56998MB!? That's 57 GB, right? Is there something wrong with my code? I measured it and when there are about 2700 chunks in the scene, I get this error, and obviously, the game is really slow up until then.

    Is my only solution to just decrease the render distance and store the chunks in a compressed file somehow? Am I using dictionaries the wrong way?
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    5,674
    Dictionaries are great for quickly looking up data by a known identifier/key.

    What they don't do is magically reduce the amount of data objects take up.

    The standard strategy for something like this is to keep only a small amount of the world in memory at a time. The player can only interact with a small area around themselves, so you should be dynamically unloading any chunks that the player can't immediately interact with. That could be chunks that are far away, or that you have otherwise determined are not interactable.
     
  3. captainspaceman

    captainspaceman

    Joined:
    Jul 14, 2017
    Posts:
    29
    Ok ya and I think I'm just going for too big of a render distance, if I increase the size of the chunks and decrease the render distance it actually works great! I was thinking to myself that I might be able to fit 1000 chunks in a scene at once and then Unity displayed a windowed error saying "virtual alloc failed" and then crashed. Is there any way to do this without using Unity GameObjects? All I need is a mesh renderer for the chunks, not even a collider or anything.
     
  4. captainspaceman

    captainspaceman

    Joined:
    Jul 14, 2017
    Posts:
    29
unityunity