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

前端 未结 7 843
谎友^
谎友^ 2020-12-12 19:15

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

相关标签:
7条回答
  • 2020-12-12 19:29

    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().

    0 讨论(0)
  • 2020-12-12 19:38

    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.

    0 讨论(0)
  • 2020-12-12 19:46

    See the docs

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

    0 讨论(0)
  • 2020-12-12 19:50

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

    Runtime.getRuntime().gc();
    

    So it's just a convenience method.

    0 讨论(0)
  • 2020-12-12 19:53

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

    0 讨论(0)
  • 2020-12-12 19:53

    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 (static) method where as Runtime.gc() is an instance method. So, System.gc() is more convenient.

    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(){
    
            // ...
    
        }
        .
        .
        .
    
    }
    
    0 讨论(0)
提交回复
热议问题