Difference between system.gc() and runtime.gc()

2019-01-21 07:42发布

What is the difference between System.gc() and Runtime.gc()?

6条回答
冷血范
2楼-- · 2019-01-21 08:05

System.gc():

1: It is a class method(static method).

2: Non-Native method.(Code which doesn't directly interacts with Hardware and System Resources).

3: System.gc(), Internally calls Runtime.getRuntime().gc().

Runtime.gc():

1: Instance method.

2: Native method(A programming language which directly interacts with Hardware and System Resources.).

查看更多
相关推荐>>
3楼-- · 2019-01-21 08:08

Both are same. System.gc() is effectively equivalent to Runtime.gc(). System.gc()internally calls Runtime.gc().

The only difference is System.gc() is a class method where as Runtime.gc() is an instance method. So, System.gc() is more convenient.

查看更多
祖国的老花朵
4楼-- · 2019-01-21 08:16

Runtime.gc() is a native method where as System.gc() is non - native method which in turn calls the Runtime.gc()

查看更多
贼婆χ
5楼-- · 2019-01-21 08:20

In the runtime system the gc is instance method but in system method the gc is static .

because of this reason we prefer to use system.gc().

查看更多
干净又极端
6楼-- · 2019-01-21 08:24

From looking at the source code: System.gc() is implemented as

Runtime.getRuntime().gc();

So it's just a convenience method.

查看更多
对你真心纯属浪费
7楼-- · 2019-01-21 08:24

See the docs

System.gc() is equivalent to Runtime.getRuntime().gc()

查看更多
登录 后发表回答