aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Runtime.gc()  and System.gc() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Runtime.gc()  and System.gc() " Watch "Runtime.gc()  and System.gc() " New topic
Author

Runtime.gc() and System.gc()

Hades Pan
Ranch Hand

Joined: Nov 25, 2001
Posts: 106
You are concerned that your program may attempt to use more memory than is available. To avoid this situation you want to ensure that the Java Virtual Machine will run its garbage collection just before you start a complex routine. What can you do to be certain that garbage collection will run when you want .
1) You cannot be certain when garbage collection will run
2) Use the Runtime.gc() method to force garbage collection
3) Ensure that all the variables you require to be garbage collected are set to null
4) Use the System.gc() method to force garbage collection
Which one is right?
What are Runtime.gc() and System.gc() ? What different on used of them?
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9046
    
  10
> 1) You cannot be certain when garbage collection will run
> 2) Use the Runtime.gc() method to force garbage collection
> 3) Ensure that all the variables you require to be garbage collected are set to null
> 4) Use the System.gc() method to force garbage collection
> Which one is right?

#1 is the correct answer.You cannot force garbage collection. You can do #3 and either #2 or #4, but it will not force gc.

> What are Runtime.gc() and System.gc() ? What different on used of them?

They are equivalent.


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Hades Pan
Ranch Hand

Joined: Nov 25, 2001
Posts: 106
Thanks Marilyn very much
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
There is no such thing as Runtime.gc() !
The correct invocation is Runtime.getRuntime().gc() !
It's a shame that even mock exam authors do not pay attention to those things !!
I like being picky (sometimes it helps)

------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
R K Singh
Ranch Hand

Joined: Oct 15, 2001
Posts: 5371
Originally posted by Marilyn deQueiroz:
> What are Runtime.gc() and System.gc() ? What different on used of them?

They are equivalent.

then why to have same method at two places??

------------------
Regards
Ravish


"Thanks to Indian media who has over the period of time swiped out intellectual taste from mass Indian population." - Chetan Parekh
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
From the Java API in class Runtime/method gc()

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

From the System.java source code

I think this answers your question !
HIH
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Runtime.gc() and System.gc()