• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Runtime.gc() and System.gc()

 
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
> 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.
 
Hades Pan
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Marilyn very much
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 5399
1
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Valentin Crettaz
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
reply
    Bookmark Topic Watch Topic
  • New Topic