Question

In Java, say that we are using a Mark and Sweep garbage collector. During the Mark phase, the collector does a DFS and then mark all the live objects live, then when it comes to the sweep phase, it passes through all the objects and reclaim memory of those object not marked live (That's what I learnt about how MS GC works). Say that if I only start from a half of the root, then there will be some objects not reachable during the mark phase, then they are not being marked live, when it comes to the sweep phase, will those un-marked objects being GCed or the GC doesn't know how to deal with them, which means we cannot start GC from half of the root? and why?

Was it helpful?

Solution

Say that if I only start from a half of the root,

You'd better don't say this. Don't even think about it (see below).

then there will be some objects not reachable during the mark phase,

They're reachable, but they won't be reached by the algorithm. Too bad.

then they are not being marked live,

Too bad.

when it comes to the sweep phase, will those un-marked objects being GCed

If there were any GC ignoring half of the root, then yes.

or the GC doesn't know how to deal with them, which means we cannot start GC from half of the root?

Exactly. All the objects marked as live will be saved in a survival area. What could you do about those not marked when the only reason for GC is to reclaim memory?

What memory can you reclaim when you have objects which are surely alive and objects which are in an unknown state?

NONE

That's the problem. You'd like to save work by not traversing some objects, but in this case the half-done job is worth zero.

It's OK to let live some objects longer than necessary, but not the other way round.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top