File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes GC- String Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "GC- String" Watch "GC- String" New topic
Author

GC- String

Shilpi M Ag
Ranch Hand

Joined: May 07, 2004
Posts: 40

at which line is the first string (myString) eligible for GC?

And does the answer change if the code is

Geoffrey Vlassaks
Greenhorn

Joined: May 12, 2004
Posts: 24
Hi,

After line 4, the String "Batman" will be eligible for GC. After line 6, myString still refers to "Robin", so this is not eligible for GC.

The second code block has te same result..

greetz,
Swamy Nathan
Ranch Hand

Joined: May 16, 2004
Posts: 187
First eg:
If you mean batman he disappears(becomes eligible for GC) at line 4.
If you mean his sidekick he satys on.

Second eg:
They all live forever in the "intern" String pool that floats around in memory till the JVM is up and running ur code.

To tell the truth I am not sure because I have a few doubts. Namely 2 doubts.

doubt 1(for eg 1). We all know that if the pool had batman already the new batman would not go into the pool unless he had been specifically intrned. Is there any possiblity that if batman was not there in the pool previously creating a new batman would send him to the pool? I hope not.

doubt 2(for eg 2). Do String pool members ever get GC'd before shutdown?
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Originally posted by Swamy Nathan:
First eg:
doubt 1(for eg 1). We all know that if the pool had batman already the new batman would not go into the pool unless he had been specifically intrned. Is there any possiblity that if batman was not there in the pool previously creating a new batman would send him to the pool? I hope not.

doubt 2(for eg 2). Do String pool members ever get GC'd before shutdown?


First of all, keep in mind that this information is not on the SCJP exam.

As for the String literal pool, the references that exist there (and the objects they refer to) are created when the class is loaded. I believe that these objects remain in existence until the JVM shuts down.

Any time you use a String literal that is already a member of the String literal pool, you'll get a reference to the existing object. If you create a String with the same contents as a String in the pool without using a literal, you will get a reference to a new object on the heap, not the one referenced from the String literal pool. You can then invoke the intern() method on that String to get a reference to the String referenced from the String literal pool and make the new String available for garbage collection.


SCJP Tipline, etc.
Swamy Nathan
Ranch Hand

Joined: May 16, 2004
Posts: 187
I guess that closes the issue.
If Corey believes something he is probably right. He may not guarantee it but he is still probably right. I am sure. We can depend on his beliefs because it indicates a learning from experience versus just reading it up somewhere and digging out obscure info from a book or elsewhere. (Not kidding. No sarcasm here.)

I guess Shilpi was able to make sense of this.
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Originally posted by Swamy Nathan:
[QB]I guess that closes the issue.
If Corey believes something he is probably right. He may not guarantee it but he is still probably right. I am sure. We can depend on his beliefs because it indicates a learning from experience versus just reading it up somewhere and digging out obscure info from a book or elsewhere. (Not kidding. No sarcasm here.)
[QB]


Thanks for the compliment, Swamy, but don't take what I say as gospel (or your holy book of choice ). I'm just another guy and I can be just as wrong as any other guy. If I say something stupid (I've been known to do so at times), don't be afraid to call me on it. I'm no smarter than anyone else, I've just been hanging around these forums a while.
Shilpi M Ag
Ranch Hand

Joined: May 07, 2004
Posts: 40
In both the cases, "mystring" is eligible for garbage collection at line 4... Is this correct?
Tejas Bavishi
Ranch Hand

Joined: Jul 28, 2003
Posts: 73
Hi

Though this is not related to SCJP exam, you can use JVM options like -verbose:gc and than the JVM will print all kinds of statistic about the garbage collection.

To learn about the GC for SCJP exam, use Kathy&Bert's book, it is explained very nicely.

Thanks
Tejas
Swamy Nathan
Ranch Hand

Joined: May 16, 2004
Posts: 187


In both the cases, "mystring" is eligible for garbage collection at line 4... Is this correct?



No No Shilps.


In second case the strings are from the pool and the pool members are immortal. They last as long as the program is running (we have Corey's word for it). So we can say in the sencond case "Batman forever"




At the risk of kicking myself let me say something in Hindi to u Shilpi.
Hope u find it funny and dont get annoyed.




Yaar puuri Ramayan sunaa di. And u still have doubtbs on whether Sita was Rama's wife or Ravana's



Shilpi M Ag we are all learning I guess and to learn properly one must keep the ego aside and dare to ask like u do.
[ June 11, 2004: Message edited by: Swamy Nathan ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: GC- String