Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do garbage collectors know about references on the stack frame?

What techniques do modern garbage collectors (as in CLR, JVM) use to tell which heap objects are referenced from the stack?

Specifically how can a VM work back from knowing where the stack starts to interpreting all local references to heap objects?

like image 628
chillitom Avatar asked May 29 '12 08:05

chillitom


People also ask

Is garbage collection based on reference?

The main concept that garbage collection algorithms rely on is the concept of reference. Within the context of memory management, an object is said to reference another object if the former has access to the latter (either implicitly or explicitly).

How does garbage collector know which objects to free?

When the garbage collector performs a collection, it releases the memory for objects that are no longer being used by the application. It determines which objects are no longer being used by examining the application's roots.

Does garbage collector work on stack memory?

Garbage Collection runs on the heap memory to free the memory used by objects that don't have any reference.

How does garbage collector remove unused references?

During the garbage collection process, the collector scans different parts of the heap, looking for objects that are no longer in use. If an object no longer has any references to it from elsewhere in the application, the collector removes the object, freeing up memory in the heap.


1 Answers

In Java (and likely in the CLR although I know its internals less well), the bytecode is typed with object vs primitive information. As a result, there are data structures in the bytecode that describe which variables in each stack frame are objects and which are primitives. When the GC needs to scan the root set, it uses these StackMapTables to differentiate between references and non-references.

CLR and Java have to have some mechanism like this because they are exact collectors. There are conservative collectors like the boehm collector that treat every offset on the stack as a possible pointer. They look to see if the value (when treated as a pointer) is an offset into the heap, and if so, they mark it as alive.

like image 75
Matt Avatar answered Oct 23 '22 05:10

Matt