Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

After a LoadLevelAdditive, is it possible to free up the memory? Unity docs say you can...

Discussion in 'Editor & General Support' started by Dreamwriter, Jul 21, 2014.

  1. Dreamwriter

    Dreamwriter

    Joined:
    Jul 22, 2011
    Posts:
    472
    But, I was under the impression you can never get that memory back, so you can't unload different parts of the world. Am I incorrect? We want to use LoadLevelAdditiveAsync exactly as described, to create a streaming world, but if you can never recover the memory, and keep exploring the world, your memory usage will just keep going up and up and up until you run out and bad things happen.
     
  2. Graham-Dunnett

    Graham-Dunnett

    Administrator

    Joined:
    Jun 2, 2009
    Posts:
    4,287
    You can Destroy game objects if you no longer need them.
     
  3. Dreamwriter

    Dreamwriter

    Joined:
    Jul 22, 2011
    Posts:
    472
    I thought that didn't actually free up memory, though, until you load a full new scene (or the assets were loaded through Resources.Load and then an UnloadUnusedAssets was called).
     
  4. Dreamwriter

    Dreamwriter

    Joined:
    Jul 22, 2011
    Posts:
    472
    Any ideas?
     
  5. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    The Garbage Collector will eventually clean up objects that aren't referenced by anything. The hard part is making sure your object isn't referenced by anything, or referenced by something that's referenced by something else, etc. Making sure there are no references anywhere to the object can be super hard when the code is complex and spaghetti-like.
     
  6. Dreamwriter

    Dreamwriter

    Joined:
    Jul 22, 2011
    Posts:
    472
    So you're sure that the memory is freed up? Because I've been told, and it's been my experience with the profiler, that you can NEVER get memory back except in two cases: when you load a new scene, or if you are using Resources.Load and after Destroying the objects call Resources.UnloadUnusedAssets. Any assets included directly in a scene are in memory forever. I've had to optimize memory to get games to run on old iPad 1/iPod Touch 4, and we couldn't clear memory any other way.

    Admittedly, this time we're targeting PC/Standalone, so maybe it's different.