The sweep pass usually also does compaction, as described above. First, the amount of work in the mark pass is proportional to the amount of non -garbage. Thus if memory is nearly full, it will do a lot of work with very little payoff. Second, the mark phase does a lot of jumping around in memory, which is bad for virtual memory systems, as we will soon see. The third approach to garbage collection is called generational collection.
Memory is divided into spaces .
When a space is chosen for garbage collection, all subsequent references to objects in that space cause the object to be copied to a new space. In other words, an object that has survived for a while is likely to live a lot longer. By carefully choosing where to move objects when they are referenced, we can arrange to have some spaces filled only with long-lived objects, which are very unlikely to become garbage. We garbage-collect these spaces seldom if ever. When all else fails, allocate simply fails.