Home arrow static arrow Java Programming [Archive] - Garbage Collection Question.
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Garbage Collection Question.
This topic has 8 replies on 1 page.

Posts:754
Registered: 6/13/03
Garbage Collection Question.  
Jul 8, 2004 6:10 AM



 
Hi ya,

It has been recommended by Sun no to depend upon
on the finalize() method for garbage collection.

So is there any other method that can perform garbage
collection.

Can anyone please answer
 

Posts:4,496
Registered: 19/06/02
Re: Garbage Collection Question.  
Jul 8, 2004 6:14 AM (reply 1 of 8)



 
1. The finalize() method does not "perform garbage collection"
2. Look at the java.lang.ref package
 

Posts:447
Registered: 3/8/01
Re: Garbage Collection Question.  
Jul 8, 2004 6:16 AM (reply 2 of 8)



 
I don't think you're understanding what finalize is. Garbage collection happens automatically whenever Java decides it's running out of space, and you don't need to do anything to perform it. The finalize method is something you can put in an object that might get called just before the object is garbage-collected, in case it has to do some special cleanup, such as closing a connection to a database or closing a network connection, although most cleanup tasks like that will happen by themselves eventually.
 

Posts:754
Registered: 6/13/03
Re: Garbage Collection Question.  
Jul 8, 2004 6:18 AM (reply 3 of 8)



 
Got it....So is there a way to forcefully call garbage collection?
 

Posts:4,496
Registered: 19/06/02
Re: Garbage Collection Question.  
Jul 8, 2004 6:24 AM (reply 4 of 8)



 
System.gc() but this may not "force" the GC to run, merely "suggest" that it does. Either way you hardly ever need to call this method. The GC doesn't need your help to tell it when to run.
 

Posts:5,904
Registered: 04/03/99
Re: Garbage Collection Question.  
Jul 8, 2004 6:24 AM (reply 5 of 8)



 

No, you are allowed to "hint" to the garbage collector that it would be a good idea, but this is very rarely a good idea. Generally speaking the GC knows better than you do. If in any doubt whatsoever (and you are) don't.

Why do you want to do this ? Perhaps we can suggest an alternative mechanism for whatever you're trying to achieve ?

Dave.
 

Posts:18,384
Registered: 21.03.00
Re: Garbage Collection Question.  
Jul 8, 2004 6:44 AM (reply 6 of 8)



 
No, you are allowed to "hint" to the garbage collector that it would be a good idea, but this is very rarely a >good idea. Generally speaking the GC knows better than you do. If in any doubt whatsoever (and you are) >don't.

Hi, I don't agree on that. In applications with a lot of object allocations, and where you are concerned about when the gc-run it can be wise to hint to the gc when you know that a stop might not cause a large impact. The gc does not know when you as a user thinks that a halt/pause might be ok, but a programmer does.
So hints can be good.

/Kaj
 

Posts:5,904
Registered: 04/03/99
Re: Garbage Collection Question.  
Jul 8, 2004 6:48 AM (reply 7 of 8)



 
Hi, I don't agree on that. In applications with a lot of object allocations, and where you are concerned about when the gc-run it can be wise to hint to the gc when you know that a stop might not cause a large impact.

Unless you have actually tested and established that there is a problem with the gc performance and tested to confirm that this approach fixes it, this is a truly terrible idea, and a classic case of premature optimisation.

I reiterate:

If in any doubt whatsoever don't.

Only if you have tested to prove that there is an issue and that gc hints will fix it is it appropriate to take this tactic, but then, of course, you won't be in any doubt.

Dave.
 

Posts:4,496
Registered: 19/06/02
Re: Garbage Collection Question.  
Jul 8, 2004 6:51 AM (reply 8 of 8)



 
Like me mate Dave say, being premature is no way to please your Julie or write java code. Aiiiiiiii
 
This topic has 8 replies on 1 page.