• 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

new object in method

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I am reading the SCJP 6.0 Certification book from Kerry and Kathy and I have a doubt I can't clarify with the book info. I googled but I didn't found any info about that. My question is about the JVM garbage collector.

For example, if I have a method like this:

public void getData() {

StringBuilder sb = new StringBuilder();

}

The book explains this: "local variables are in the stack and instance variables / objects in the heap. When a method finalizes and returns: local variables are eligible for garbage collection", but my question is: what about the object instantiated in the method ? Is automatically eligible for garbage collection also ?

if I have the same example but in a for statement like this:

for (int i=0; i <1000; i++) {
StringBuilder sb = new StringBuilder();
}

StringBuilder object is elegible for garbage collection when the for loop finish ?

Thanks
Chris
 
Ranch Hand
Posts: 357
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes
 
Bartender
Posts: 1638
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not a performance question. Moving to SCJP certification forum.
Please CarefullyChooseOneForum while posting.
 
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Objects created with local variables are always eligible for garbage collection unless the reference to that object is passed outside the scope of that variable.


In the above case, the scope of s is the method body only. But as this method returns the object reference in a return statement, the object will not eligible for
GC after the execution comes out of the method body.


But in the above code, the object refernced by s will be eligible for GC once the execution comes out of the method body as this object is not referenced furthur.

Hope this helps.
 
Ranch Hand
Posts: 528
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank You Very Much Rajsekhar it is really valuable for me
 
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I had seen a question in Devaks's Simulator. dont remember the number.

It was adding an object into a collection(Vector), so its not eleigible for GC. But what if this collection is declared inside a method?
 
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
then collection object itself is eligible for gc after the end of that method.
 
See where your hand is? Not there. It's next to this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic