system.gc()和runtime.gc()之间的区别

时间:2011-06-01 06:49:04

标签: java android garbage-collection

System.gc()Runtime.gc()之间的区别是什么?

7 个答案:

答案 0 :(得分:56)

两者都是一样的。 System.gc()实际上等同于Runtime.gc()System.gc()在内部调用Runtime.gc()

唯一的区别是System.gc()是一种类方法,其中Runtime.gc()是实例方法。因此,System.gc()更方便。

答案 1 :(得分:15)

从查看源代码:System.gc()实现为

Runtime.getRuntime().gc();

所以这只是一种方便的方法。

答案 2 :(得分:8)

See the docs

System.gc()相当于Runtime.getRuntime().gc()

答案 3 :(得分:2)

Runtime.gc()是一种原生方法,其中System.gc()是非本地方法,而后者则调用Runtime.gc()

答案 4 :(得分:2)

<强> System.gc()的

1:这是一种类方法(静态方法)。

2:非本机方法。(不直接与硬件和系统资源交互的代码)。

3:System.gc(),内部调用Runtime.getRuntime()。gc()。

<强> Runtime.gc():

1:实例方法。

2:Native方法(一种直接与硬件和系统资源交互的编程语言。)。

答案 5 :(得分:0)

在运行时系统中,gc是实例方法,但在系统方法中,gc是静态的。

因为这个原因我们更喜欢使用system.gc()。

答案 6 :(得分:0)

两者相同 System.gc()实际上等效于 Runtime.gc()

System.gc()内部调用 Runtime.gc()

唯一的区别是:

System.gc()是一个类(静态)方法,其中 Runtime.gc()是一个实例方法。因此, System.gc()更方便。

System.gc()

public final class System extends Object{

    public static void gc(){
        .
        .
         Runtime.getRuntime().gc();
    
    }
    .
    .

}

Runtime.gc()

public class Runtime extends Object{

    public void gc(){

        // ...

    }
    .
    .
    .

}