• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

doubt on garbage collection

 
shreya prabhu
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
12. void doStuff3() {
13. X x = new X();
14. X y = doStuff(x);
15. y = null;
16. x = null;
17. }
18. X doStuff(X mx) {
19. return doStuff2(mx);
20. }//here answer is not sure when object created at line 13 is garbage collected but outside the method we have made the references null so why is it not garbage collected?
 
sadasiva kumar
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An object becomes eligible for Garbage Collections when its last live reference disappears.
1.The reference goes out of scope,permanently.
2.The reference is assigned another object.
3.The reference is explicitly set to null.

If you are declaring reference as null outside method ,it is not eligible for Garbage Collection because reference dies at end of method.

regards
SADASIVAKUMAR UTTI
SCJP1.4
 
karnatam narendraprasad
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi archan,


can i have complete code.
 
sushil bhogale
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Garbage control is under the conrol of JVM. The JVM decided when to run GC. So, though have made references NULL, its not necessarily that GC has been performed on them. Experinece suggest that it runs only when JVM senses that memory is running low.

Also, if you are writing one stand-alone code and running from the command prompt, do not expect to get the output as GC runs in other JVM than your program.


JUST FYI....

Among plenty of the garbase collector's algorithm, following are the two important once -
1. mark and sweep &
2. reference counting.
But Java specification does not gurantee any particular implementation. Its important to understand that any object becomes eligible for garbage collection when NO LIVE thread can access it. Object can be accessed by any thread only if object is reachable i.e. it should have some reachable reference.

Please write a finalize() method and invole GC.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you post the missing code, please tell us where the mock question comes from. Thanks!
 
Bert Bates
author
Sheriff
Posts: 8900
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think we're circling around the right answer
 
shreya prabhu
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this question is from kathy sierra gabage collection questions at the end of the chapter..
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, but I think we need to see the missing code in order to provide an answer.
 
JB Ramesh
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
----------------------------------------
12. void doStuff3() {
13. X x = new X();
14. X y = doStuff(x);
15. y = null;
16. x = null;
17. }
18. X doStuff(X mx) {
19. return doStuff2(mx);
20. }//here answer is not sure when object created at line 13 is garbage collected but outside the method we have made the references null so why is it not garbage collected?
-------------------------------------------

Yes. The answer is "not sure when object created at line 13 is garbage collected" because we dont know about doStuff2() method. And refernce 'mx' is passed into that method. within that method that reference may or may not be retained. So answer is right.
 
Guru dhaasan
Ranch Hand
Posts: 126
Java Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you are marking the object as null, you are recommending the GC to clear that. It is just a suggestion and you cant put any constraint on that
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic