随笔都是学习笔记
随笔仅供参考,为避免笔记中可能出现的错误误导他人,请勿转载。

Runtime分析:

Runtime类属于单例设计模式;如果想要获取实例化对象,那么就可以依靠类中的getRuntime()方法。

 

  - 获取实例化对象:public static Runtime getRuntime();

 

public class Main {
    public static void main(String[] args) {
        Runtime rt = Runtime.getRuntime();  // 获取实例化对象
        System.out.println(rt.availableProcessors());   // 获取北极CPU的内核数,并发访问最多访问数量
        
    }
}
  - 获取最大可用内存空间:public long maxMemory();
  - 获取可用内存空间:public long totalMemory(); 
  - 获取空闲内存空间:public long freeMemory();
public class Main {
    public static void main(String[] args) {
        Runtime rt = Runtime.getRuntime();  // 获取实例化对象
        System.out.println("【CPU内核】:" + rt.availableProcessors());   // 获取北极CPU的内核数,并发访问最多访问数量
        System.out.println("【MAX_MEMORY】: " + rt.maxMemory());      // 获取最大可用内存空间,输出为byte,默认为系统的四分之一
        System.out.println("【TOTAL_MEMORY】: " + rt.totalMemory());  // 获取可用内存空间
        System.out.println("【FREE_MEMORY】: " + rt.freeMemory());    // 获取空闲内存空间
        System.out.println((double) rt.maxMemory()/(1024 * 1024 * 1024));
        System.out.println((double) rt.totalMemory()/(1024 * 1024 * 1024));
        System.out.println((double) rt.freeMemory()/(1024 * 1024 * 1024));
        rt.gc();
    }
}
  - 手工进行GC处理:public void gc();  // 垃圾回收

 

 

 

 面试题:

请问什么是GC?如何处理?

  - GC(Garbage Collector)垃圾收集器,是可以由系统自动调用的垃圾释放功能,或者使用Runtime类中的gc(手工调用).

posted on 2022-02-03 14:00  时间完全不够用啊  阅读(509)  评论(0编辑  收藏  举报