Win a copy of Rust Web Development this week in the Other Languages forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Reg. GC from JavaCaps

 
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have two questions regarding GC:
1. You can call the garbage collector whenever you want to.( True/False )

When we invoke System.gc() , does it mean we are calling the
garbage collector whenever we want to.


The correct answer is 1.
Is is because the String2 value "Today" is lost after line 4.
Can someone explain it to me in better terms.
thanks
 
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Is it that in line 3, the "Test" object is lost so it is eligible for gc, and in line 4 the reference string1 will point to the same object as reference string2?
I dont't think that "Today" will be lost.
Just an idea,
Cristian
 
Ranch Hand
Posts: 282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Angela Narain:
I have two questions regarding GC:
1. You can call the garbage collector whenever you want to.( True/False )

When we invoke System.gc() , does it mean we are calling the
garbage collector whenever we want to.


False. (No you can not call garbage collector on your wish. when you invoke System.gc(), you are just 'requesting' JVM for the calling Garbage Collector but cann't make/force JVM to do so).
Remember, GC is the lowest priority thread for JVM.

Originally posted by Angela Narain:
[B]


The correct answer is 1.
Is is because the String2 value "Today" is lost after line 4.
Can someone explain it to me in better terms.
thanks [/B]


This is because in line no 3 you are assigning null to String1, making one String object ("Test") elligible for garbage collection as it is not referenced any more.
(But even though if line no 3 wouldn't have been there, answer would have been same as in line 4 you are assigning String2 to String1..making "Test" elligible for Garbage Collection).
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree with you but since the String object are created using String literals and not dynamic object allocation, the String should be kept in the private String pool of the class and not garbage collected. Is this behaviour compiler-specific ? Keeping String literals in the pool is not a requirement, but just a possible optimization, right ??
Val
 
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hai Angela,
String Literals are not GCed.
Sun Exam wont be testing u like this. Look for objects that are created with new operator, String concatenation etc for GC questions.
HTH
tvs sundaram
SCJP
 
Valentin Crettaz
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was almost sure that String literals were not gced. Thanks TVS you made my mind clear !!
Val
 
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does it mean that no objects will be gc'ed in the above example. Can somebody tell me whether String litertal is object or not.
--Farooq.
 
Valentin Crettaz
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are two main ways of creating String instances:
1. Create a String instance using a String literal in which case the private String pool of the class will be asked whether it already contains such a String. If it contains one then it is returned and affected to the variable. If not a new is created and added to the pool
String s = "String1";
2. Allocate it dynamically
String s = new String("String1");
in which case a new String instance will be created in any case

The first way is provided for optimization purposes, in order to avoid creating multiple identical String instances. Since a String is immutable, there is no way one could be modified, so why not using an existing one ?
To sum up, in the example above, no String instance should be gced. Anyone correct me if I'm wrong !
Val
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
...In other words...The answer is Zero.
Shyam
 
Angela Narain
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is with reference to Valentin's post :
Below is what i know about String literals :
1. Suppose we have a statement as :

A new string will be created in the string pool.
2. Now if again we have a statement as :

then as the contents is same both s and s1 will share
the same string object.
What if the code had been as :

Pls. correct me if wrong .
thanks

[This message has been edited by Angela Narain (edited September 25, 2001).]
 
Valentin Crettaz
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Angela,
when you create a String instance using a String literal, that String will automatically be added to the String pool of the class. However, when you create a String dynamically, the String is NOT added to the String pool unless the method intern() is invoked on it !
In the code you mentioned, one String object will be garbage collected, namely the String "Test".
Val
 
Angela Narain
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Valentin.
Will the string object will gced after line4 ?
I am not clear of the sequence,
that is how it is made null first and then assigned
a new reference.
Can you explain to me.
 
Valentin Crettaz
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As soon as statement on line 3 has been executed the String "Test" may be gced anytime depending on the will of the garbage collector
Val
 
Cristian Negresco
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1.String string1 = "Test";
2.String string2 = "Today";
3.string1 = null;
4.string1 = string2;
I think this is also about my old "instance" problem,
My opinion is that it works like that:
line 1. the "string1" handle points to "Test" object,
line 2. the "string2" handle points to "Today" object,
line 3. the "string1" handle points to null, so there is no handle to point to "Test" object so he is eligible for gc
line 4. "string1" handle will point to the same object as "string2".
BTW, why the Strings Literals are not gc. ?

 
Valentin Crettaz
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Cristian, the String "Test" will NOT be gced since the String instance is created using a literal and thus the String "Test" is automatically interned in the internal String pool of the class !!
If the String was created like this
String s = new String("Test");
then it would have been eligible for garbage collection as soon as s is set to null !!!
Val
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because they are always pointed to by the String pool.
 
reply
    Bookmark Topic Watch Topic
  • New Topic