IL2CPP and garbage collection

Someone told me that IL2CPP will help with garbage collection, is this true? I see on the roadmap that it’s finally coming for standalone/desktop PC. Does this mean the garbage collection woes on desktop are finally over?

I’m not sure exactly how IL2CPP works… so if anyone can enlighten me that would be great – and also, how it relates to garbage collection (if it does)?

:slight_smile:

I don’t believe there’s a significant difference. GC still happens and it doesn’t eliminate the need for defensive programming.

As far as I’m aware there will be no deliberate difference. I’m not super familiar with the intimate details.

For the record garbage collection will never go away. With a better collector we just might get to make it a slightly lower priority in terms of optimisation targets.

That will be coming when the .NET profile gets updated for sure. It’ll result in a more efficient generational version of the GC.

Yeah. My point was a better GC won’t mean garbage collection is no longer a concern. Managing memory is always going to be a performance concern with games.

1 Like

As Dustin-Horne mentioned, IL2CPP does nothing for the garbage collection. By the way, we ship the GC source code that IL2CPP uses with the editor, if you’re curious.

4 Likes

Very cool, I wasn’t aware of that!

You weren’t paying attention! Whole IL2CPP runtime source code is in there, as we compile it when you build your project :).

2 Likes

Once I hit the Build button I consider everything to happen after to be magic. :slight_smile:

8 Likes

Me too, to some degree. I like to understand the magic where I can, and I do understand quite a bit of it, but there’s no way I have the time to be intimately familiar with what Unity is doing for every different build target I’ve used over the years.

That’s why I use Unity in the first place. :wink:

3 Likes

I’m familiar as long as it stays in the .NET stack… once it goes into IL2CPP and C++ it’s (mostly) voodoo to me. I haven’t done anything with C++ for about 15 years.

1 Like

lol Yes. I’m pretty much the same way. I still have a lot to learn :roll_eyes: Also sometimes I’m intimidated to ask what something does or means because I’m afraid it might be a stupid question – and sometimes it is – but I want to learn more stuffs.

Anyways, thanks for the info. I know we’ll still have to manage GC to some extent, but I was just wondering if IL2CPP made it a lot easier. I guess the answer is “no”.

So can I ask a follow up question? What WILL make the GC faster, and when will Unity be getting it? :smile: I see Dustin-Horne said the updating of the “.NET profile” will do this, but what does that mean exactly? I see it on the “Research” portion of the roadmap, so is this what I should be waiting for in terms of faster GC?

The updated .NET Profile comes with a newer version of the Mono runtime. You can get a preview build of that here: https://forum.unity3d.com/threads/upgraded-mono-net-in-editor-and-some-players-on-5-6-0b5.454387/

This does not have a different GC though. We might switch to the Mono GC (SGen) in a later version of Unity. This is a generational GC, which means that many pause times for GC will likely be shorter, but we have not profiled this yet.

In general, the best way to make the GC faster is to avoid allocations when possible. We’re actually focusing a good bit of effort on allocation-free APIs at the moment. Those will be coming in future versions of Unity.

3 Likes

Pooled objects should make their way through Gen1 to Gen2 as well. Any time there are a large number of objects this should make GC more efficient as there will be less objects being considered for collection during normal GC cycles. In some cases it can result in memory taking a bit longer to free up though if you’ve decided to dump some long lived objects. It also doesn’t improve the issue of heap complexity if you have short lived objects that are referencing Gen2 objects, but it’s not quite as common of a scenario.

Don’t be intimidated! Sometimes people (myself included) get a bit snarky when the same questions are asked over and over, but don’t take it personally… there’s no such thing as a stupid question. Everyone has different levels (and types) of experience. If you don’t ask, you won’t learn!

1 Like

Thanks for the info Josh. Yes, the problem I’m having with the GC though is usually not due to my own code, or my own allocations. So that actually has very little to do with most of my GC problems unfortunately. I take precautions (and test) my own code to try to use as little GC as possible. And usually my code runs well and avoids allocations (not saying I’m perfect though of course). My main problem is with third-party assets (mainly from the asset store) that do not do the same, or simply cannot avoid it. Sometimes it’s because of bad coding architecture, and sometimes it’s simply unavoidable and a problem with Unity’s internal garbage collector.

For example, I was using a third-party networking solution that was creating a new array of data when it needed to send data over the network to another player. And it was doing this a lot. So I was going to have to spend the time figuring out which code-piece was doing this, and when you have a highly complex code-base to work with, this is not easy and takes a lot of time.

Another example is an asset I’m dealing with right now that streams in terrain tiles via scenes, and every time the terrain tiles are unloaded, it causes massive GC spikes with GC.MarkDependencies and with Application.LoadLevelAsync integration etc…

Here is a picture of my profiler for this:

Is something like this even avoidable (other than not loading in terrain tiles)? It seems like simply an internal Unity issue… I could be wrong though?

So you can see that often times, it’s third-party solutions that are the problem, but GC is still unavoidable in some cases. Sometimes (perhaps most times) the GC causes me to have to design my game in a completely different way than what I was initially wanting to do – simply to avoid terrible GC spikes.

Amen (I second that) :slight_smile: I understand getting frustrated at newbie questions though, that is understandable. But like you said, if you don’t ask, how can you learn (given it’s not easily available on google of course)? But even then, you’re technically still asking lol.

Anyways, this thread has been informative for me, so thank you :smile:

1 Like

The screenshot with the profiler you showed doesn’t refer to the C# garbage collector. It refers to the asset garbage collector which runs when you load a new scene or call Resources.UnloadUnusedAssets().

Ah I see. So, just the new scene loading causes it. Thank you.

There are and have been lot’s of threads on API calls and Asset Store code that is not optimised for use with the Unity GC this thread is aimed at thinking about the workload the Unity API places on the GC.

A GC has to scan all memory objects for live references so that it can work out if an object can be removed and it’s memory freed up.

Unity has some big and possibly some very GC unfriendly classes.

MonoBehaviour is a key class that we extend nearly all our component code from.

It’s main complexity seems to be it’s Messages section Unity - Scripting API: MonoBehaviour

Note that it’s complex interplay of Start()/Awake() and On() messages can make it complex to use and recycle from a managed pool.

Probably the most complex class from a GC perspective is the Transform class as this is also potentially the root or a branch in the a hierarchy of other transforms.

Note also the tight intertwining of object and component hierarchies, Unity has always had problems with nested prefabs and I wonder if this could be part of the problem.

Also there is the GameObject class which allows you to traverse the component/transform hierarchy and find other components up or down the tree.

So you can infer from this that a component based approach to game development using Unity can quickly become a spaghetti western of references to sub components within hierarchies and across hierarchies.

E.g.
Cowboy has hat, boots, gun (Weapon class), bullets (Damage/Missile class) and is controlled by the Cowboy-Controller.
Indian has feathers, moccasins, bow (Weapon), arrows (Damage/Missile class) and is controlled by the Player.
Both have a hit collider and Health class.
The Cowboy also needs a Sensor (class) and trigger volume to detect the player as a target.

Even in this simple static example (no animation/particle fx or even meta game scripts have been mentioned) you can see a network of connections are needed between components.

So from a GC perspective is this a complex and even memory heavy way to run things?
Or does Unity need a GC that works well with complex component hierarchies?

I’m going to go out on a limb and guess that you have the internal architecture of Unity wrong. And probably that you have your understanding of garbage collection wrong too.