• 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

garbage collection

 
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi can anybody help in learning the following code about gc:

class Super
{
static int idcounter;
static int population;
protected int blobid;
public Super()
{
blobid=idcounter++;
++population;
}
protected void finalize() throws Throwable
{
super.finalize();
System.out.println("swee");
--population;
}
}

class Blob extends Super
{
int fat[];
static int counte=0;
public Blob(int a)
{
fat=new int[a];
System.out.println(blobid+"hi");
counte++;
}
protected void finalize() throws Throwable
{
super.finalize();
System.out.println(blobid+"bi");
System.out.println(counte);
}
}

public class Finalizers
{
public static void main(String[] args)
{
for (int i=0;i<5 ;i++ )
{
new Blob(50000);
}
System.out.println(Super.population+"alive");
}
};


The only thing i dn understand in this code is why is it that in the Blog class constructor an array fat is defined.Because when i tried to use fat as js an integer instead of array then finalize method is not called.Why is it necessory to create array obj to call GC.Plz help me in this...!!!
 
Marshal
Posts: 79475
379
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch.

I find it a bit difficult to understand what exactly you are after. By "gc" do you mean garbage collection? Please spell out such abbreviations. And what does js mean?

Why are you trying to finalize your objects in the first place? The finalize() method is hardly ever called. All yo have to do is use your objects for the last time, and wait. The garbage collection will come round whenever it feels like it without your having to call it. I tried your class, and got this printout:

0 hi
swee
0 bi
1
1 hi
2 hi
swee
2 bi
3
3 hi
4 hi
3 alive

That would I hope be what you expected. Yo don't seem to do anything with the array, so I don't know why it is there at all.
A confusing class; anybody else better able than me to help, please do.



At least I can use THAT graemlin for once.

CR
 
venkatesh pendharkar
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi thanx for replying & im really sorry for the abbrevations that i used.gc=garbage collection & js means just.Nways;actually i hv started preaparing for Sun certification & was studying Garbage collection ,thats when i came across this code which made me confusing.I am getting the same output that you have printed i.e. 2 times bi is printed which means 1st 2 blob objects are garbaged out of five that are created.I understand this completely that there is no guarantee that Garbage collection will be called every time.But in the above code if we remove the statement in the Blob constructor where we create the array object or insted of array we define it as a normal int then the compiler doesn go in the finalize method.
Earlier code:
class Blob extends Super
{
int fat[];
static int counte=0;
public Blob(int a)
{
fat=new int[a];
System.out.println(blobid+"hi");
counte++;
}
now if we replace fat array by fat integer:
{
int fat;
static int counte=0;
public Blob(int a)
{
fat=a;
System.out.println(blobid+"hi");
counte++;
}
now the compiler wont print the statements in finalize method.So why is that we need to define the array object so that Garbage colection is called.
 
Campbell Ritchie
Marshal
Posts: 79475
379
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't know.

CR

Anybody else?
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by venkatesh pendharkar:
But in the above code if we remove the statement in the Blob constructor where we create the array object or insted of array we define it as a normal int then the compiler doesn go in the finalize method.



If you don't instantiate the array, your program will consume much less memory - probably so little memory that the garbage collector simply decides that there still is enough left that he doesn't have a reason to start a time consuming garbage collection. Whereas, when you also create all those arrays, the memory gets filled up to the point that the garbage collector is forced to start working.
 
Ranch Hand
Posts: 1923
Scala Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
(please use code-tags and indentation)


In the first place, you use an Array, which is an object.
In the second place, there is just a tiny value, which isn't a target of gc.
[ May 01, 2006: Message edited by: Stefan Wagner ]
 
venkatesh pendharkar
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanx a lot Campbell,Ilja & Stefan for solving my doubt.This code was really confusing me ,thanx u people for helping me in this.
 
Campbell Ritchie
Marshal
Posts: 79475
379
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's a pleasure.
CR
 
No. No. No. No. Changed my mind. Wanna come down. To see 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