• 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
  • Ron McLeod
  • Junilu Lacar
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • Carey Brown
  • Stephan van Hulst
Bartenders:
  • Frits Walraven
  • fred rosenberger
  • salvin francis

Doubt over Garbage Collection

 
Ranch Hand
Posts: 65
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Exam Lab for SCJP 5 Question 19


1. class A{
2.
3. A a1;
4. A a2;
5.
6. public void finalize(){
7. System.out.println("-");
8. }
9.
10. public static void main(String args[]){
11. A s1=new A();
12. s1.a1=new A();
13. s1.a2=s1;
14. s1.a1.a2=new A();
15. s1.a1.a2.a2=new A();
16. s1.a1.a2.a2.a1=s1.a1;
17. s1.a2.a1.a2=null;
18. System.gc();
19. }
20.
21. }

How many Objects are eligible for GC after Line 17. As mentioned in the ans I tried to draw graph which is attached with this question.
First of all is this graph correct?


Secondly if it is then am I correct then the object we are making null is shown in yellow color and Objects that are not reachable are shown by red arrow. T two objects eligible for gc are s1.a1.a2.a2 And s1.a1.a2.a2.a2
I know its a very long question but please can anyone tell me is it correct or not?





 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

My 2 cents .. i hope others could correct me if i'm mistaken

I think your graph is great !


T two objects eligible for gc are s1.a1.a2.a2 And s1.a1.a2.a2.a2



I think the objects eligible for garbage collection are :
s1.a1.a2.a2 (same with your answer) and ..
s1.a1.a2.
In this case, both of these objects are still connected, but they are consideres an island of unreferenced objects, thus being eligible for garbage collection ..

Cheers,
Albert Kam
 
Sheriff
Posts: 9683
42
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
Jeena your graph was great, but references don't point to other references, they point to objects, so precisely this is the correct memory representation of the question. There will be 2 objects eligible of GC as they are in a island of isolation as mentioned by Albert

memory-map.jpg
[Thumbnail for memory-map.jpg]
 
Jeena Jeen
Ranch Hand
Posts: 65
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you guys for the reply. And thanks Ankit for pointing out the mistake in graph.
I understand why the two objects mentioned by Albert are eligible for gc.
I think s1.a1.a2.a2.a2 is not eligible for gc because it was never created as a new Object.
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Thank you guys for the reply. And thanks Ankit for pointing out the mistake in graph.
I understand why the two objects mentioned by Albert are eligible for gc.
I think s1.a1.a2.a2.a2 is not eligible for gc because it was never created as a new Object.



I agree. but...

I think...The object referenced by "s1.a1.a2.a2.a1" still remains valid because still referenced by "s1.a1"...so, only one object is elegible for GC.

correct?

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

As pointed by Albert,
the two objects referenced by reference variables s1.a1.a2 AND s1.a1.a2.a2 are eligible for GC.

Anand
 
Ranch Hand
Posts: 453
Google Web Toolkit Hibernate Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

I guess i am really confused.

According to me 4 objects are created but none are eligible for gc.please let me know where i am wrong.
untitled.GIF
[Thumbnail for untitled.GIF]
 
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In line 15, a brand new object is created. In line 17, the only reference to that object is assigned null.
The only other thing that happens in the meantime is that on line 16 the same object that was just created
gets an assignment of another object, that has a valid reference elsewhere, to one of its fields. So
it seems clear to me that only one object is eligible for gc. Am I wrong?
 
this llama doesn't want your drama, he just wants this tiny ad for his mama
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic