In unity an IL2CPP build, is the “.NET semantic runtime” implemented in C++ or still in C#?

how does this embedded runtime maintain managed memory semantics once everything is compiled to native code?

The same way every other .net implementation does it. Objects have a well defined layout that allows the garbage collector to follow references, and there is a list of roots the GC can start tracing from. If the code is compiled just in time or ahead of time does not really make any difference. Reflection will be more difficult to implement in an ahead of time scenario than memory management.

There is nothing stopping you from having GC in ‘native’ languages. You can use automatic memory management in C++, usually in the form of reference counting, or a full garbage collector if you prefer. You can also use manual memory management in C#. But interoperability with libraries is much easier if everyone uses the same type of memory management, so languages tend to mostly stick to one model.

Interoperability is not really a problem with IL2CPP, since code generated from IL will all use the garbage collector, except for the few parts that interact with the rest of the system and need to deal with manual allocations.

Is that runtime itself implemented in C++ (e.g., as part of libil2cpp and an embedded GC), or is it still written in C# and then AOT-compiled?

I have no idea, but does it matter?

Read more here: Source link