Page 1 of 1

About Question enthuware.ocajp.i.v7.2.923 :

Posted: Wed Aug 29, 2012 11:42 am
by Javanaut
Is Pm not eligible for GC because it is a local or automatic variable and lives on the stack instead of the heap? I got this right but am still confused about this.

Re: About Question enthuware.ocajp.i.v7.2.923 :

Posted: Wed Aug 29, 2012 11:58 am
by admin
pM is a reference. references are never garbage collected. The objects to which they point to are garbage collected (depending on their eligibility in terms of how many references are pointing to those objects).

In this case, the object pointed to by pM has other references that are pointing to that object. So even if you make pM point to null, the object will not be eligible for GC.

Also, Objects always reside in the heap and never on the stack. References may be on the heap (if they are within an object, which is on the heap) or on the stack (such as local variables).

HTH,
Paul.

Re: About Question enthuware.ocajp.i.v7.2.923 :

Posted: Tue Jun 17, 2014 1:31 pm
by Shortrope
Thanks Paul, I get it now, pM is just a reference to the same object m is referencing. Only pM is set to null. m is not changed.

I am still fuzzy on garbage collection with objects within Objects.

When N is instantiated it creates an instance of M as a private member.
What if n is set to null: n = null;
I would assume the object that was referenced by n is now eligible for gc and therefore m would be thrown in the garbage when n is gc'ed. Is this true? Or does m keep it ineligible?

Re: About Question enthuware.ocajp.i.v7.2.923 :

Posted: Tue Jun 17, 2014 8:43 pm
by admin
Shortrope wrote: I am still fuzzy on garbage collection with objects within Objects.

When N is instantiated it creates an instance of M as a private member.
What if n is set to null: n = null;
I would assume the object that was referenced by n is now eligible for gc and therefore m would be thrown in the garbage when n is gc'ed. Is this true? Or does m keep it ineligible?
Yes, if there is no other reference to the contained object, then that will be garbage collected as well. You may want to go through a book for this topic or check out this online article: http://javarevisited.blogspot.in/2011/0 ... -java.html

HTH,
Paul.

Re: About Question enthuware.ocajp.i.v7.2.923 :

Posted: Thu Feb 19, 2015 8:49 am
by Sergiy Romankov
I believe that the second option: a call to n.makeThisNull() marks the private instance of m for garbage collection - is true.
Because object of Type M has only one reference - m,
when we call n.makeThisNull() method it calls a method makeItNull() and take reference m as parameter inside makeItNull() m assingns null.
So there is no more references to object and it is eligible for garbage collection?
Please explain why it is not true?

Re: About Question enthuware.ocajp.i.v7.2.923 :

Posted: Thu Feb 19, 2015 9:11 pm
by admin
When makeThisNull calls makeItNull, it passes a copy of the reference m. Not m itself. It is that copy which is set to null. m remains as it is.

Your confusion stems from a fundamental misunderstanding about how references are passed in java. You should go through this article http://www.javaranch.com/campfire/StoryPassBy.jsp to understand this concept. Once you do that you will realize why option 2 is incorrect.

Re: About Question enthuware.ocajp.i.v7.2.923 :

Posted: Wed Jun 22, 2016 8:52 am
by wuqing1450
Here is my understanding.

1. N n = new N()
reference n ==> a instance of N
reference m ==> a instance of M

2. n.makeThisNull(); makeItNull(m);
reference n ==> a instance of N
reference m, pM ==> a instance of M

3. pM = null;
reference n ==> a instance of N
reference m ==> a instance of M
reference pM ==> null

Re: About Question enthuware.ocajp.i.v7.2.923 :

Posted: Thu Nov 29, 2018 8:04 pm
by OCAJO1
If
class N{ private M m = new M();
public void makeItNull(M pM){
pM = null;
}

was changed to

class N{ M pM;
private M m = new M();
public void makeItNull(M pM){
this.pM = null;
}

then the answer would change, would it not?

Re: About Question enthuware.ocajp.i.v7.2.923 :

Posted: Thu Nov 29, 2018 9:16 pm
by admin
Yes, that will change the answer.

Re: About Question enthuware.ocajp.i.v7.2.923 :

Posted: Thu Jan 03, 2019 3:32 pm
by crazymind
Hi,what will happen to those objects (still reachable) when the program end? Are they garbage collected or destroyed?

Re: About Question enthuware.ocajp.i.v7.2.923 :

Posted: Sat Jan 05, 2019 12:25 am
by admin
Once a program ends, the operating system reclaims all memory that it gave to the JVM. Garbage collection is not required here because all of the memory is reclaimed by the OS.

Remember that garbage collection of any object is never guaranteed. So, you can never rely on when will be performed and whether an object will actually be destroyed. If a program doesn't need much memory, it is possible that the JVM will never even perform garbage collection during the execution of the program.