• 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

Cannot understand the following GC example from Khalid Mughal

 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The following exampleis used to demonstratde the garbage collection mechanism. I am not able to understand the example very clearly:
The code is as follows:
class SuperBlob {
static int idCounter;
static int population;
protected int blobId;
public SuperBlob(){
System.out.println("Super blob is getting called");
blobId=idCounter++;
++population;
}
protected void finalize() throws Throwable {
super.finalize();
--population;
}
}
class Blob extends SuperBlob {
int[] fat;
public Blob(int bloatedness){
fat=new int[bloatedness];
System.out.println(blobId+";Hello");
}
protected void finalize() throws Throwable{
System.out.println(blobId+";Bye");
super.finalize();
}
}
public class Finalizers {
public static void main(String[] args){
int blobsRequired, blobSize;
try {
blobsRequired=Integer.parseInt(args[0]);
blobSize =Integer.parseInt(args[1]);
}
catch(IndexOutOfBoundsException e){
System.out.println("Usage:Finalizers <number of blobs> <blob size>");
return;
}
for(int i=0;i<blobsRequired;++i){
new Blob(blobSize);
}
System.out.println(SuperBlob.population+"blobs alive");
}
}
a. What is being garbage collected in the above example? Is it int array fat? or is it Object Blob?
b. Will this have different results on different jvm's?
c. Why is super(finalize) being called in class
SuperBlob? What is the use of that? What will that call?
Could someone please explain the above.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic