How does (dynamic) memory go through the garbage collector?

I read about the different types of GC and how they work. They all cover a memory set that could potentially be fixed, but none of what I read provides any guidance on how this is actually done. Could something like the following be off the mark?

void* myAlloc(size_t size)
{
    if (needToGc())
        gc();
    void* obj = malloc(size);
    if (!obj)
        outOfMemory(); // or maybe GC and try once more
    *alloced = obj;
    *alloced = *alloced + 1;
    return obj;
}
void gc()
{
    // go through memory pointed to in `alloced`
}

I suspect so, but this is the only thing that comes to mind ...

+3
source share
1 answer

Garbage collectors have a lot of variety, depending on requirements (memory overhead, latency, data location, ...). Some of my answers may not apply to some complex collectors.

-, needToGc: , malloc .

, , , , , . , , . :

  • . , - , .

  • , .

  • , , , , .

, , . , , .

mark-and-sweep. , 0. , : 1, ; 0, 1 , . . , : - 0, ; reset 0.

"--sweep", .

. wikipedia .

+1

Source: https://habr.com/ru/post/1758885/


All Articles