garbage collection - How does Boehm GC work for C program? -
i checked boehm gc. gc c/c++.
i know mark-and-sweep algorithm. i'm in curious how picks pointers in whole c memory. understanding c memory plain byte array. possible determine value in memory pointer or not?
the boehm gc conservative collector, means assumes pointer. means can find false positive references, integer coincidentally has value of address in heap. result, blocks may stay in memory longer non-conservative collector.
here's description boehm's page:
the garbage collector uses modified mark-sweep algorithm. conceptually operates in 4 phases, performed part of memory allocation:
- preparation each object has associated mark bit. clear mark bits, indicating objects potentially unreachable.
- mark phase marks objects can reachable via chains of pointers variables. collector has no real information location of pointer variables in heap, views static data areas, stacks , registers potentially containing pointers. bit patterns represent addresses inside heap objects managed collector viewed pointers. unless client program has made heap object layout information available collector, heap objects found reachable variables again scanned similarly.
- sweep phase scans heap inaccessible, , hence unmarked, objects, , returns them appropriate free list reuse. not separate phase; in non incremental mode operation performed on demand during allocation discovers empty free list. sweep phase unlikely touch page not have been touched shortly thereafter anyway.
- finalization phase unreachable objects had been registered finalization enqueued finalization outside collector.
you should know boehm gc needs given set of "roots", starting points mark-and-sweep algorithm. stack , registers automatically roots. need explicitly add global pointers roots.
edit: in comments, concerns pointed out conservative collectors in general. true integers heap pointers collector can cause memory not released. not big of problem might think. scalar integers in program used counts , sizes , small (so not heap pointers). run problems arrays containing bitmaps, strings, floating point data, or of sort. boehm gc lets allocate block gc_malloc_atomic
indicates collector block not contain pointers. if in gc_typed.h, find ways specify parts of block may contain pointers.
that said, fundamental limitation of conservative collector cannot safely move memory around during collection since pointer rewriting not safe. means won't of benefits of compaction lowered fragmentation , improved cache performance.
Comments
Post a Comment