This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Brogden's 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 "Brogden Watch "Brogden New topic
Author

Brogden's Garbage Collection?

Edmund Chiang
Greenhorn

Joined: Apr 11, 2001
Posts: 17
On page 155 the code goes as follows:
1)String str = new String(Size =" + x);
2)System.out.println(str);
3)str = null; //string object eligible for garbage collection after line 3 is executed

Middle of page:
"Normally all objects created for local variables in a method becomes eligible for garbage collection when method exits"
So I'm left with 2 conflicting statements:
1) objects are eligible for garbage collection after its declared null
2) objects are eligible for garbage collection when the method returns
So am I misreading this chapter?
Can someone please shed some light?
Thanks in Advance!
Edmund
alpa urja
Greenhorn

Joined: Dec 27, 2000
Posts: 17

1) objects are eligible for garbage collection after its declared null
2) objects are eligible for garbage collection when the method returns
Both the probabilities are correct because,if before the method gets over or returns,if the object is assigned a null value(and if there are no more references to that object)then it is eligible for garbage collection.The rule(2) is ok generally,that is if the object is not assigned a null value.
Just understand,that the objects created in the method are local to that method,so are eligible for garbage collection once the method is over or is returned.
Edmund Chiang
Greenhorn

Joined: Apr 11, 2001
Posts: 17
Thanks for the prompt reply
I appreciate it!!
=)
Leonardo Silva
Greenhorn

Joined: Apr 03, 2001
Posts: 13
>2) objects are eligible for garbage collection when the method returns
>Just understand,that the objects created in the method are local to that method,
>so are eligible for garbage collection once the method is over or is returned.
Humm.. but alpa...
RHE pg 178 says "An object created inside a method is likely to outlive the method invocation."
After that it says that the final variables of the method (constants) are copyed to the object just created and then
it(object) can access them(variables).
Considering this the objects are not destroyed.
Correct me if I am wrong

Leonardo Silva
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Hi Leonardo,
In normal circumstances, objects created in a method will be eligible for garbage collection once the method ends. However, if a new object is created in a method and a reference to the object is returned to the calling method, the object will not be eligible for garbage collection until some later point in the program.
For example,

The same would apply if the Object was added to a class variable.
Bottom line, as long as at least one active reference to the object exists, it will not be eligible for gc
Hope that helps.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform


Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Brogden's Garbage Collection?
 
Similar Threads
garbage collection!!
GC , thread
q on garbage collection
Garbage Collection
Dan exam doubt 19