Runtime类
Runtime描述的是运行时状态,在每一个JVM进程中都会提供唯一的一个Runtime类实例化对象,所以想要获取Runtime类的实例化对象就必须使用Runtime类中提供的getRuntime()方法。
方法:
public static Runtime getRuntime();
获取Runtime类的实例化对象。
Runtime runtime=Runtime.getRuntime();
public int availableProcessors();
获取可用的CPU处理器数量。
int cpu=runtime.availableProcessors();
System.out.println(“cpu:”+cpu);
public long maxMemory();
获取最大可用内存量。以字节为单位。
long max=runtime.maxMemory();
public long totalMemory();
获取总共可用内存量。
long total=runtime.totalMemory();
public long freeMemory();
获取空闲内存量。
long free=runtime.freeMemory();
public void gc();
运行垃圾收集器,释放垃圾空间。
runtime.gc();
System类
System是一个系统类,主要功能是进行信息的输入和输出。
输入:System.in
Scanner scanner=new Scanner(System.in);
输出:System.out或System.err
System.out.println(“hello”);//黑字
System.err.println(“hello”);//红字
其他常用方法:
public static void arraycopy(Object a,int a1,Object b,int b1,int length);
数组复制。(原数组名称,原数组开始点,目标数组名称,目标数组开始点,需要复制的长度)。
String string=“hello word”;
char[] a=string.toCharArray();//字符串转换为字符数组
char[] b = new char[4];
System.arraycopy(a,6,b,0,4);
public static long currentTimeMillis();
获取当前时间。返回毫秒数。
long l=System.currentTimeMillis();
public static void gc();
释放垃圾空间。
System.gc();