File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Performance and the fly likes Regding GarbageCollection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Performance
Bookmark "Regding GarbageCollection" Watch "Regding GarbageCollection" New topic

Regding GarbageCollection

Aabha Varma

Joined: Jan 12, 2005
Posts: 22
I have a program that creates 1000's of value object. All of them are in memory. When i run the program for the first time, its pretty fast and then on subsequent run its damn slow. I think its becasue GC is not happening....

ihave a method implementaion like this. could you tell me what needs to be done so that the objects are garbage collected

arrayList method(){
for(i = 0 ; i < n i++){
ValueObject v = new ValueObject();
arrayList.add(v) ;
return arrayList;

Now my question lies.. if after addding to arrayList can i make the valueobject v to null ??
can you direct me to some good implementation of effective garbage collection.
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 13036
Java already has a highly effective and efficient GC mechanism. Not surprising as it has been refined over 10 years.
It looks like you have the common problem of objects not being GCed because somewhere there is a collection with a reference. Since the ValueObject v reference is local to the method nothing you can do to it will help.
Ranch Hand

Joined: Dec 05, 2004
Posts: 48
I don't think you need to worry about this logic. Insted I suggest you to optimize other parameters in the system, like the min heap size, connection pool size,Number of threads, instances of Servelts/EJB in pools etc...

After ensuring that they are all optimized and still the problem is not solved them that's the time to dig into the application using application time stamps or using some profiler to tracce out sticky objects & time consuming operations (wait for connections/DB call.Queue operations etc).

Thanks,<br />Bhanu<br />SCEA Step-I<br />SCBCD for J2EE 1.3<br />SCJP1.4
Ilja Preuss

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Aabha Varma:

Now my question lies.. if after addding to arrayList can i make the valueobject v to null ??

That wouldn't change anything. valueobject is just a reference to the object you also put into the list, so even if you set valueobject to null, the object will still be referenced by the list (fortunately). So no object becomes eligible for gc'ing by doing so. Additionally, the variable is set to reference to a new object in the next iteration of the loop anyway, which would have the same effect (no longer referencing the old object).

The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
I agree. Here's the link:
subject: Regding GarbageCollection
It's not a secret anymore!