Forums Register Login
Garbage Collection
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
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 ]
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.
But "Test" is a string literal. It will never be garbage-collected.
I say none also. Everything is still in scope.
Please see this link.
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 ]
Yes the link is helpful.
But I don't know if it actually clears up all my confusion.
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.
Yes that helped thanks.
(It acts like a pointer. Even though Java doesn't have pointers.)
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.
CAUTION! Do not touch the blades on your neck propeller while they are active. Tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database

This thread has been viewed 585 times.

All times above are in ranch (not your local) time.
The current ranch time is
Feb 23, 2019 06:41:26.