Statistik
Statistik

Reputation: 31

How are memory allocations handled in "bytecode based" languages?

As far as I understand any program written in let's say c# the source is first compiled to an intermediate language, which is then jitted by the clr to corresponding machine instructions.

But what exactly happens if I e.g. create a new array of size N: Is the very allocation done on machine instruction level, or does the clr allocate that memory on the fly (and thus has possibilities to manage it) and passes the memory adress further down?

I am asking because I want to understand how the garbage collector keeps track of all the references when references could be created on machine instruction level.

Upvotes: 0

Views: 51

Answers (1)

omajid
omajid

Reputation: 15203

From the introduction of the Book of the (.NET) Runtime itself:

More important is the simple requirement it places on the runtime itself:

Garbage collection requires ALL references to the GC heap to be tracked.

In other words, every memory allocation that's done by the runtime - single bytes, arrays, objects and anything else - needs to be tracked so the garbage collection can continue to work correctly.

If memory was allocated (through what you call "machine instruction level", or through calling third party native code that the runtime has no insight into) the memory can no longer be tracked by the runtime and then the GC wouldn't be able to operate on it correctly.

Upvotes: 0

Related Questions