• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

gc

 
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class GC
{
public static void main(String[] args)
{ String one = new String("name");
String two = "name";
two = null; // line 1
System.out.println("done");
}
}
How many objects become eligible for GC after execution of Line 1 above?
[This message has been edited by mohit joshi (edited December 02, 2000).]
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
None,
Because
1. Garbage collection applies to objects created with call to constructors.
2. String objects created as String s1 = "Two" will not refer to the String s2 = new String("Two").
Hope this helps.
 
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Saumil,
You claim that object <code>"name"</code> will not be GC'd.
Definitely, being in the constant pool, it may still have references to it even after <code>two</code> stops referring to it. Let's assume however, for the sake of example, that <code>two</code> was the *only* reference to <code>"name"</code>. By removing that reference the reference count for <code>"name"</code> becomes zero. Thus, unless there some special rule governing GC of objects in the constant pool, <code>"name"</code> should be GC'd.
So, here is my turn to pose a question to the forum: Are objects in the string constant pool GC'd as any other object, or is there some special rule governing their GC?
Thanks,
Panagiotis.

Originally posted by saumil shukla:
None,
Because
1. Garbage collection applies to objects created with call to constructors.
2. String objects created as String s1 = "Two" will not refer to the String s2 = new String("Two").
Hope this helps.


 
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Gc keeps an eye only on the objects which are created with keyword 'new' .
String created like
String a="xyz";
go to a pool .Altough this is an object but gc knows nothing about them so can never be garbage collected
[This message has been edited by Nasir Khan (edited December 02, 2000).]
 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nasir is right, though references to pool objects are the only way to refere to them but gc does not bother to collect objects in the pool. Reason?: String Pool is meant be small in size, just a way to optimize string literals, to be used here and there vide references. Therefore they are not suppose create a major threat to resources.

This conclusion is based on a simple expriement:
Made two java files, one containing 1000 non-similar String literals and the other containing 1000 Strings created with new statement. Both were with same skeleton and both executed System.gc(I used another java application to create these two files, I'll post it if you'll need it). compiled and ran both under two instances of JVM with -verbose:gc (makes gc to report its efforts after garbage collection). The result spoke for itself: No memory recovery in the case of String literals and ofcourse gc recovered some 360K in the case of new String.
[This message has been edited by Amond Adams (edited December 02, 2000).]

[This message has been edited by Amond Adams (edited December 02, 2000).]
[This message has been edited by Amond Adams (edited December 02, 2000).]
 
saumil shukla
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Amond,
Could you please mail me your code? I am very curious esp. want to see how gc works in I/Os.
please mail me at saumil_shukla@hotmail.com
Thanks
Saumil
 
Amond Adams
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
saumil you can find that code hereunder;
import java.io.*;
class WS{
final static int NUMSTRS = 1000;
final static int NUMCHRS = 1000;
final static int CHRSTART = 65;
final static String STRNM = "str";
public static void main(String[] args){
try{
FileWriter focs = new FileWriter("CS.java");
FileWriter fons = new FileWriter("NS.java");
StringBuffer buf = new StringBuffer();
String str1 = "\t\tString " + STRNM;
String str2 = " = \"";
String str3 = "\";\n";
String str = null;
String nstr1 = " = ";
String nstr2 = " new String(\"";
String nstr3 = "\");\n";

String temp = null;
temp = "public class CS{\n";
focs.write(temp, 0, temp.length());

temp = "public class NS{\n";
fons.write(temp, 0, temp.length());

temp = "\tpublic static void main(String[] args){\n";
focs.write(temp, 0, temp.length());
fons.write(temp, 0, temp.length());

StringBuffer nb = new StringBuffer();
nb.append("\n\n\n\t\t");
for(int i=0; i < NUMSTRS;i++){
for(int j=0; j < NUMCHRS ; j++){
int cc= ((((i % NUMCHRS) + j)%NUMCHRS) + CHRSTART);
if (cc == 92) cc = 65;
buf.append((char)cc);
}
str = str1 + i + str2 + buf.toString() + str3;
focs.write(str, 0, str.length());
str = str1 + i + nstr1 + nstr2 + buf.toString() + nstr3;
fons.write(str, 0, str.length());
str = null;
buf.setLength(0);
nb.append(STRNM + i + " = ");
}
nb.append("null;");
String ns = nb.toString();
focs.write(ns, 0, ns.length());
fons.write(ns, 0, ns.length());

temp = "\n\n\n\t\tSystem.out.println(\"Sleeping...\");\n\t\tSystem.gc();\n\t\ttry{\n\t\t\tThread.sleep(10000);\n\t\t}\n\t\t catch(Exception ex){}\n\t\tSystem.out.println(\"Done.....\");\n\t}\n}";
focs.write(temp, 0, temp.length());
fons.write(temp, 0, temp.length());

focs.close();
fons.close();
} catch(IOException ioe){
ioe.printStackTrace();
System.out.println("error");
}
}
}

just use cut and paste. It will produce two files;
CS.java (all String literals)
NS.java (all new String() objects)
compile them and run as I did watch and compare all verbose output.
 
Panagiotis Varlagas
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So, according to Nasir and Amond, once a String object is put to the constant pool, it stays there till the JVM exits (an optimization for object re-use). Taking that as granted let's consider the following piece of code:
<code>
String s = "Hello";
s = null;
String t = "Hello";
</code>
For variable t to be set to refer to the still-alive "Hello" object there must be a "hidden" reference somewhere, kept by the JVM; otherwise there would be no way to refer to "Hello".
If that is true, then that is the reason constant pool objects don't get GC'd. The garbage collector considers them (and rightfully so) non-garbage and does not collect them.
Panagiotis.
 
mohit joshi
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When I posted this question, I wasnt aware of this fact. Its been good learning for me also.
I have been having some trouble with Runtime.gc() method and
Runtime.freeMemory() method.
public static void main(String[] args)
{ Runtime rt = Runtime.getRuntime();
rt.gc();

try {
for(int i = 0; i<100; i++){
rt.gc();
Thread.sleep(1000);
System.out.println(i+" "+rt.freeMemory());
}

}
catch(Exception ex){
}
}
When I run this, the free memory values change after 6th 16th 32nd and 64th iteration. If I remove the gc() from the loop,
then the value changes after every 3 - 4 iterations. Why is this happening? I guess checking free memory is not a very reliable way of finding if something has been garbage collected.
 
The first person to drink cow's milk. That started off as a dare from this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic