• Runtime类


      Runtime类是一个运行时的描述类,在每一个JVM中都存在Runtime类的对象,但是这个类查询文档的时候,是没有构造方法的,因为其在定义的时候将构造方法私有化了。因为其只能有一个实例化对象,那么该类使用的是单例设计模式。那么依然是单例设计模式那么在类的内部一定有一个取得对象的方法,

        1.取得Runtime对象方法,public static Runtime()

       那么取得Runtime类之后最主要的功能是可以用过它来观察当前的内存操作情况,

      取得当前空余内存空间大小:public long freeMemory()

      取得当前可以使用的总空间大小:public long totalMemory()

      取得当前最大内存空间:public long maxMemory()

      执行垃圾处理:public void gc()

    范例:观察一下内存信息的取得

     1 package cn.Tony.demo;
     2 
     3 public class TestDemo {
     4     public static void main(String[] args) throws Exception {
     5         Runtime run=Runtime.getRuntime();
     6         System.out.println("1.MAX="+byteTom(run.maxMemory()));
     7         System.out.println("1.TOTAL="+byteTom(run.totalMemory()));
     8         System.out.println("1.FREE="+byteTom(run.freeMemory()));
     9         String str="";
    10         for(int x=0;x<999;x++){
    11             str+=x;
    12         }
    13         System.out.println("2.MAX="+byteTom(run.maxMemory()));
    14         System.out.println("2.TOTAL="+byteTom(run.totalMemory()));
    15         System.out.println("2.FREE="+byteTom(run.freeMemory()));
    16         run.gc();
    17         System.out.println("3.MAX="+byteTom(run.maxMemory()));
    18         System.out.println("3.TOTAL="+byteTom(run.totalMemory()));
    19         System.out.println("3.FREE="+byteTom(run.freeMemory()));
    20     }
    21     public static double byteTom(long num) {
    22         return (double)num/1024/1024;
    23     }
    24 }

    面试题:什么叫gc?如何处理

      gc(GarbageCollector):垃圾收集器,用于释放无用的空间

      gc有两种处理形式,一种是自动不定期调用,另外是Runtime类中的gc()方法手工调用。 

  • 相关阅读:
    MapReduce案例WordCount
    MapReduce排序案例
    MapReduce倒排索引
    MapReduce自定义排序编程
    GroupingComparator 自定义分组
    CombineTextInputFormat小文件处理场景
    cdh 2.6.0版本和apache 2.7.x版本 本地执行环境的差异。
    DistributedCache 分布式缓存
    MapReduce数据压缩机制
    MapReduce其他功能
  • 原文地址:https://www.cnblogs.com/Tony98/p/10495606.html
Copyright © 2020-2023  润新知