About Question enthuware.ocajp.i.v7.2.922 :
Moderator: admin
-
- Posts: 1
- Joined: Tue Feb 16, 2016 4:16 pm
- Contact:
About Question enthuware.ocajp.i.v7.2.922 :
An object should be eligible for garbage collection only when his reference becomes pointing to null ?
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.922 :
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
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.
-
- Posts: 3
- Joined: Tue Dec 05, 2017 3:34 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.922 :
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/
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.
-
- Posts: 7
- Joined: Wed Aug 22, 2018 9:57 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.922 :
After line 10? Why not in line 10?
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.922 :
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.
-
- Posts: 7
- Joined: Wed Aug 22, 2018 9:57 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.922 :
Sorry, but and the line 6?
(I understand what you said about line 10.. )
(I understand what you said about line 10.. )
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.922 :
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.
-
- Posts: 7
- Joined: Wed Aug 22, 2018 9:57 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.922 :
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
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
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.922 :
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.
-
- Posts: 1
- Joined: Tue Oct 01, 2019 11:18 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.922 :
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.
* 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.
Who is online
Users browsing this forum: No registered users and 81 guests