aspose file tools*
The moose likes Beginning Java and the fly likes Garbage Collection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Garbage Collection" Watch "Garbage Collection" New topic
Author

Garbage Collection

Thomas Markl
Ranch Hand

Joined: Mar 08, 2001
Posts: 192
Hello,
How many Strings are eligable for Garbage Collection. Which String and when are they eligable?
Thanks for your answers.
String string1 = "Test";
String string2 = "Today";
string1 = null;
string1 = string2;
A) 1
B) 2
C) 3
D) 0
Shilpa Bhargava
Ranch Hand

Joined: Sep 15, 2002
Posts: 39
I think neither of the strings are ready for garbage collection since they still hold references.
Please correct me if I am wrong
[ September 19, 2002: Message edited by: Shilpa Bhargava ]
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1510
The answer is A. i.e the string object "Test" is available for GC. Note that it's the string object that's GC'ed and not the references.


Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
But "Test" is a string literal. It will never be garbage-collected.


Ron Newman - SCJP 1.2 (100%, 7 August 2002)
William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 986

I say none also. Everything is still in scope.


Please ignore post, I have no idea what I am talking about.
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
Please see this link.
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Quote: I say none also. Everything is still in scope.
When the only reference to the "Test" String object was removed (changed to refer to null), scope had little to do with the situation. Were these regular objects, then the "Test" String object would indeed be eligible for garbage collection. As Ron alluded to, and as is pointed out in the conversation linked to by Anthony, the memory model used for Strings is a bit different than that used for other Java objects.
[ September 20, 2002: Message edited by: Dirk Schreckmann ]

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 986

Yes the link is helpful.
But I don't know if it actually clears up all my confusion.
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
William,
If you'd like to get a better idea of how object referencing works, take a look at the nice flash animation that Corey McGlone put together. It can be accessed from http://www.geocities.com/mcglonec1978/javacert/javacert.html .
Otherwise, if you have any specific (or vague) questions, just ask.
William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 986

Yes that helped thanks.
(It acts like a pointer. Even though Java doesn't have pointers.)
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
You can think of a reference variable in Java as a pointer.
The difference between Java references and C/C++ pointers is that there is no such thing as pointer arithmetic in Java, and there is no "&" operator to generate pointers to local variables.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Garbage Collection