Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unity's garbage collector - Why non-generational and non-compacting?

I've just read in Unity's docs that

Unity’s garbage collection – which uses the Boehm GC algorithm – is non-generational and non-compacting. “Non-generational” means that the GC must sweep through the entire heap when performing a collection pass, and its performance therefore degrades as the heap expands. “Non-compacting” means that objects in memory are not relocated in order to close gaps between objects.

Does anyone of You knows or assumes why Unity aborts using standard .Net GC with generations and compacting? I made some tests and i'm really surprised that even objects from LOH are in generation 0 and probably GC's trying to collect them with small objects.

like image 913
Smith Avatar asked Oct 04 '17 21:10

Smith


People also ask

What are generations and how are they used by the garbage collector?

Collecting a generation means collecting objects in that generation and all its younger generations. A generation 2 garbage collection is also known as a full garbage collection, because it reclaims objects in all generations (that is, all objects in the managed heap).

What is garbage collection Why do we use compaction?

When the garbage has been removed from the heap, the Garbage Collector can consider compacting the resulting set of objects to remove the spaces that are between them. The process of compaction is complicated because, if any object is moved, the GC must change all the references that exist to it.

What are generations in garbage collector Gen 0 1 and 2 )?

Generation 0 identifies a newly created object that has never been marked for collection. Generation 1 identifies an object that has survived a GC (marked for collection but not removed because there was sufficient heap space) Generation 2 identifies an object that has survived more than one sweep of the GC.

What are generations in garbage collector C#?

C# garbage collection belongs to the tracing variety. It's often called a generational approach since it employs the concept of generations to figure out which objects are eligible for collection. Memory is divided into spaces called generations. The collector starts claiming objects in the youngest generation.


1 Answers

You paid attention to the details, so here comes a story of the details you deserve.

It was in early 2008 that Unity and Mono announced their collaboration, and at that time Unity licensed the Mono runtime (GPL covered for open source usage) so as to embed it. And the Boehm GC was the primary GC in Mono then.

Time passed and Mono 4.x/5.x by default uses SGen GC with generational/compacting features. However, Unity did not want to pay the licensing again. Thus, you see the documentation remains it was.

Microsoft acquired Xamarin in 2016, and hence gained control of Mono core assets. It republished the code base under MIT, so solving the licensing issue for ever. Unity joined .NET Foundation and started to work with Microsoft/Xamarin to incorporate the latest Mono runtime into the game engine.

That effort is still undergoing and should soon reach maturity (currently an experimental feature).

BTW, Unity cannot use the standard .NET GC yet. Microsoft does not open source its GC in .NET Framework, but a version in .NET Core. That GC is different from Mono's, and would require more efforts to be embedded into Unity. I guess that's why Mono 5 was chosen to be integrated right now. Maybe in the future Unity would migrate to the .NET Core GC.

Some of the events can be found in the .NET timeline.

like image 121
Lex Li Avatar answered Sep 21 '22 17:09

Lex Li