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

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
Javanaut

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

Post 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.

admin
Site Admin
Posts: 10053
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

Shortrope
Posts: 15
Joined: Sun Jun 01, 2014 8:27 pm
Contact:

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

Post 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?

admin
Site Admin
Posts: 10053
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

Sergiy Romankov
Posts: 31
Joined: Thu Feb 19, 2015 8:25 am
Contact:

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

Post 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?

admin
Site Admin
Posts: 10053
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

wuqing1450
Posts: 8
Joined: Thu Mar 24, 2016 12:02 pm
Contact:

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

Post 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

OCAJO1
Posts: 221
Joined: Mon Nov 26, 2018 2:43 pm
Contact:

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

Post 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?

admin
Site Admin
Posts: 10053
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

Yes, that will change the answer.
If you like our products and services, please help us by posting your review here.

crazymind
Posts: 85
Joined: Mon Dec 24, 2018 6:24 pm
Contact:

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

Post by crazymind »

Hi,what will happen to those objects (still reachable) when the program end? Are they garbage collected or destroyed?

admin
Site Admin
Posts: 10053
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 38 guests