aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Diff b/w System.gc() VS Runtime.gc() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Diff b/w System.gc() VS Runtime.gc()" Watch "Diff b/w System.gc() VS Runtime.gc()" New topic
Author

Diff b/w System.gc() VS Runtime.gc()

vidya sagar
Ranch Hand

Joined: Mar 02, 2005
Posts: 580
HI FREINDS

Can any one specify the differences between System.gc() and Runtime.gc()

Thanks in advance....
Jay Pawar
Ranch Hand

Joined: Aug 27, 2004
Posts: 411
I believe both do the same task of "suggesting" garbage collecter to run.
However, gc() method is static in System class and non-static in the Runtime class.

System.gc() // static method gc in System class

For Runtime you have to do something like this



Hope that helps...


Cheers,<br />Jay<br /> <br />(SCJP 1.4)<br />Heights of great men were not achieved in one day, they were toiling day and night while their companions slept.
Jeff Jetton
Ranch Hand

Joined: Mar 29, 2005
Posts: 71
From Sun's API documentation:


System

public static void gc()

Runs the garbage collector.

Calling the gc 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 a best effort to reclaim space from all discarded objects.

The call System.gc() is effectively equivalent to the call:

Runtime.getRuntime().gc()




And...


Runtime

public void 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 virtual machine has made its best effort to recycle all discarded objects.

The name gc stands for "garbage collector". The 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.


- Jeff
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
There is no difference. One is a level of indirection for the other.
In most implementations, System.gc() merely calls Runtime.gc().

And of course, you have to throw in the ambiguity in the API Spec. just to confuse people, otherwise, it just wouldn't be Java now, would it?.


Tony Morris
Java Q&A (FAQ, Trivia)
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17257
    
    6

"vidyasagar"-
Welcome to the JavaRanch! Please adjust your displayed name to meet the

JavaRanch Naming Policy.

You can change it

here.

Thanks! and welcome to the JavaRanch!

Mark
[ April 08, 2005: Message edited by: Mark Spritzler ]

Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Raghu Shree
Ranch Hand

Joined: Mar 18, 2005
Posts: 143
Hi Mark,
Why r u always suggest to change the display name. I saw this message in every post


Raghu J<br />SCJP 1.4<br /> <br />The Wind and waters are always<br />on the side of the ablest navigators.<br /><a href="http://groups.yahoo.com/group/scjp_share" target="_blank" rel="nofollow">SCJP Group</a><br /><a href="http://groups.yahoo.com/group/JavaBeat_SCWCD" target="_blank" rel="nofollow">SCWCD Group</a>
vidya sagar
Ranch Hand

Joined: Mar 02, 2005
Posts: 580
Hi mark

I hope now u Satisfy

Rather than telling each person to change Ur name to naming policy

Better u Validate while creating R updating profile
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Diff b/w System.gc() VS Runtime.gc()