What are the roots in garbage collection?
I have read the definition of root as "any reference that you program can access to" and definition of live is that an object that is being used, which can be a local variable, static variable.
I m little confused with discriminating the difference between root and live objects.
What is path to root? How does root and live objects work?
Can someone elaborate ?
A. The root of a number in math is a number that when multiplied by itself produces the original number. For example, the square root of 49 is 7 because 7×7=49. In this case, because 7 is multiplied by itself twice to produce 49, we call 7 the square root of 49. The cube root of 27 is 3, because 3×3×3=27.
The roots of a quadratic equation are the values of the variable that satisfy the equation. They are also known as the "solutions" or "zeros" of the quadratic equation. For example, the roots of the quadratic equation x2 - 7x + 10 = 0 are x = 2 and x = 5 because they satisfy the equation.
If the equation is in the form y = ax^2 + bx +c, simply replace the y with 0. This is done because the roots of the equation are the values where the y axis is equal to 0. For example, suppose the quadratic is 2x^2 - 20x + 5 = 0, where a = 2, b = -20, and c = 5.
The real roots are expressed as real numbers. Suppose ax2 + bx + c = 0 is a quadratic equation and D = b2 – 4ac is the discriminant of the equation such that: If D = 0, then the roots of the equation are real and equal numbers. If D > 0, then the roots are real and unequal.
If you think of the objects in memory as a tree, the "roots" would be the root nodes - every object immediately accessible by your program.
Person p = new Person(); p.car = new Car(RED); p.car.engine = new Engine(); p.car.horn = new AnnoyingHorn();
There are four objects; a person, a red car, its engine and horn. Draw the reference graph:
Person [p] | Car (red) / \ Engine AnnoyingHorn
And you'll end up with Person
at the "root" of the tree. It's live because it's referenced by a local variable, p
, which the program might use at any time to refer to the Person
object. This also goes for the other objects, through p.car
, p.car.engine
, etc.
Since Person
and all other objects recursively connected to it are live, there would be trouble if the GC collected them.
Consider, however, if the following is run after a while:
p.car = new Car(BLUE);
And redraw the graph:
Person [p] | Car (blue) Car (red) / \ Engine AnnoyingHorn
Now the Person
is accessible through p
and the blue car through p.car
, but there is no way the red car or its parts can ever be accessed again - they are not connected to a live root. They can be safely collected.
So it's really a matter of taking every starting point (every local variable, globals, statics, everything in other threads and stack frames) — every root — and recursively following all the references to make up a list of all the "live" objects: objects which are in use and unsuitable for deletion. Everything else is garbage, waiting to be collected.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With