• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Garbage Collection

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

I doubt on the following concept can anyone help me???
String str="First";
String str="Second";
String str="Third";
4:String str="Four";
AFter code reaches line 4 how many String objects are eligible for Garbage Collection.
Some says that only String objects created with new operator are eligible for GC so here no object will be eligible for GC..Hoz that possible???
HELP???
 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
This code will give compile error. B'cos it is re-declaring String variable str.
If code is like below,
String str="First";
str="Second";
str="Third";
str="Four";
Then after line 4, 3 objects will be eligible for Garbage Collection.
How the string object is created (with new or without new) has no effect on Object eligible for GC.
Hope this is correct.
If anybody is having different opinion, please write.
sujit
 
Ranch Hand
Posts: 396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi sujit,
i don't agree with u. as far as i know objects in string pool are never eligible for garbage collection.
gc is applicable to object heap only.
let's see what others say
regards
deeksha
 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi
i thimk deeksha is right u can not garbage collected string Objects those are not created with new operator
Rishi
 
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Ajith & Eveyone out here:

If code is like below,
String str="First";
str="Second";
str="Third";
str="Four";
Then after line 4, 3 objects will be eligible for Garbage Collection.
Should we conside this as the bottom line:
"How the string object is created (with new or without new) has no effect on Object eligible for GC."
Pls help us out.


------------------
Regards,
Raj.
-------------------------
Afforts should be Appriciated.
-------------------------
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi every one
why string objects are never eligible for garbage collection
please help

Originally posted by Rajpal Kandhari:
Hello Ajith & Eveyone out here:

If code is like below,
String str="First";
str="Second";
str="Third";
str="Four";
Then after line 4, 3 objects will be eligible for Garbage Collection.
Should we conside this as the bottom line:
"How the string object is created (with new or without new) has no effect on Object eligible for GC."
Pls help us out.


 
Sujit Kurtadikar
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One basic question related to the main question.
String s1="Hello";
If there is no previous string "Hello" where will string object (Hello) will be created on heap or stack.
If string object's are never eligile for GC, then don't you think it will eat memory.
So, in my view 3 objecets are candidate for GC after line 4.
sujit
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sujit,
Java uses a seperate area of memory to create a String pool.
If you create a string using a literal

If the string already exists in the pool, Java returns a reference to the existing string. If the string does not exist in the pool, Java creates a new String Object and stores it in the pool.
The string pool is not part of the main memory heap so it is not eligbile for gc.
Hope that helps.
------------------

Jane Griscti
Sun Certified Java 2 Programmer
"When ideas fail, words come in very handy" -- Goethe
 
Sujit Kurtadikar
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Thanks for clarification. But I want to verify this.
How to find the address of Object. Whether hashCode() gives address of Object?
String s1 = "Hello";
s1 = "World";
s1 = "Hello";
as you said, String objects are not eligible for GC, then address of Object refered by s1 and s3 must be same.
Can anybody tell how to verify this ?
sujit
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Sujit,
Yes..u can find it by using hashCode()...
1.String s1 = "Hello";
2.s1 = "World";
3.s1 = "Hello";
if u compile the code u will c the memory address of line 1 and 3 will be same and the memory address of line 2 will be dif.
I have done it.
Hope..its help.
Ratul Banerjee

Originally posted by Sujit Kurtadikar:

Thanks for clarification. But I want to verify this.
How to find the address of Object. Whether hashCode() gives address of Object?
String s1 = "Hello";
s1 = "World";
s1 = "Hello";
as you said, String objects are not eligible for GC, then address of Object refered by s1 and s3 must be same.
Can anybody tell how to verify this ?
sujit


 
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
there is a lot of doubt prevailing in this forum about string objects being thrown in a pool and hence not eligible for gc. i have checked a lto fo stuff on that and found it to be all bull.
yes, a pool is formed... but with objects having some reference! not floating non-referenced string objects.
just remember, any dereferenced object is eligible for gc.
 
Jane Griscti
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I decided to try and get to the bottom of this once and for all so this afternoon I sent an e-mail to Peter van der Linden, author of Just Java 2 and Sun kernal developer, with the following question:
Are de-referenced pooled (interned) Strings available for garbage collection?
He was kind enough to send me a reply ( in under 2hrs! ... boy am I impressed )
Here's his response:

I didn't know the answer, but asking around the right folks:
> Keeping interned Strings around forever is clearly JLS and JVM
spec compliant, so this is an implementation question.
> In most 1.2+ VMs I know of (HotSpot, Classic, EVM, ...) the VM
maintains a table of interned strings. If the last user-program
reference to an interned String disappears the string will get
garbage collected.
> That is, the VM implicitly treats these table pointers as weak
references.
Not sure what you can do with the info, given that it is impl-dependent but it is good to hear that some optimization has been done.
Peter

Bottom line, since it is implementation dependent the questions on the exam won't be that nit-picky and Ashuman is right in stating that, for exam purposes, we should remember that any dereferenced object is eligible for gc

------------------
Jane Griscti
Sun Certified Java 2 Programmer
"When ideas fail, words come in very handy" -- Goethe
reply
    Bookmark Topic Watch Topic
  • New Topic