System类包含一些有用的类字段和方法,他不能被实例化
- 方法:
- public static void gc()运行垃圾回收器
- public static void exit(int status)终止当前正在运行的Java虚拟机,参数用作状态码,根据惯例,非零表示异常
- Public static long currentTimeMillis()返回以毫秒为单位的当前时间
- public static void arraycopy(Object src,int srcPos,object dest,int destpos ,int length)从指定原数组中复制一个数组,复制从指定位置开始,到目标的指定位置结束
代码:
public static void exit(int status)
System.out.println("hello");
System.exit(0);
System.out.println("world");
Public static long currentTimeMillis()
long start= System.currentTimeMillis();
for(int i=0;i<100000;i++) {
System.out.println("helloworld");
}
long end= System.currentTimeMillis();
System.out.println("一共用时"+(end-start)+"毫秒");
public static void arraycopy(Object src,int srcPos,object dest,int destpos ,int length)
int arr[]= {11,22,33,44,55};
int arr2[]= {11,22,33,44,55};
System.arraycopy(arr, 1, arr2, 2, 2);
System.out.println(Arrays.toString(arr));
System.out.println(Arrays.toString(arr2));
public static void gc()
System.gc();
就是呼叫java虚拟机的垃圾回收器运行回收内存的垃圾。
其实基本没什么机会用得到这个命令, 因为这个命令只是建议JVM安排GC运行, 还有可能完全被拒绝。 GC本身是会周期性的自动运行的,由JVM决定运行的时机,而且现在的版本有多种更智能的模式可以选择,还会根据运行的机器自动去做选择,就算真的有性能上的需求,也应该去对GC的运行机制进行微调,而不是通过使用这个命令来实现性能的优化。