• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Question about Garbage collection from ExamLab diagnostic Test

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

How many objects will be eligible for garbage collection after executing line 14. Please provide solution with explanation.
 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Puja, welcome to javaranch.

Can you tell us which line is confusing you?? Usually you should try to prepare a memory map that represents the objects created by the program.




For example the memory map of the program at line 12 would look like this

 
PujaR Agarwal
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ankit,


I too made a memory map diagram for the same and I conclude that because of a2.a=null and a1=a3 just one object that is a2 is eligible for garbage collection because as a1 and a3 are still being referenced so they are not eligible . am I understanding it correctly?


Thanks
Puja
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i think one object will be eligible for GC. .......also a1, a2, a3 are references not objects ...so you cannot say

a2 is eligible for garbage collection

. Please correct me if i am wrong.
 
PujaR Agarwal
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks

but I am still not sure for the correct explanation. Can anybody give the correct solution with explanation ?



 
Ankit Garg
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you continue with my diagram, then after line 14, the memory map would look like this

 
PujaR Agarwal
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello Ankit,

Can you please tell more specifically which object is eligible for garbage collection and why because by diagram I am able to understand that one object is there which is eligible for it but which one . I guessed is a2. Please clear it.


Thanks
Puja
 
deepesh soni
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
On line 10
A a2=new A(new A(null));
the object which is created by calling "new A(null)" does not have any reference. So this object will be garbage collected.
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes only one object is eligible for garbage collection and thats the one referenced by instance member a of object a2 which is afterwards made to null.
 
PujaR Agarwal
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks all.

This is another example of garbage collection from ExamLab

Class A{
A aob;
public static void main(String args[]){
A a=new A();
A b=new A();
A c=new C();
A.aob=b;
b.aob=a;
c.aob=a.aob;
A d=new A().aob=new(A);
c=b;
c.aob=null;
system.gc();
}
}

How many objects are eligible for garbage collection and which are they ?


I tried to solve this question and conclude that object c and d will be eligible for garbage collection. Please correct me if I am wrong.

Thanks
Puja
 
deepesh soni
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
IMO Only the object initially referenced by c will be garbage collected.
 
PujaR Agarwal
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
but examlab answer is 2 objects are eligible.
 
Sheriff
Posts: 7136
1360
IntelliJ IDE jQuery Eclipse IDE Postgres Database Tomcat Server Chrome Google App Engine
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

IMO Only the object initially referenced by c will be garbage collected.

but examlab answer is 2 objects are eligible.



For this second question, there are 2 objects eligible. It was previously discussed.
The first question was discussed here.

Devaka.
 
Devaka Cooray
Sheriff
Posts: 7136
1360
IntelliJ IDE jQuery Eclipse IDE Postgres Database Tomcat Server Chrome Google App Engine
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

PujaR Agarwal wrote:This is another example of garbage collection from ExamLab


Please start a new topic for new questions, that would be helpful to both you and others.
 
PujaR Agarwal
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks all for helping me.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic