dcsimg
what is the real method Runtime.gc() OR!_NEWLINE_!System.gc() ?
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   My_XLNC
Posted On:   Friday, January 25, 2002 01:15 AM

what is the real method Runtime.gc() OR System.gc() The link below points to a mock exam answer to the question no 55 says there is a method called *Runtime.gc()* which only suggests that the Java Virtual Machine does its garbage collection. And that garbage collection can not be forced. Whereas one certification book by authors *Khalid Mughal & Rasmussen* say that there is a method System.gc() which can force garbage collection. The answer and the authors contradict each other. can any one explain http://www.jchq.net/mockexams/exam1.htm    More>>

what is the real method Runtime.gc() OR
System.gc()


The link below points to a mock exam

answer to the question no 55 says there is a method called *Runtime.gc()* which only suggests that the Java Virtual Machine does its garbage collection. And that garbage collection can not be forced.

Whereas one certification book by authors *Khalid Mughal & Rasmussen* say that there is a method System.gc() which can force garbage collection.


The answer and the authors contradict each other.

can any one explain


http://www.jchq.net/mockexams/exam1.htm

   <<Less

Re: what is the real method Runtime.gc() OR!_NEWLINE_!System.gc() ?

Posted By:   Dermot_Hennessy  
Posted On:   Friday, January 25, 2002 01:49 AM

Hi,


First point, the System implementation of gc calls Runtime, i.e.


public static void gc() {
Runtime.getRuntime().gc();
}


Secondly, your question would be answered through reference to the JVM definition. The garbage collection thread is a low priority thread which is normally run by the JVM when resources are low, or when higher priority threads are idle. (Note that I say "normally"). You can suggest that the JVM perform a cleanup, but the JVM provider is under no obligation, in implementing the JVM, to ensure that GC is forced at exactly the point where you call the method.

I agree with you that documentation on the matter is confusing - authors use the word "force" carelessly.

A final note, here is the javadoc from Sun for Runtime.getRuntime.gc()


Runs the garbage collector.
Calling this method suggests that the Java Virtual Machine expend
effort toward recycling unused objects in order to make the memory
they currently occupy available for quick reuse. When control
returns from the method call, the Java Virtual Machine has made
its best effort to recycle all discarded objects.


The name gc stands for "garbage
collector". The Java Virtual Machine performs this recycling
process automatically as needed, in a separate thread, even if the
gc method is not invoked explicitly.


The method System.gc() is the conventional and convenient
means of invoking this method.

Hope this helps

About | Sitemap | Contact