• 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

Couldnt understand the concept of Garbage Collectors in the following program

 
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
This is Kajol here. I am a begginer in JAVA. I was going through the Garbage Collectors concept from Thinking in JAVA, but couldnt get the following program.
//: Garbage.java
// Demonstration of the garbage
// collector and finalization
class Chair {
static boolean gcrun = false;
static boolean f = false;
static int created = 0;
static int finalized = 0;
int i;
Chair() {
i = ++created;
if(created == 47)
System.out.println("Created 47");
}
protected void finalize() {
if(!gcrun) {
gcrun = true;
System.out.println(
"Beginning to finalize after " +
created + " Chairs have been created");
}
if(i == 47) {
System.out.println(
"Finalizing Chair #47, " +
"Setting flag to stop Chair creation");
f = true;
}
finalized++;
if(finalized >= created)
System.out.println(
"All " + finalized + " finalized");
}
}
public class Garbage {
public static void main(String[] args) {
if(args.length == 0) {
System.err.println("Usage: \n" +
"java Garbage before\n or:\n" +
"java Garbage after");
return;
}
while(!Chair.f) {
new Chair();
new String("To take up space");
}
System.out.println(
"After all Chairs have been created:\n" +
"total created = " + Chair.created +
", total finalized = " + Chair.finalized);
if(args[0].equals("before")) {
System.out.println("gc():");
System.gc();
System.out.println("runFinalization():");
System.runFinalization();
}
System.out.println("bye!");
if(args[0].equals("after"))
System.runFinalizersOnExit(true);
}
} ///:~
Can somebody please help me..... asap.
It would be really nice if I get a detailed explanation for the above program.
Thanks & waiting for replies
Kajol
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This looks like an older example of the garbage.java example. I have 'thinking in java 2nd ed', and I think Bruce made some improvements in this Garbage.java example, and the text that accompanied it. For one thing, he has this sentence:
In Java 1.1, a method System.runFinalizersOnExit() was introduced that caused programs to run all the finalizers as they exited, but the design turned out to be buggy and the method was deprecated.
Accordingly, the 2nd edition of this code example no longer has this deprecated method.
It might be helpful if you can ask specifically what aspects of the program example you find confusing, or else should we assume that everything in the example is unclear, and you are looking for an explanation of every line of code?
------------------
 
Kajol Shroff
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI Stephen,
Thanks for ur reply......i think i am reading the 1st edition of thinking in JAVA...should i download....the new edition or the first one is also fine.......
I am confused with the whole program........
I didnt get the whole program.....
Please reply.....
Thanks for ur prompt reply Stephen
Kajol
 
Kajol Shroff
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everybody,
I think i am reading the 1st edition of thinking in JAVA...should i download....the new edition or the first one is also fine.......
I am confused with the whole program........ I didnt get the whole program.....Please reply.....
Thanks
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have been going through the 2nd edition of Thinking In Java and yes the program source code has been modified. You can download the 2nd edition from www.bruceeckel.com.
A complete explanation of the program is given after the source code. In addition as part of the exercise you are to run the aprogram with 3 different options :
java Garbage all
java Garbage finalize
java Garbage gc
What he wants to emphasize is that all Chair objects are garbage collected if you call System.gc(). calling System.runFinalization() collects only some objects. The remaining objects are not garbage collected. You can see this when you run the program with finalize option.
also note that finalize method is called during the initial run of the garbage collector. The memory of the object is retireved on a susequent run of garbage collector.
SJ
 
Sajan Joseph
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have been going through the 2nd edition of Thinking In Java and yes the program source code has been modified. You can download the 2nd edition from www.bruceeckel.com.
A complete explanation of the program is given after the source code. In addition as part of the exercise you are to run the aprogram with 3 different options :
java Garbage all
java Garbage finalize
java Garbage gc
What he wants to emphasize is that all Chair objects are garbage collected if you call System.gc(). calling System.runFinalization() collects only some objects. The remaining objects are not garbage collected. You can see this when you run the program with finalize option.
Also note that finalize method of the object is called during the initial run of the garbage collector. The memory of the object is retireved on a susequent run of garbage collector.
SJ
 
Kajol Shroff
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Sajan Joseph,
So u say I need to download, Thinking in JAVA 2 edition,
Okie i will do that.......
I had a copy of first edition.....and so i was referring it....
Its good taht u told me to download 2 edition....
I will do it........
tell me something more, about how to go ahead in JAVA as i am planning for SC JP .....
Reply
Kajol
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic