public class Demo { public static void main(String[] args) { long start = System.currentTimeMillis();//当前系统时间 Runtime runtime = Runtime.getRuntime(); System.out.println("刚开始 最大内存: "+runtime.maxMemory()/1024.0/1024+"M"); System.out.println("刚开始 总共内存: "+runtime.totalMemory()/1024.0/1024+"M"); System.out.println("刚开始 剩余内存: "+runtime.freeMemory()/1024.0/1024+"M"); // runtime.gc();//垃圾回收 String str = null; for(int i=0;i<1000;i++) { str+=i; } System.out.println("循环后 最大内存: "+runtime.maxMemory()/1024.0/1024+"M"); System.out.println("循环后 总共内存: "+runtime.totalMemory()/1024.0/1024+"M"); System.out.println("循环后 剩余内存: "+runtime.freeMemory()/1024.0/1024+"M"); System.gc();//也是调的Runtime的gc() System.out.println("gc后 最大内存: "+runtime.maxMemory()/1024.0/1024+"M"); System.out.println("gc后 总共内存: "+runtime.totalMemory()/1024.0/1024+"M"); System.out.println("gc后 剩余内存: "+runtime.freeMemory()/1024.0/1024+"M"); long end = System.currentTimeMillis();//当前系统时间 System.out.println("运行用时: "+(end-start)+"毫秒"); } /** * final 定义不能够被继承的父类,不能够被子类覆写的方法,定义常量 * finally 是在异常处理中进行异常处理的统一出口 * finalize 是Object类的一个方法protected void finalize() throws Throwable * 当系统调用System.gc()进行回收时,会调用finalize()进行收尾,相当于希构函数 * */ @Override protected void finalize() throws Throwable { super.finalize(); } }