Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Memory leak issue

Discussion in 'Editor & General Support' started by Upp000, Feb 11, 2023.

  1. Upp000


    Mar 4, 2021
    In my game there is a memory leak, I can see in the task manager that the memory is building up.
    It goes from 400mb to 600mb in my test play (it can goes higher if I play longer), and I can't figure out the reason.
    There is no scene change in my game, it is seamless open world. And it uses multithreading to process lots of data.
    But I destroy texture and sprite, I set array and list to null, I dispose NativeArray, I even use Resources.UnloadUnusedAssets() and GC.Collect().
    Nothing works.
    What else can I do?
    Does this mean something is still using the data?
  2. PraetorBlue


    Dec 13, 2012
  3. Upp000


    Mar 4, 2021
    I have tried it.
    There are lots of data in my game, so It took a very long time to open the memory snapshot.
    I compared too snapshots but still couldn't figure out what causes the memory leak.
    There are lots of new data but also lots of data that no longer exist, so I can't tell which data type has memory increase.
    Unity tells us to compare the scene and an empty scene. But it seems pointless in my case. The memory leak happens within the scene, instead of between scenes.
  4. nasos_333


    Feb 13, 2013
    Try to not destroy anything when possible, creating a new of anything can cause the build up and also delay the game to create new resource in memory when is needed again and delay more when the garbage collector kicks in.

    That is why pooling is so important.

    E.g. set a global Texture2D or Rendertexture, create it and assign to it as needed. Avoid any new constructor when possible and make sure to clean all render textures, note rendertexture cleaner is different than the temporary rendertexture clean function

    Also try disable each script during play time and see if one of them stop the build up

    Just using the data wont increase RAM, you probably have some code that constantly creates a new structure that likely causes the issue.

    Ideally the optimal is to never use the "new" function during run time, when possible. Even better is to design the game structures based on this from begining.
    Last edited: Feb 12, 2023
  5. MartinTilo


    Unity Technologies

    Aug 16, 2017
    Taking two snapshots in quick succession (but at enough of an interval to fit the rythm at which your memory grows) and comparing those is also valid.
    If you see anything that's new that you would not expect to see new, check if the old stuff of the same type* is still around and why.

    * With >1.0.0 version of the package you can toggle on "Show Unchanged". You can capture with or import snapshots from older Unity versions into an empty Unity 2022.2+ project with the 1.0.0 version of the package.
    Upp000 likes this.