Based on 'Boehm-Demers-Weiser Garbage Collector'.
Generally roots
mean pointers stored in statically allocated or stack allocated program variables.
A mark-sweep garbage collector traverses all reachable objects in the heap (more accurately it's gc heap in our case) by following pointers beginning with the roots
.
In this project roots
include followings,
- (Add here)
- (Add here)
(TODO)
...
It is usually best not to mix garbage-collected allocation with the system malloc-free. If you do, you need to be careful not to store pointers to the garbage-collected heap in memory allocated with the system malloc.
You need to be careful using std::vector since it allocates memory for its elements internally.
It allocates based on the allocator you pass in at construction. If you didn't specify one, you get the default allocator and it will allocate on the native heap!
So it is needed to specify GC allocator for the std::vector with elements that may have GC pointers.
To elaborate, at mark phase, GC start to scan pointer-like values and checks if the values belong to the GC heap. But if we allocate on the native heap using std allocator, the ranges are out of GC heap range, so GC ignores the values.
It is not recommended to use std::vector, because its std::vector::end
points next space of the last element which makes GC think it as a non-collectable space!
Please refer here.