Search Unity

Memory Management and Crashes for big game

Discussion in 'Editor & General Support' started by jasonoda, Jun 27, 2017.

  1. jasonoda

    jasonoda

    Joined:
    Sep 7, 2012
    Posts:
    67
    I have a giant game done in Javascript. When I make a build, the game crashes randomly about every 20 minutes. Usually during points of high instantiation. Clearly I have not managed my memory properly.

    The entire giant game takes place in one scene and has lots of procedural generation and instantiation. I've tried to look around for some tips on how to keep these crashes from happening, but they always seem like bandaids for very specific situations. Also, going back over 100000 lines of code to implement some best practices things seems like it will have a relatively low impact on things.

    Is there a way to just clear everything from memory between levels and then just re-reference the things I still need via script? I don't mean forcing garbage collection, I mean clearing everything lingering in the memory. Or perhaps I'm going about things in the wrong way?
     
  2. Dave-Carlile

    Dave-Carlile

    Joined:
    Sep 16, 2012
    Posts:
    967
    You can't really make that claim unless you've examined error logs which indicate it's a memory issue. There should be a crash log produced by Unity in the build folder. What sort of errors are showing up there?
     
  3. tsibiski

    tsibiski

    Joined:
    Jul 11, 2016
    Posts:
    604
    If the game is crashing after a time period, and not during specific parts of the game, then it sounds like a GC/Memory Leak issue, given what you've said.

    What is the game running on? Is it running on an android/iphone? Particularly, a low quality device? Memory management is much trickier on those, and you may have to get really clever to fix those issues. However, if it is crashing on high end machines, or on a PC/Mac, then you'd have a hard time crashing a game from too much memory usage without having a major memory leak somewhere. And if that is the case, find what is not being destroyed, and make sure it gets destroyed.

    While your codebase may or may not benefit from a refactoring to help memory usage, it sounds to me, given what we currently know, that there are a small number of points of failure. Correcting those should be your priority.

    Do you attach A LOT of listeners? You must manually detach listeners/delegates from references, as the GC cannot destroy them for you.

    Do you spawn a lot of objects, where you may not have a backup to destroy those that passed your primary means of destroying them when they are no longer useful? If it happens in the Unity Editor too, you can see the game objects building up in your hierarchy view.