Search Unity

Need Help With Memory Management

Discussion in 'Scripting' started by Roldo, Apr 13, 2019.

  1. Roldo

    Roldo

    Joined:
    Oct 25, 2014
    Posts:
    26
    So, i was coding a random level generator for my game and noticed a problem where unity wont free up memory after i destroy all instances of prefabs(in case generator failed on the first attempt) and will take even more memory when i restart the generator. It wouldnt even free up memory when i exit play mode. Then i compiled the game and it got even weirder: it would not free up memory after i closed the game and it would just take up more memory after i re opened it(eg. If it took 1gb of ram to run, if would take 2gb second time i opened it). Im using UnloadUnusedAssets and GC.Collect() as well as cleaning up all lists and dictionaries and ensuring that coroutines are stopped after generator has finished. I dont know whats causing the issue and how to fix it, so if anyone has any ideas, please share. Im using unity 2019 beta
     
  2. image28

    image28

    Joined:
    Jul 17, 2013
    Posts:
    457
    An example/snippet of your memory management code may help
     
  3. MartinTilo

    MartinTilo

    Unity Technologies

    Joined:
    Aug 16, 2017
    Posts:
    265
    What are you using to track your memory usage? Have you had a look at the new Memory Profiler Package to track what is kept in memory and why?

    Also, nothing in RAM persists across 2 distinct runs of your standalone build. Are you writing your generated level to disk and reloading it?
     
  4. Roldo

    Roldo

    Joined:
    Oct 25, 2014
    Posts:
    26
    Thank you, i will check it out!
    And no, im generating it at runtime from seed
     
  5. Roldo

    Roldo

    Joined:
    Oct 25, 2014
    Posts:
    26
    I did check memory with the new profiler, however it foes not show any memory usage increase either(memory usage is still increasing with each run). Im starting to think that it is an editor bug.
     
  6. MartinTilo

    MartinTilo

    Unity Technologies

    Joined:
    Aug 16, 2017
    Posts:
    265
    If the Memory Profiler is not showing the memory increase, then what is showing you that? The System monitor, The Profiler Window?

    Also, as you mentioned that you suspect this to be an Editor bug: is the memory increasing for a standalone build or for the editor? If the later, you should profile a standalone build to find out if your game logic has issues. There might be issues were your game logic manages to retain some memory between runs in the editor but there's also a whole bunch of other things going on in the Editor relating to memory.

    You might have memory fragmentation. Also the managed heap only ever grows and never shrinks. Also, native memory pools also only ever grow. So even if you don't have anything in them anymore, the total memory that unity uses for that section of the memory is not getting freed. You can use the Memory Map in the Memory Profiler Package to see those regions and how much of them is actually used (external tools will only see them as fully used). That view can also be helpful in finding fragmentation issues. Outside of that view, we sadly currently don't show full usage of memory. i.e. something where we account for reserved but unused memory sections.
     
  7. Roldo

    Roldo

    Joined:
    Oct 25, 2014
    Posts:
    26
    Thank you for the reply! The increased memory usage is shown in the system monitor, and the standalone build memory leak was a bug on my side, ive corrected it, however total editor memory usage does still increase by a smaller amount(by around 100-200mb each run)
     
  8. MartinTilo

    MartinTilo

    Unity Technologies

    Joined:
    Aug 16, 2017
    Posts:
    265
    Are you using ScriptableObjects to store data of the generated level? Does this growth in memory stop at some point or do you then just crash out of memory?
     
  9. Roldo

    Roldo

    Joined:
    Oct 25, 2014
    Posts:
    26
    I am using ScriptableObjects to store prefabs used for generator, however the levels are generated in runtime and not stored in memory(for now, as i didnt implement save/load game system yet). I also updated to version 2019.1.0f2 and the growth in memory seems to slow down, now it goes in increments of around 5-10mb of ram.
     
  10. MartinTilo

    MartinTilo

    Unity Technologies

    Joined:
    Aug 16, 2017
    Posts:
    265
    does it grow by 5-10mb gradually or from a measurement before hitting play to hitting play, waiting for generator to finish and then stopping the game and measuring again?

    Can you:
    1. make sure the profiler is not recording
    2. Take a memory snapshot just after opening by using the Memory Profiler Package (Don't open it yet, that would load it into memory and skewer the result)
    3. press play, wait, press stop
    4. Take a second memory snapshot
    5. Open both snapshots
    6. Press the diff button
    7. Look at the Memory Map Diff view
    That should give you some idea of why the memory grew. Likely it is the Mono heap that grows. Remember, it never shrinks, not even in the editor. Additionally you might be loosing memory space due to fragmentation. The Memory Map Diff view should expose some of that.
     
  11. Roldo

    Roldo

    Joined:
    Oct 25, 2014
    Posts:
    26
    Thanks for the help and sorry for the wait. I figured out the source of the issue: it was to do with one feature of the generator i was working on, ive fixed it now. Thank you and sorry for inconveniance!
     
    MartinTilo likes this.