• 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
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

lacal variables availabiliy for GC

 
Ranch Hand
Posts: 516
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
are local variables are available for GC after method returns??
String myMethod(String s){
String s1="hai";
myClass ob=new myClass();
return s+s1;
}
how many objects are available for GC after method returns?.plz explain..
 
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by basha khan:
are local variables are available for GC after method returns??
String myMethod(String s){
String s1="hai";
myClass ob=new myClass();
return s+s1;
}
how many objects are available for GC after method returns?.plz explain..


Local variables themselves are not garbage collected. They are created on the stack, so when your method exists they cease to exist.
However, any objects they reference may still survive the method end, if there is another reference to that object after the method ends.
You are creating 2 new objects in your above method.

myClass ob=new myClass(); //new object of type 'myClass'
return s+s1;v //new String object, concatenation of these two strings.
Note that the statement
String s1="hai";
doesn't create an object,it just places the reference of the string literal "hai" into the variable s1. This string literal was created for you automatically when your class was loaded, and cannot be garbage collected.
When your method ends, the reference variable ob will cease to exist; since there are no other references to the myClass() object you created, it will be elibible for garbage collection.
The new String object however, will not be eligible for gc when the method ends, as you are returning a reference to it to the caller of your method.

Rob
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To Rob, another possibibilty:
The string object returned will be eligable for g.c. if we dont't assign it to any variable:
instance.myMethod(new String("hola"));
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's true. But in looking at the original question, it just asked about eligibilty of gc when the method ends so I had to draw the line somehwere
That's one thing that worries me about taking this test...overthinking the answer and adding something extra in my thought process that isn't explicit in the question. If I have to add conditions to my thinking, such as "well , IF the calling method doesn't do something with the returned reference..." I think I will be in a lot of trouble for the test. So I try to just focus on the immediate code.

Rob
 
basha khan
Ranch Hand
Posts: 516
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thans rob
suppose if i call the method 100 times,100 myClass objects will be there.so none of the objects were not elegible for GC??
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is that a double-negative on purpose?
If you call that method 100 times, ALL 100 of the myClass objects you create will be eligible for garbage collection, because every time the method exits, the reference to that new object is lost.
Rob
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic