OCPJP 6.0
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
OCPJP 6.0
Rohit Ramachandran wrote:
When line 0 executes,
---------ob1--------ob2---------ob3
gb1->grab(){g->grab(){g->grab(){g->null}}}
gb1.g---------------^
gb1.g.g---------------------------^
gb1.g.g.g------------------------------------^
-------------------------------------------ob4---------ob5
So when line1 executes- gb1.g.g->grab(){g->grab(){g->null}}, when gb1.g.g points to a new object, ob3 has nothing accessing it.
When line2 executes-
---------ob6--------ob7----------ob8
gb2->grab(){g->grab(){g->grab(){g->gb1}}}
gb2.g----------------^
gb2.g.g-----------------------^
gb2.g.g.g-> the same object pointed by gb1;
When line3 executes-
gb2.g-> the same object pointed by gb1.g, ob7 has nothing accessing it.
Totally 8 objects are created.
I think 2 objects ob3 and ob7 are eligible for Garbage collection. Please let me know if I'm wrong.
OCPJP 6.0
OCPJP 6.0
Saumya Srivastava wrote:Whether gb1.g.g.g will also be eligible to GC? As it is pointing to null..? I am not sure
Rohit Ramachandran wrote:Yes yes yes.
3 Objects eligible for garbage collection. As I've mentioned ob3 and ob7. Since ob8 can be reached only through an ob7.g, since ob7 can't be accessed even ob8 can't be accessed.
Hence 3 objects eligible are ob3,ob7 and ob8. You should understand it now. If you don't read it and try.
OCPJP 6.0
OCPJP 6.0
Saumya Srivastava wrote:I m sorry..I don't know what is the purpose of that... how it came and what it means...Do you know?
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Thanks & Regards
Sumit Kothalikar
Dude, I don't get it. How will anything be eligible for garbage collection at line 3?
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Ankit Garg wrote:If it helps, I draw diagrams like this for GC questions (objects marked as GC in the diagram are eligible for garbage collection)
OCPJP 6.0
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime. |