• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Garbage Collection

 
Thomas Markl
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1511
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ron Newman
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But "Test" is a string literal. It will never be garbage-collected.
 
William Barnes
Ranch Hand
Posts: 986
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I say none also. Everything is still in scope.
 
Anthony Villanueva
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please see this link.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
William Barnes
Ranch Hand
Posts: 986
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes the link is helpful.
But I don't know if it actually clears up all my confusion.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 986
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes that helped thanks.
(It acts like a pointer. Even though Java doesn't have pointers.)
 
Ron Newman
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic