I'm working on a problem similar to that of garbage collection, where I need to be able to notify every object pointing to a particular object, should that object be moved elsewhere.
What are the pros a开发者_Go百科nd cons of just maintaining a list of all inbound references to an object? Is this used in garbage collection algorithms? If not, why not?
The standard copying garbage collectors uses different technique to handle object relocations. As it is traversing the object graph and copying objects, the first time it reaches an object via a some pointer it:
- Copies the object to the new address (ex: address A).
- Updates the pointer.
- Mark the original object to say "moved to address A"
Now, every other time the GC reaches that object via a pointer, it'll see the "moved to address A" mark and update the pointer.
Adapting this to your use case, I think this would mean that objects would point to a relay pointer. If you move the actual object, update the relay pointer with the new address. With this technique, each access to an object has an extra step (to read the relay object) and moving the object is O(1). The extra space used is O(number-of-objects).
With the incoming-pointers technique, each access has no overhead and moving the object is O(incoming-pointers). The extra space used is O(number-of-pointers).
精彩评论