跳到主要内容

简述System.gc()和Runtime.gc()的作用?

参考答案:

System.gc()和Runtime.gc() 两者都是一样的。 System.gc()实际上等同于Runtime.gc()
System.gc()内部调用Runtime.gc() 。
唯一的区别是System.gc()是一个类方法,其中Runtime.gc()是一个实例方法。 所以System.gc()更方便。
从源代码看: System.gc()被实现为
Runtime.getRuntime().gc();
所以这只是一个方便的方法。

看文档:
System.gc()等同于Runtime.getRuntime().gc()
Runtime.gc()是一个本地方法,其中System.gc()是非本地方法,该方法又调用Runtime.gc()
在运行时系统中,gc是实例方法,但在系统方法中,gc是静态的。
因为这个原因,我们更喜欢使用system.gc()。
System.gc()的:
1:它是一个类方法(静态方法)。
2:非本地方法(不直接与硬件和系统资源交互的代码)。
3:System.gc(),内部调用Runtime.getRuntime().gc()。
Runtime.gc():
1:实例方法。
2:本地方法(一种直接与硬件和系统资源交互的编程语言)。