The Good News:
This code will repeatedly run the garbage collector until it actually collects the
garbage, overcoming the "best effort" nature of System.gc().

The Bad News:
In a multi-threaded environment, this thread might well loop forever, as the
(native Sun jdk) gc won't actually throw anything out unless it runs to completion
(according to a source from JavaOne), and anything that contends with the gc will
halt it and it will wait to start all over again.  Additionally, one might mention
that creation of new objects (again, multi-threaded) might outpace the gc, and thus
it might loop unnecessarily in a situation where there is no garbage to collect
(e.g., a bunch of objects get allocated by thread2 while thread1 is calling the
gc() in the loop.  this is pretty likely because the gc runs in the idle thread
(under WinNT) and has the lowest possible priority, in order to avoid contention
with application threads).  Even if this code doesn't hang you up entirely, it will
definitely kill your performance, unless you're in a single-threaded system.

I have heard that HotSpot implements an incremental garbage collection algorithm.

The gc is shrouded in mystery, and dealing with it is something of a black art.
I'd be interested in hearing from anyone at Sun (or anywhere else that has
implemented a JVM) who has actually worked on the implementation of the gc.  This
is, of course, totally off-topic to our JSP discussion, so if you'd like to conduct
this via direct e-mail, that would be fine.
-bml

David Orkin wrote:

> try this code:
>
> Runtime rt = Runtime.getRuntime( );
> long isFree = rt.freeMemory( );
> long wasFree;
> do {
>    wasFree = isFree;
>    rt.gc( );
>    isFree = rt.freeMemory( );
> }
> while( isFree > wasFree );
> rt.runFinalization( );
>
> ===========================================================================
> To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
> FAQs on JSP can be found at:
>  http://java.sun.com/products/jsp/faq.html
>  http://www.esperanto.org.nz/jsp/jspfaq.html

--
_________________________________________________________________________

Brian M. Long      RandomWalk Computing, Inc.    [EMAIL PROTECTED]

'Apollo was astonished / Dionysus thought me mad' -Hemispheres, 14:52
__________________________________________________________________________

===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
FAQs on JSP can be found at:
 http://java.sun.com/products/jsp/faq.html
 http://www.esperanto.org.nz/jsp/jspfaq.html

Reply via email to