I’m reading about the C# garbage collector, and how the CLR builds object graphs.
The chapter references different roots that could be active for the object:
• References to global objects (though these are not allowed in C#, CIL
code does permit allocation of global objects)
• References to any
static objects/static fields
• References to local objects within an
application’s code base
• References to object parameters passed
into a method
• References to objects waiting to be finalized
(described later in this chapter)
• Any CPU register that references
an object
I was wondering if someone could give examples of these roots in code?
Thanks
Assume you run the following program:
When the program breaks into the debugger, there are 3+ objects that are not eligible for garbage collection because of the following references:
Class1object referenced by the static fieldfoostring[]object referenced by the parameterargsstringobjects referenced by thestring[]object referenced byargsClass2object referenced by the local variablebarThe
Class3object is eligible for garbage collection and may already have been collected or be waiting to be finalized.References to global objects are not allowed in C#.
References in CPU registers are an implementation detail of the VM.