Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice

Mono Upgrade (1162701) Consistent Memory Leak on .NET 4.x Runtime Only for WebGL Build

Discussion in 'Experimental Scripting Previews' started by JJJohan, Jun 18, 2019.

  1. JJJohan

    JJJohan

    Joined:
    Mar 18, 2016
    Posts:
    214
    We are currently evaluating upgrading our existing project from the now-deprecated 3.5 equivalent runtime to the new runtime as part of our move to the 2018.4 LTS. The application we develop is used as a visualisation service for geospatial data and deals with constant runtime data streaming and quite a number of different data formats.

    Unfortunately we've found there is a very consistent memory leak of managed memory that occurs only in the new runtime and I've been able to track it down to the presence of circular references in the code.

    For example, ClassA knows about ClassB and ClassB knows about ClassA. This accounts for both direct and indirect circular references and unfortunately it isn't possible for us to guarantee there are no circular dependencies because our Unity project codebase is used as an API of sorts for other internal teams.

    I've been able to produce a very small repro project and have submitted it as part of a bug report (Case 1162701). The odd thing is that building a Windows IL2CPP or Mono build does not suffer from the leak, only the WebGL build output. I was contemplating posting about it in the WebGL subforum however it seems to be specific to the runtime.

    I guess the point of the thread is more as a question, are there any known functional changes to how the garbage collector operates between the old and the new runtime? I fear that despite it being replicated easily with a few lines of code it'll be a difficult fix hence why I'm looking for some input on any developers that may be familiar with the changes or might have any idea why something like this could be happening.

    Sadly there is no improvement when trying builds on Unity 2019.1, 2019.2 or even 2019.3.
     
  2. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,938
    I'm not aware of any specific changes to the GC in the .NET 4.x equivalent scripting runtime that would cause this to happen. I suspect to have uncovered a bug. We will investigate it, thanks!
     
    JJJohan likes this.
  3. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,938
  4. JJJohan

    JJJohan

    Joined:
    Mar 18, 2016
    Posts:
    214
    Thanks for the update Josh. A shame that issue is a year old but I can imagine this is not an easy bug to fix.
     
  5. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,938
    Yes, this is a fun one!
     
  6. JJJohan

    JJJohan

    Joined:
    Mar 18, 2016
    Posts:
    214
    I couldn't help but notice a couple of branches on the Unity mono and bdwgc repositories being merged related to this issue. I'd be quite happy to offer to test any potential work-in-progress solutions, even if it means trialing in a 2019.3 beta or 2020.1 alpha. We've got a department here that's really keen on sticking to a .NET 4.x codebase for external projects used in our Unity codebase.
     
  7. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,938
    Yes, fixes for this issue for IL2CPP and Mono scripting backends are on the way to releases in the 2020.x series. I'll let you know when they land so that you can give them a try. Are you interested in the Mono or IL2CPP fixes (or both)?
     
    pwroames likes this.
  8. JJJohan

    JJJohan

    Joined:
    Mar 18, 2016
    Posts:
    214
    Thanks Josh. I'm primarily after the IL2CPP fixes myself, targeting WebGL. Our Windows builds aren't targeting 32-bit so they've not been affected.
     
    pwroames likes this.
  9. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,938
    The fixes for IL2CPP are now in 2020.1.0a8 (the Mono fixes landed in 2020.1.0a7). Once the a8 release is available and you get a chance to try this, please let me know how it goes. Thanks!
     
    JJJohan likes this.
  10. JJJohan

    JJJohan

    Joined:
    Mar 18, 2016
    Posts:
    214
    I can confirm that as of 2020.1.0a8 our WebGL builds no longer leak any memory. Fantastic work guys!

    I see from the issue tracker that this is also planned for 2019.3. Are there any further plans to backport this to 2018.4 LTS as well? If not, I guess we'll probably move to 2019.3 or 2019.4 when it's available.
     
  11. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,938
    It looks like the issue track is incorrect here - we're probably not going to back port this change to 2019.3, as it is a somewhat risky change to the GC algorithm.

    We're determining why the issue tracker shows it as under review for 2019.3.
     
  12. DeltaPiSystems

    DeltaPiSystems

    Joined:
    Jul 26, 2019
    Posts:
    30
    I can also confirm that this has fixed my memory leak issues in WebGL. It's much better in a lot of areas now!