• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Invoking GC

 
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When is it appropriate to use the current runtime and when is it appropriate to use system (e.g. rt.gc() versus System.gc() where rt = is a reference to the current runtime object) when invoking garbage collection?
 
Rancher
Posts: 1449
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Calling System.gc() is equivalent to calling Runtime.getRuntime().gc(), so it may be a more a matter of style than anything.
 
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In System.gc() no intance is created. But in the latter case, whenever you call Runtime.getRuntime().gc() a NEW Runtime object is creates everytime, and it has no reference to it. So the GC has lots of cleaning work to do.
regds
maha anna
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, check out these excerpts from the java source code (jdk 1.3 beta):
<code><pre>public final class System {

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

}</pre></code>

<code><pre>public class Runtime {

private static Runtime currentRuntime = new Runtime();

public static Runtime getRuntime() {
return currentRuntime;
}

public native void gc();

}</pre></code>
So it turns out that they both do exactly the same thing - no new Runtime instance is created; there's just one Runtime needed, which referenced by a static variable of the Runtime class. the System.gc() version is a tiny bit slower because it involves one more method call, but that's nothing at all compared to the time taken by the native gc() method. I think the method was probably first developed as part of Runtime, and later someone realized it would be shorter and easier to remember to have an equivalent shortcut method as part of System.
 
maha anna
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's interesting.
regds
maha anna
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!