Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / August 2006

Tip: Looking for answers? Try searching our database.

What is the use of System.gc and Runtime.gc?

Thread view: 
laredotornado@zipmail.com - 09 Aug 2006 20:14 GMT
Hello,

I read that in Java 1.4, garbage collection cannot be forced and calls
to System.gc() and Runtime.gc() are not reliable because they can defer
to threads of higher priority.  Then what exactly happens when
System.gc and Runtime.gc are called and when (if ever) do they get
executed?

Thanks, -
Oliver Wong - 09 Aug 2006 20:57 GMT
> Hello,
>
[quoted text clipped - 3 lines]
> System.gc and Runtime.gc are called and when (if ever) do they get
> executed?

   These methods get executed as soon as you call then. System.gc() simply
calls Runtime.getRuntime().gc(). Runtime.gc() invokes a native method in the
virtual machine. What the VM does at this point is implementation dependent.
I believe a legal implementation is to just ignore the method call
completely.

   - Oliver
Luc The Perverse - 10 Aug 2006 03:03 GMT
>> Hello,
>>
[quoted text clipped - 9 lines]
> dependent. I believe a legal implementation is to just ignore the method
> call completely.

But that is not the way is actually done in the Java VM by Sun for common
platforms . . . .  is it?

--
LTP

:)
Oliver Wong - 10 Aug 2006 15:01 GMT
>>> Hello,
>>>
[quoted text clipped - 12 lines]
> But that is not the way is actually done in the Java VM by Sun for common
> platforms . . . .  is it?

   I guess when Sun finishes open-sourcing their JVM, we'll find out! ;D

   Actually, from what I understand, you can configure what garbage
collection algorithm Sun's JVM will use via command line arguments, so
probably what Sun's JVM does depends heavily on which garbage collector its
using.

   - Oliver
Andrew Thompson - 11 Aug 2006 02:53 GMT
..
> I read that in Java 1.4, garbage collection cannot be forced and calls
> to System.gc() and Runtime.gc() are not reliable because they can defer
> to threads of higher priority.  Then what exactly happens when
> System.gc and Runtime.gc are called and when (if ever) do they get
> executed?

  (from subject)
> What is the use of System.gc and Runtime.gc?

<AFAIU>
I regard them as a *suggestion* to the JVM that
'now is a good time to perform GC - if it should be
getting close to being needed'.

If the VM fails to act on the call, it obviously has
lots of free space and therefore it does not make
sense to waste CPU cycles on it.
</AFAIU>

Andrew T.
Chris Smith - 11 Aug 2006 03:11 GMT
> ..
> > I read that in Java 1.4, garbage collection cannot be forced and calls
[quoted text clipped - 10 lines]
> 'now is a good time to perform GC - if it should be
> getting close to being needed'.

Basically, System.gc is a tool to increase perceived performance at the
expense of regular performance.  If all that mattered was average
throughput for the entire life of the application, then System.gc would
be meaningless.  However, in real life, there are times when a one-
second delay will not be noticed, and there are times when it will seem
interminably slow.  One possible idea to increase the *perceived*
performance of the application is to run System.gc from places where a
one-second delay won't be noticed.  For example, if I'm writing an IDE,
I may call System.gc after opening a new project, because I'd rather the
resources associated with the old project are found and recycled now
when the user expects to wait, in hopes that it won't occur later on
between the user pressing the key and the key being echoed to the
screen.  The latter would be fatal to perceived performance, while now
doesn't matter at all.

System.gc is also useful for testing the behavior of the garbage
collector.

> If the VM fails to act on the call, it obviously has
> lots of free space and therefore it does not make
> sense to waste CPU cycles on it.

That's probably not accurate, though.  A call to System.gc can't just be
ignored; after all, the API spec guarantees that by the time a call to
System.gc returns, a "best effort" has been made to reclaim all
available space.  Calls to System.gc might do nothing if, for example,
there's already another concurrent collection in progress and the system
can't run two collections at once.  It may also return immediately if
there is no deferred garbage collection work (for example in a reference
counting scenario).  However, it doesn't appear legal for it to just
return without doing work because the VM disagrees that work needs to be
done.

Existing behavior is consistent with this.  I don't know of a VM that
will ignore System.gc based on how full the heap currently is.

Signature

Chris Smith - Lead Software Developer / Technical Trainer
MindIQ Corporation



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.