Domanda

If we call System.gc() it internally call the Runtime.getRuntime().gc() method then why sun's need to make gc method in two class like System and Runtime, without System class we can call the Runtime.getRuntime().gc() and request the JVM for garbage collection.

What is the reason behind making gc() method in two classes.

Please explain your reason of that.

È stato utile?

Soluzione 2

I think, if object is eligible for garbage collection then the JVM internally call the gc() method. System class contain static gc() method so jvm easily call this method using the class name i.e. System.gc(). But the Runtime class contain non-static gc() method, it is impossible to jvm to call this method directly b'coz it needs object to call non static method.

System.gc() method internally call the Runtime.getRuntime.gc()

Altri suggerimenti

It is just a convenient access to the same method. Can be found in the API description as well:

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

   Runtime.getRuntime().gc()

Hint: Do not call the GC manually. This is a bad idea in most cases.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top