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

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

Moderator: admin

Post Reply
assembler07
Posts: 1
Joined: Tue Feb 16, 2016 4:16 pm
Contact:

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

Post by assembler07 »

An object should be eligible for garbage collection only when his reference becomes pointing to null ?

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

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

Post by admin »

In general, if there are no references pointing to an object then it is eligible to be garbage collected. But it is a bit more complicated than that. Please go through the following links to understand better:
http://beginnersbook.com/2013/04/java-g ... ollection/

http://www.oracle.com/webfolder/technet ... index.html
If you like our products and services, please help us by posting your review here.

Rohit Singhania
Posts: 3
Joined: Tue Dec 05, 2017 3:34 am
Contact:

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

Post by Rohit Singhania »

Yes. The process is called as Garbage Collection. In this process compiler identifies the objects which are in use and which are not, and deletes the unreferenced objects that is no longer referenced by any part of program.

I would like to suggest refer below resource to understand garbage collection better,

https://www.flowerbrackets.com/garbage- ... n-in-java/
Last edited by Rohit Singhania on Thu Aug 01, 2019 3:52 am, edited 2 times in total.

herbertscbr
Posts: 7
Joined: Wed Aug 22, 2018 9:57 am
Contact:

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

Post by herbertscbr »

After line 10? Why not in line 10?

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

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

Post by admin »

Only when the execution of the line is completed, can its effect be seen. So, the object can't be GCed, while line 10 is being executed. Thus, "after" and not "in".
If you like our products and services, please help us by posting your review here.

herbertscbr
Posts: 7
Joined: Wed Aug 22, 2018 9:57 am
Contact:

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

Post by herbertscbr »

Sorry, but and the line 6?
(I understand what you said about line 10.. )

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

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

Post by admin »

I am not sure what is your question about line 6. Can you be a bit more clear?
If you like our products and services, please help us by posting your review here.

herbertscbr
Posts: 7
Joined: Wed Aug 22, 2018 9:57 am
Contact:

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

Post by herbertscbr »

In the following code, after which statement (earliest), the object originally held in s, may be garbage collected ?

1. public class TestClass{
2. public static void main (String args[]){
3. Student s = new Student("Vaishali", "930012");
4. s.grade();
5. System.out.println(s.getName());
6. s = null;
7. s = new Student("Vaishali", "930012");
8. s.grade();
9. System.out.println(s.getName());
10 s = null;
}
}


6. s = null; //right answer
10 s = null;//to me, this answer is correct too

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

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

Post by admin »

The problem statement is asking about "object originally held in s". This object is eligible for gc after 6.
If you like our products and services, please help us by posting your review here.

reekjohns
Posts: 1
Joined: Tue Oct 01, 2019 11:18 pm
Contact:

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

Post by reekjohns »

Generally, an object becomes eligible for garbage collection in Java on following cases:

* Any instances that cannot be reached by a live thread.
* Circularly referenced instances that cannot be reached by any other instances.
* If an object has only lived weak references via WeakHashMap it will be eligible for garbage collection.
* The object is created inside a block and reference goes out scope once control exit that block.

Post Reply

Who is online

Users browsing this forum: Google [Bot] and 44 guests