• 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:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

string doubt

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From the following code how many objects are eligible for garbage collection?
String string1 = "Test";
String string2 = "Today";
string1 = null;
string1 = string2;
A) 1
B) 2
C) 3
D) 0
Here No object is created only string literal are there so answer would be 0 as string one made are not garbage collected unless they are made throw new String("test") way.
Please explain and correct me
thanks in advance
 
author and deputy
Posts: 3150
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
The answer i believe is "one" object ["Test"] is available for collection at the line string1=null.
because the "Test" object is no longer required to be referenced by a String.But the object "Today" is required by string1 and string2.
Regardds
Balaji
 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
There is a conflict in my mind too regarding the difference b/w String literal & String object.But i always consider the String literal as an object created in a pool and succeed in solving mocks.

Two objets are created in a pool one at Line 1 & second at Line 2
At Line 3 string1 is made null which means tha it lost the reference of object created at Line 1(i.e Test).This is the only object which is available for GC.
Well, you may refer RHE for this topic.
Regards,
Hassan.
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ya Payal you absolutely correct because there is no object is create only when u create string using new String that will create String object so answer is 0

 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
I'm not so sure about literal string being GC'ed.
Please consider the following code:

After compiling, I run "javap -c X", and get the following result:
0 ldc #1 (String "abcd")
2 astore_1
3 ldc #2 (String "xyz")
5 astore_2
6 aconst_null
7 astore_1
8 ldc #1 (String "abcd")
10 astore_2
11 ldc #2 (String "xyz")
13 astore_1
14 return
From that result, I think that literal strings are created once only by the compiler, and any further assignments of any literal string will be referring to the same reference in memory.
This happens even if we have set s1 to null.
That's why I'm not so sure that string literals can be GC'ed.
Quoting JLS 3.10.5:
The result of explicitly interning a computed string is the same string as any pre-existing literal string with the same contents.
Wouldn't this mean that JVM doesn't do any GC on literal strings? so that any string assignments which include intern() method can therefore search any pre-existing literal string?
please correct me if I'm wrong..
- eric
 
Ranch Hand
Posts: 1157
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I think the answer should be one in this case.The String literal "Test" would be eligible for garbage collection.
It doesn't mean it would be garbage collected immediately; it will be *marked* for garbage collection.This happens invariably when the reference of the String is marked as null or made to point to another String literal.
Hope this helps,
Sandeep
SCJP2,OCSD(Oracle JDeveloper),OCED(Oracle Internet Platform)
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everyone
Just to throw in my 2 cents...
There are no actual objects being created with new so there are no objects to be gc'd. I dont think that string literals are actual objects and even if they are I think that they are created on the heap and dont get gc'd until the JVM exits. So the actual answer would be D: 0 objects.
Dave
 
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
String literals are different from String objects.
String literals are not GCed.
Strings created with new operator are only GCed.
Otherwise a new String which had resulted using String methods such as concatenation, toUpperCase,toLowerCase,Changing a character in a particular position etc results in a new string and they can be GCed.
This is the summary of String GC.
HTH
any comments please
 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So what the answer is.....0
We cannot take a poll here as it is not a democracy....
Thanx
Rajani
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From RHE, 2nd Edition, page 256:
"Every string literal is represented internally by an instance of String. Java classes may have a pool of such strings. When a literal is compiled, the compiler adds an appropriate string to the pool. However, if the same literal already appeared as a literal elsewhere in the class, then it is already represented in the pool. The compiler does not create a new copy; instead, it uses the existing one from the pool."
So references to the string literals are maintained by the pool and so those literals would not become eligible for garbage collection.
April
[This message has been edited by April.Johnson (edited July 26, 2001).]
 
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I just wanted to share my view with all of you folks.
String comparsions had always been a issue . Not to forget
many threads had come and gone here in javaranch
In this example
String string1 = "Test";
Behind the Scene:
Compiler looks for this String litreal "Test" as its first
priority. If its not there in the pool, the implicit nature of the String litreal kicks in, which is nuting but an String instansiation. So, an instance named "Test" gets created.
But the string formation from the program came as litreal form of string creation ie "Test" not new String ("Test")... the created string remains as litreal not as an independent object.
As long as it remains in the pool, as litreal, it wont be GC'ed
Becoz the main intention of pooling will fail.
Thankx
 
Desai Sandeep
Ranch Hand
Posts: 1157
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I believe, when you say :

String s = "Test"; //Line 1
String s1 = new String("Test"); //Line 2

Here 2 objects (you may also call it literals in String case..it doesn't matter) gets created.
In this case, s1 is not going to point to the same "Test" which already exists at the execution of Line 1.Infact at Line 2 a new String literal "Test" is created.
Now when you say :

s = null;

then the reference of s to "Test" (created at Line 1) is lost and hence "Test" String literal (which is nothing but an Object) becomes eligible for Garbage Collection.
Hope this makes sense
Sandeep
SCJP2,OCSD(Oracle JDeveloper),OCED(Oracle Internet Platform)
 
Mini Pilla
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Even I think that "Test" is eligible for GC but why that concept is so misunderstood???
Thanx
Rajani
 
Ragu Sivaraman
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Few earlier threads regarding this issue http://www.javaranch.com/ubb/Forum34/HTML/000315.html http://www.javaranch.com/ubb/Forum1/HTML/000166.html
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys,
String literals are not gc'd in the current JDK releases; nor does the JLS mandate them to be gc'd. It's very unlikely you will get a gc question involving String literals on the real exam as the gc behaviour is system-dependent. The exam only tests on system-independent behaviours.
You will see numerous gc questions in mock exams that involve String literals. For the purpose of answering them; assume String literals are gc'd.
Hope that helps.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
[This message has been edited by Jane Griscti (edited July 26, 2001).]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic