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

L

laredotornado

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, -
 
O

Oliver Wong

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?

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
 
L

Luc The Perverse

Oliver Wong said:
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.

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

Oliver Wong

Luc The Perverse said:
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
 
A

Andrew Thompson

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.
 
C

Chris Smith

Andrew Thompson said:
<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'.

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top