• 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

Garbage Collection

 
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Source: Enthuware


Answer: Objects passed to the method are never garbage collected in that method. So p cannot be GCed.
x is not an object.
"abc" is a string literal which goes to the string pool and is not GCed.
So, only a is eligible for GC.

1> even after the method returns the string literal isn't GCed?
2> what if i explicitly assign null to str: str=null; ?
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sutaria,

The reason why strings are not GCed is because, String will be handled in a different way than all the other Objects. Even though String IS-A Object, the memory handling of Strings are different. This is called String Pool. Before even creating a new String Literal say for example
"Sutaria" in the pool, it will check for the existance of the string. If it can't find one then it will create. So lets come your question

1) Even after the method returns, the string literal won't be GCed.

This is because the String pooling

2) If you create String s=null;
This will not create a String Object at all (unless you mention String s = "null" .This is because you have just given a reference 's' and not actually created an Object.

Hope this clarifies your doubt...
 
Milan Sutaria
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Justin,
i meant adding a line to the end of the code: str=null;
 
Ranch Hand
Posts: 141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Milan Sutaria:
Justin,
i meant adding a line to the end of the code: str=null;




Literal's will only be GCed if the class is unloaded from the JVM. A way to make it GCed is initializing it with new String("") like this..:


With this code you'll have two objects to be GCed.
 
Milan Sutaria
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks Raphael
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic