b*******n 发帖数: 8 | 1 The following code creates one Point object and one Rectangle object. How many
references to those objects exist after the code executes? Is either object
eligible for garbage collection?
...
Point point = new Point(2,4);
Rectangle rectangle = new Rectangle(point, 20, 20);
point = null;
...
My answer: only one object exists after the code executes. point object is
eligible for garbage collection.
Is my answer right? | w*r 发帖数: 2421 | 2 nah.. before the GC, theere were 2 objcts, one is the instance of Point
another is the instance of Rect, however, the pint references to null, if
there were not other references to the Point object, that object is eligible
for GC.
many
【在 b*******n 的大作中提到】 : The following code creates one Point object and one Rectangle object. How many : references to those objects exist after the code executes? Is either object : eligible for garbage collection? : ... : Point point = new Point(2,4); : Rectangle rectangle = new Rectangle(point, 20, 20); : point = null; : ... : My answer: only one object exists after the code executes. point object is : eligible for garbage collection.
| B******N 发帖数: 445 | 3 after this code get execute, how many objects eligible for garbage collection
is depending on context.
Let's say if no other object hold reference "point" and "rectangle", then both
eligible for GC. If something hold it like array or vector, then it will not
be GC. IN either case "point = null" makes no difference. So you can remove
it. It's different as c++ "delete".
example:
...
Point point = new Point(2,4);
Rectangle rectangle = new Rectangle(point, 20, 20);
point = null;
mVector.add(point)
.
【在 b*******n 的大作中提到】 : The following code creates one Point object and one Rectangle object. How many : references to those objects exist after the code executes? Is either object : eligible for garbage collection? : ... : Point point = new Point(2,4); : Rectangle rectangle = new Rectangle(point, 20, 20); : point = null; : ... : My answer: only one object exists after the code executes. point object is : eligible for garbage collection.
| B******N 发帖数: 445 | 4 sorry! wrong logic.
I really means this:
...
Point point = new Point(2,4);
Rectangle rectangle = new Rectangle(point, 20, 20);
mVector.add(point):
point = null;
....
in this case point = null dosn't make sure point is GC.
in the original post the "point" is GCed, because the "rectangel" is not
really hold the "point" reference, it just get it's x and y value. It's
different as you put it in an array or vector. Whether the "rectangle" GC
depends on Context.
collection
both
object
【在 B******N 的大作中提到】 : after this code get execute, how many objects eligible for garbage collection : is depending on context. : Let's say if no other object hold reference "point" and "rectangle", then both : eligible for GC. If something hold it like array or vector, then it will not : be GC. IN either case "point = null" makes no difference. So you can remove : it. It's different as c++ "delete". : example: : ... : Point point = new Point(2,4); : Rectangle rectangle = new Rectangle(point, 20, 20);
| B******N 发帖数: 445 | 5 my brain messed up, damn.
I mean GC is eligible for Garbage Collected. Not real time GC. sorry again.
not
remove
this
How
【在 B******N 的大作中提到】 : sorry! wrong logic. : I really means this: : ... : Point point = new Point(2,4); : Rectangle rectangle = new Rectangle(point, 20, 20); : mVector.add(point): : point = null; : .... : in this case point = null dosn't make sure point is GC. : in the original post the "point" is GCed, because the "rectangel" is not
|
|