File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes doubt on garbage collection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "doubt on garbage collection" Watch "doubt on garbage collection" New topic
Author

doubt on garbage collection

shreya prabhu
Ranch Hand

Joined: Feb 10, 2007
Posts: 31
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

Joined: Oct 30, 2003
Posts: 91
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


SADASIVAKUMAR UTTI, SCJP1.4
A bend in the road is not the end of the road ... unless you fail to make the turn.
karnatam narendraprasad
Ranch Hand

Joined: Mar 01, 2007
Posts: 30
hi archan,


can i have complete code.
sushil bhogale
Greenhorn

Joined: Mar 13, 2007
Posts: 8
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

Joined: Aug 31, 2004
Posts: 11343

When you post the missing code, please tell us where the mock question comes from. Thanks!


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8805
    
    5
I think we're circling around the right answer


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
shreya prabhu
Ranch Hand

Joined: Feb 10, 2007
Posts: 31
this question is from kathy sierra gabage collection questions at the end of the chapter..
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Okay, but I think we need to see the missing code in order to provide an answer.
JB Ramesh
Greenhorn

Joined: Jan 09, 2007
Posts: 20
----------------------------------------
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

Joined: Sep 13, 2006
Posts: 126

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


Thanks, Shiv
SCJP, OCE - JSP & Servlets Developer
 
wood burning stoves
 
subject: doubt on garbage collection
 
Similar Threads
garbage collection
Clarify Question
Garbage Collection
Garbage Collector
Garbage Collector question