Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Garbage collection when compiling to C

What are the techniques of garbage collection when compiling a garbage collected language to C? I know of two:

  1. maintain a shadow stack that saves all roots explicitly in a data structure

  2. use a conservative garbage collector like Boehm's

The first technique is slow, because you have to maintain the shadow stack. Potentially every time a function is called, you need to save the local variables in a data structure.

The second technique is also slow, and inherently does not reclaim all garbage because of using a conservative garbage collector.

My question is: what is the state of the art of garbage collection when compiling to C. Note that I do not mean a convenient way to do garbage collection when programming in C (this is the goal of Boehm's garbage collector), just a way to do garbage collection when compiling to C.

like image 223
Jules Avatar asked Jan 05 '11 11:01

Jules


People also ask

How does C handle garbage collection?

Garbage collection (GC) is a memory recovery feature built into programming languages such as C# and Java. A GC-enabled programming language includes one or more garbage collectors (GC engines) that automatically free up memory space that has been allocated to objects no longer needed by the program.

Does C use garbage collection?

C does not have automatic garbage collection. If you lose track of an object, you have what is known as a 'memory leak'. The memory will still be allocated to the program as a whole, but nothing will be able to use it if you've lost the last pointer to it. Memory resource management is a key requirement on C programs.

Why C has no garbage collection?

There are two reasons why C / C++ doesn't have garbage collection. It is "culturally inappropriate". The culture of these languages is to leave storage management to the programmer. It would be technically difficult (and expensive) to implement a precise garbage collector for C / C++.

Is garbage collection good in programming?

Any programmer that has had to manually manage memory knows the value of automatic memory management, also known as garbage collection. Garbage collection prevents several potential bugs that occur in manually managed memory, such as dangling pointers, double free errors, and several types of memory leaks.


1 Answers

Potentially every time a function is called, you need to save the local variables in a data structure.

No, you don't - you can leave the local variables on the C stack and still iterate through them: put all reference variables in an array and add a pointer to that to a linked list to which you append a node when entering a new stack frame.

Mockup:

struct vm
{
    struct scope *root;
};

struct scope
{
    struct scope *prev, *next;
    size_t size;
    struct ref *refs;
};

void foo(struct vm *vm, struct scope *caller)
{
    struct ref local_refs[42];
    struct scope scope = {
        caller, NULL, sizeof local_refs / sizeof *local_refs, local_refs };

    caller->next = &scope;

    // ...

    caller->next = NULL;
}

However, you'll have to jump through some major hoops if you want to support continuations/non-local jumps. In that case, it's easier to heap-allocate everything.

like image 200
Christoph Avatar answered Sep 20 '22 09:09

Christoph