Forums Register Login

GC question, OCP book Exam 2 Question 1

+Pie Number of slices to send: Send


In question 1 of OCP Exam 2 one of the sentences to gauge whether there is invalidity or validity states, "If Object X has a reference to Object Y, then Object Y cannot be GCed". I am always confused with garbage-collection because these can not be verified with System.out.println statements.

Does this mean that a reference can point to both ObjectX and Object Y at the same time? Why isnt , "If Object X has a reference to Object Y, then Object Y cannot be GCed" a true statement?

Respectfully,

Ted North - technical skills champion - people skills be damned
+Pie Number of slices to send: Send
Your graph is uncorrect.

The right design is this:

x --> ( OBJ x ) --> ( OBJ y )

This is a code example:



That's why "If Object X has a reference to Object Y, then Object Y cannot be GCed"
+Pie Number of slices to send: Send
 

Ted North wrote:Does this mean that a reference can point to both ObjectX and Object Y at the same time?


No. In general, when we say that an object "has" something, we mean that something is a member of the class. An instance of that class (object X) will thus keep alive a reference to the something (object y) as long as the object X instance is alive and kicking (unless/until that reference is made to point to a different object Y). Here, it means that object X is an instance of a class which has object Y as an instance field.

Why isnt , "If Object X has a reference to Object Y, then Object Y cannot be GCed" a true statement?


It could be the case that Object X is itself unreachable. In that case, and assuming there is no other way to reach object Y, then both objects can be garbage collected.
+Pie Number of slices to send: Send
 

Dennis Deems wrote:

Ted North wrote:Does this mean that a reference can point to both ObjectX and Object Y at the same time?


Here, it means that object X is an instance of a class which has object Y as an instance field.




----

Thanks Dennis Deems, I can understand this.
1
+Pie Number of slices to send: Send
Maybe another angle:



In this case class Car "has-a" Engine.
+Pie Number of slices to send: Send
 

Bert Bates wrote:Maybe another angle:



In this case class Car "has-a" Engine.



So, when the class Car is nulled any reference to Engine will also be lost? Did I understand this correctly?



A comment from Bert Bates is the muffin on top of this java thread. Thank-you for taking the time to respond Bert.

Respectfully,

TN - javanaut
+Pie Number of slices to send: Send
 

Ted North wrote:
So, when the class Car is nulled any reference to Engine will also be lost? Did I understand this correctly?


that is depends on relationship between Car and the Engine. Car has engine via dependence injection? if no, yes when car die, then engine die, else engine may be used by some one..

Engine:I give you engine, you use..no problem you die thats your business...

Engine: I am fully depends on this guy Car, if he destroyed, that is equal to I died....

got Ted?
+Pie Number of slices to send: Send
@Ted:your picture uploads are attractive
Honk if you love justice! And honk twice for tiny ads!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1420 times.
Similar Threads
illegal array reference assignments
string pool literals are not garbase collected ??
Garbage collection-Marcus exam
how many objects eligible for GC?
RHE QUESTION - stack
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 12:25:45.