System.gc()调用

在调用System.gc()方法时,只是“暗示着 Java 虚拟机做了一些努力来回收未用对象,以便能够快速地重用这些对象当前占用的内存”,并不意味着gc将马上运行。

为什么会这样呢?

这是因为System.gc()调用Runtime.getRuntime().gc(), 而后者只是一个native方法,见下:

 

    /**
     * Runs the garbage collector.
     * Calling this method suggests that the Java virtual machine expend 
     * effort toward recycling unused objects in order to make the memory 
     * they currently occupy available for quick reuse. When control 
     * returns from the method call, the virtual machine has made 
     * its best effort to recycle all discarded objects. 
     * <p>
     * The name <code>gc</code> stands for "garbage 
     * collector". The virtual machine performs this recycling 
     * process automatically as needed, in a separate thread, even if the 
     * <code>gc</code> method is not invoked explicitly.
     * <p>
     * The method {@link System#gc()} is the conventional and convenient 
     * means of invoking this method. 
     */
    public native void gc();

 

native是方法修饰符。Native方法是由另外一种语言(如c/c++,FORTRAN,汇编)实现的本地方法。gc()方法的具体实现可能在不同的环境中有不同的表现,有的情况下可能能马上开始内存清理,有的情况下却不能,所以只能说是尽最大努力

你可能感兴趣的:(C++,c,虚拟机,C#,fortran)