How to store references in a mark and sweep garbage collector?
I started writing my own scripting language over the most recent weekend for both the learning experience and for my resume when I graduate high school. So far things have gone great, I can parse variables with basic types (null, boolean, number, and string) and mathematical expressions with operator precedence, and have a rudimentary mark and sweep garbage collector in place (after completing the mark/sweep collector I will implement a generational garbage collector, I know naive mark/sweep isn't very fast). I am unsure how to store the referenced objects for the garbage collector, though. As of now I have a class GCObject that stores a pointer to the it's memory and whether it is marked or not. Should I store a linked list to it's referenced objects in the class? I have looked at garbage collectors from other languages but I see no linked lists of references per GCObject, so it is confusing me.
TLDR: How do I store objects that are referenced by other objects in a mark and sweep garbage collector? Do I just store linked lists of objects in all my GCObjects?
Thanks guys.
You generally don't store the references to an object in anything but the locations at which those references naturally occur. During the mark operation, you don't need to know which references point to an object; rather, you need to know which references an object (or root) contains, so you can recursively mark those objects.
You also need, for the sweep phase, a way to iterate through all objects so you can finalise any unreferenced objects and return their storage to the allocation pool. How you would do this exactly depends on your general purpose allocator - you probably want to write a custom one.
(I'm assuming you don't want to do compaction - that's a whole lot more complicated).
链接地址: http://www.djcxy.com/p/79098.html上一篇: 压缩发生后GC如何更新引用