Search Unity

DOTS multiplayer and deleting an allocation that is older than its permitted lifetime of 4 frames

Discussion in 'NetCode for ECS' started by daschatten, Oct 14, 2019.

  1. daschatten

    daschatten

    Joined:
    Jul 16, 2015
    Posts:
    208
    This one took me some time to figure out:

    Code (CSharp):
    1. Internal: deleting an allocation that is older than its permitted lifetime of 4 frames (age = 6)
    2. Unity.Collections.LowLevel.Unsafe.UnsafeUtility:Free(Void*, Allocator)
    3. Unity.Entities.EntityCommandBufferData:DestroyConcurrentAccess() (at Library/PackageCache/com.unity.entities@0.1.1-preview/Unity.Entities/EntityCommandBuffer.cs:337)
    The reason i ran into this warning was a change in FixedTimeLoop to gain more performance in editor:

    Code (CSharp):
    1. #if UNITY_EDITOR
    2.     public const float fixedTimeStep = 1f / 15f;
    3.     #else
    4.     public const float fixedTimeStep = 1f / 60f;
    5.     #endif
    What happened? Due to the reduced fixedTimeStep the SimulationSystemGroups (Server + Client) didn't run every frame. This combined with systems running in SimulationSystemGroup using
    BeginSimulationEntityCommandBufferSystem caused the warnings. Example:
    • Frame 1
      • Update InitializationSystemGroup
      • Update SimulationSystemGroup
      • Update SystemX: Writes to BeginSimulationEntityCommandBufferSystem
    • Frame2
      • Update InitializationSystemGroup
      • Skip SimulationSystemGroup
    • Frame3
      • Update InitializationSystemGroup
      • Skip SimulationSystemGroup
    • Frame4
      • Update InitializationSystemGroup
      • Skip SimulationSystemGroup
    • Frame5
      • Update InitializationSystemGroup
      • Skip SimulationSystemGroup
    • Frame6
      • Update InitializationSystemGroup
      • Skip SimulationSystemGroup
      • => Warning appears!
    • Frame 7
      • Update InitializationSystemGroup
      • Update SimulationSystemGroup
      • Update SystemX: Writes to BeginSimulationEntityCommandBufferSystem
    Conclusion:
    If the framerate is much higher than the fixedTimeStep the SimulationSystemGroup will be skipped several frames which may trigger this warning because allocations just become older without being processed.

    Framerate can be much higher if:
    • fixedTimeStep is manually set smaller in FixedTimeLoop
    • VSync is off
    Typical allocations which may cause issues in this case:
    • Systems running in SimulationSystemGroup using BeginSimulationEntityCommandBufferSystem
    • Systems running in Initialization or Presentation groups using *SimulationEntityCommandBufferSystem
    • Systems in Initialization or Presentation allocating but rely on systems in Simulation to dispose
    Especially the combination "System runs in Simulation and uses BeginSimulationEntityCommandBufferSystem" is widely used in my code.

    What can be done to resolve this?
    • Using EndSimulationEntityCommandBufferSystem could cause noticable lags if something visible is spawned, stays at default position, is rendered and moved to its correct position next frame
    • Using BeginInitializationEntityCommandBufferSystem (or other barriers outside Simulation) may cause errors if framerate is lower than fixedTimeStep and SimulationSystemGroup runs multiple times per frame.
    => Unfortunately I have to look at each system and check how to handle its specific use case to avoid this issue.
     
  2. Scorr

    Scorr

    Joined:
    Jul 2, 2013
    Posts:
    73
    I'm running into what I think is the same issue here. Using ECB and updating simulation at a different rate than rendering causes this warning when there are 4 frames or more in between.
     
  3. erenaydin

    erenaydin

    Joined:
    Mar 1, 2011
    Posts:
    384
    You have helped me a lot.

    In my situation, I created a seperate server / client (completely seperate).
    When I run an empty server, I got the same issue and I got 20gb of Editor.log because of those warnings. It not happening on client only NetCode.

    The information gave me an idea to increase Time Scale to 2.6 (from 1), and warnings are gone!
    so now I'm able to test my server. This problem happens on the server only NetCode project with;
    Editor Version: 2020.3.11f1
    Packages;
    upload_2021-6-8_23-40-43.png
    upload_2021-6-8_23-40-57.png
    upload_2021-6-8_23-41-20.png

    Result (server only Netcode project)
    upload_2021-6-8_23-42-3.png
     

    Attached Files:

  4. 8bitgoose

    8bitgoose

    Joined:
    Dec 28, 2014
    Posts:
    448
    This has been an annoyance with the TempJob allocations for years with Unity. They need to add a way to turn off this debug error OR change the duration of the allocation warning.

    If computers get stupidly fast for some reason and start running at 500 fps on older games say in 2025, this warning is going to come out of nowhere on games that have been around for a while even though they were fine when they were first released and could only hit 90fps.

    I also think there needs a general allocator for all jobs that Unity uses internally. So if you are running really slow Unity stuff for whatever reason, you can switch them to use persisent instead of tempjob. Would solve the above thing too. On a asset I am making, the user can set the allocation level depending on how fast it is going to be used.
     
    InabaOrin likes this.
  5. TRS6123

    TRS6123

    Joined:
    May 16, 2015
    Posts:
    246
    If I'm not mistaken, the Unity team knows about this issue and they say the workaround is to enable V-Sync in the game view.
     
  6. 8bitgoose

    8bitgoose

    Joined:
    Dec 28, 2014
    Posts:
    448
    @TRS6123 That works fine for games, but in this case the physics step is being run at 20fps but the game may be run at 120fps. This will throw errors.
     
  7. CMarastoni

    CMarastoni

    Unity Technologies

    Joined:
    Mar 18, 2020
    Posts:
    900
    it is a well know issue with fixed time step loop in general. IIRC some fix in that regards are in progress.
     
  8. 8bitgoose

    8bitgoose

    Joined:
    Dec 28, 2014
    Posts:
    448
    Awesome, good to hear :)
     
  9. pragith

    pragith

    Joined:
    Aug 15, 2021
    Posts:
    2
    help me too
    same issue
     
  10. NikiWalker

    NikiWalker

    Unity Technologies

    Joined:
    May 18, 2021
    Posts:
    316
    FWIW: The current "best fix" is: Inside EntityCommandBufferSystem.CreateCommandBuffer, change the line to:
    Code (CSharp):
    1. var cmds = new EntityCommandBuffer(World.UpdateAllocator.ToAllocator, -1, PlaybackPolicy.SinglePlayback);
    I've tested it: It fixes this issue, but be aware - it may impact performance.
     
  11. CMarastoni

    CMarastoni

    Unity Technologies

    Joined:
    Mar 18, 2020
    Posts:
    900
    The WorldUpdateAllocator is not available in any Entities public releases. It is an internal in-development feature so they can't rely on them.
     
  12. NikiWalker

    NikiWalker

    Unity Technologies

    Joined:
    May 18, 2021
    Posts:
    316
    Ah, true, my bad!
    Allocator.Persistent
    will work in the short-term, with the same performance caveats.
     
  13. Das7Hindernis

    Das7Hindernis

    Joined:
    Jul 26, 2017
    Posts:
    2
    Hi as I understand I only should have this problem if I use some native Arrays oder the EntityCommandBuffer inside an ecs system job.But I've had some working code which I wanted to use unity NetCode. After importing the netcode package I started to get this error. So I emptyed my project to check if some of my code causes the Problem. But it still appeared with "empty" project. Then I started to play arround with Multiplayer-> PlaymodeTools. Like in the Posts above only on the Server Mode it causes this error. In my understanding it seems something to be wrong inside the netcode package ? Am I right ? I am struggeling a while with this error and I am searching for a solution because It causes a hughe performance issue on my machine but I dont understand where to change to "Allocator.Persistent" because the problem occurs even in empty project with netcode package installed.

    It seems it comes from BeginSimulationEntityCommandBufferSystem
    upload_2021-8-27_11-30-11.png
    Kind Regards Das7Hindernis
     
  14. Das7Hindernis

    Das7Hindernis

    Joined:
    Jul 26, 2017
    Posts:
    2
    Found an Issue on the Issuetracker:
    https://issuetracker.unity3d.com/is...-in-play-mode-with-netcode-package-imported-1
     
  15. BrianKesecker

    BrianKesecker

    Joined:
    Feb 5, 2021
    Posts:
    2
    Saw this warning continuously repeating in 2021.2.8f1 and below versions in an HDRP project with volumetric clouds, various lit shader graphs, and some terrain objects present. Enabling "VSync (Game view only)" option under the Game tab stopped the message spam. Running the application after a clean build didn't seem to have issues (no ever-increasing memory or CPU/GPU cycles). In editor the framerate was not exceeding 60 with average hardware. If anyone is still looking for the editor option:

    VSync.png
     
    bb8_1, Boy1nABox and baptistajaden like this.
  16. Occuros

    Occuros

    Joined:
    Sep 4, 2018
    Posts:
    300
    Thank you! that also helped with the latest 0.50.0 release.
     
    BrianKesecker likes this.