Question

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.

Était-ce utile?

La solution 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()

Autres conseils

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.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top