Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question fills the RAM in server application

Discussion in 'Editor & General Support' started by Farshadfarzan368, Sep 20, 2023.

  1. Farshadfarzan368

    Farshadfarzan368

    Joined:
    Sep 10, 2022
    Posts:
    73
    I wrote a server with Unity and C# language for the game server, in which many objects are made from classes
    They are all code and have no game objects
    Now the problem is that it fills the RAM slowly until it crashes. One or more parts of the code cause the RAM to fill up and not empty
    I don't know where it is, it should be found
    I installed the memory capture and got the output in two times.
    https://drive.google.com/file/d/1UkNvJM50SUf_vIAlVxPQhEFzsiAFB1nP/view?usp=drivesdk
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,051
    Grab the profiler and find out what is leaking.

    Windows -> Analysis -> Profiler

    If it's in your code, fix it.

    If not, file a bug.

    Help -> Report Bug
     
  3. MartinTilo

    MartinTilo

    Unity Technologies

    Joined:
    Aug 16, 2017
    Posts:
    2,482
    As the attached Memory Snapshots show, they already used the more fitting tool for the job, i.e. the Memory Profiler (at version 1.1.x no less)


    That doesn't seem to be an entirely accurate description based on the content of these snapshots, though I can understand the confusion. upload_2023-9-21_17-44-57.png
    I think you're adding a whole bunch of instances of Pasur.Pasur4BargServer MonoBehavior instances to the CarBargContainer GameObject and never Destroy any of them. The additional components bloat up the native size for the GameObject (due to serialization data for the component, and the map to its components) and accumulate MonoBehaviour Instances with mostly Native Memory impact upload_2023-9-21_17-47-39.png

    If you wanted these to not be tied to native Memory, don't have Pasur4BargServer inherit from MonoBehaviour.
    Have one managing MB in the scene that has a list of all pure C# classes (no inheriting from UnityEngine.Object, i.e. MonoBehaviour or ScriptableObject) and call Update or whatever you need on these directly from that single MB as it iterates over the List.
    Also make sure that you're not just continuously add new instances without ever getting rid of them again. If they inherit from UnityEngine.Object, then they need to be Destoyed. If they are pure C# classes, just removing them from that list (and any (static) event or other reference) should leave them for the Managed GC to clean up.

    I'd also recommend reading this and these pages from our documentation for further details.
     
  4. MartinTilo

    MartinTilo

    Unity Technologies

    Joined:
    Aug 16, 2017
    Posts:
    2,482
    There is also a bunch of pure C# object instances floating about but they are all held in memory by these MonoBehaviours upload_2023-9-21_18-7-46.png