2 ms·
Copying is faster if the memory to be collected is mostly garbage. You only copy the good stuff and don't even look at the garbage. Many programs (particularly
by skybrian 14y ago
Copying is faster if the memory to be collected is mostly garbage. You only copy the good stuff and don't even look at the garbage. Many programs (particularly in scripting languages) allocate memory but only use it for a short time, so they create lots of garbage.
Often this is combined with generational garbage collection - new objects live for a short time in a temporary area, and then if they are still in use (not garbage), they get copied to longer-term storage.
- jeffdavis 14y agoCopying also means moving to new addresses. What strategies are used to update the referencing pointers to the new location?
- to3m 14y agoThe pointers are updated as you copy them. (If you know the types ahead of time, you therefore structure objects so that they consist of runs of pointers and runs of non-pointers.) Wikipedia has a reasonable explanation of the basic algorithm: http://en.wikipedia.org/wiki/Cheneys_algorithm http://en.wikipedia.org/wiki/Cheneys_algorithm It probably gets more complicated if you try to share it with a non-copying collector, but the basic algorithm is very straightforward to implement.