• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Brogden's Garbage Collection?

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the prompt reply
I appreciate it!!
=)
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
>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
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
reply
    Bookmark Topic Watch Topic
  • New Topic