質問

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.

役に立ちましたか?

解決 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()

他のヒント

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.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top