Search Unity

Headless server and memory usage

Discussion in 'Editor & General Support' started by numbers1234, Sep 1, 2020.

  1. numbers1234

    numbers1234

    Joined:
    Jan 5, 2019
    Posts:
    31
    I am trying to reduce the memory usage of my game's server. Upon looking into it, I noticed the server is unnecessarily loading textures and meshes, which is taking up a lot memory. I set up some foreach loops and destroyed materials, terrains, meshes and textures for each scene loaded. However this is not reducing memory in the slightest. Even after calling GC.Collect() and Resources.UnloadUnusedAssets(). In the memory profiler it looks like I cleared up 850 mbs for "RenderTexture", but that memory is not being freed and the executable is still consuming the same amount of memory. Countless google searches have yielded nothing. I have attached an image of the two memory captures. The top one is without any deletion, and the bottom one is after destroying unneeded assets. Any help would be greatly appreciated.
     

    Attached Files:

  2. MartinTilo

    MartinTilo

    Unity Technologies

    Joined:
    Aug 16, 2017
    Posts:
    2,461
    Hi there,
    Unity's Memory Manager is optimized to be greedy. I.e. it doesn't like to free memory it once claimed, especially native Memory. That is because claiming it takes time and cycling through too much memory (claim, release, claim, release...) can lead to some OSs deciding to kick your app out of memory.

    If you check the Profiler Windows Memory Module, or the Memory map in the packaged one, it will very likely show you the space it reserved vs the space that is Allocated and full of objects.

    To avoid it to bloat up like this, you should add a pre build step for your server build where you delete the textures then and there so that they never even get loaded in it. Alternative ways are to separate logic and visual representation (in DOTS the Worlds concept can be used for that) and to then build without the visuals for the server.
     
    Cornwine and numbers1234 like this.
  3. numbers1234

    numbers1234

    Joined:
    Jan 5, 2019
    Posts:
    31
    Awesome! Thank you MartinTilo that should do the trick. For anyone else that comes across this look into IPreprocessBuild or IPreprocessBuildWithReport for 2018 on up.
     
    Cornwine, tuccci and MartinTilo like this.
  4. ryandanielbarton

    ryandanielbarton

    Joined:
    Dec 31, 2015
    Posts:
    53
    I love both of you

    Thank you.
     
    numbers1234 likes this.