Runtime类:
这个类主要是获得一些与运行时有关的环境属性,或者是创建新的进程;
Runtime类是没有构造方法的,他的构造方法被私有化了,这就属于单例设计模式了,只有唯一一个Runtime类的对象;
实例化对象:public static Runtime getRuntime()
- RunTime的方法:
- 返回所有可用的内存空间:public long totalMemory()
- 返回最大可用的内存空间:public long maxMemory()
- 返回空余的内存空间:public long freeMemory()
范例:观察内存空间
public class test {
public static void main(String args[]) throws Exception {
Runtime run = Runtime.getRuntime();
System.out.println("1:MAX = " + run.maxMemory());
System.out.println("1:TOTAL = " + run.totalMemory());
System.out.println("1:FREE = " + run.freeMemory());
String str = "";
for (int x = 0; x < 2000; x++) {
str += x; // 产生大量垃圾
}
System.out.println();
System.out.println("2:MAX = " + run.maxMemory());
System.out.println("2:TOTAL = " + run.totalMemory());
System.out.println("2:FREE = " + run.freeMemory());
}
}
一旦产生垃圾之后,会改变可用内存空间的大小;但是在Runtime中有一个方法:public void gc(),可以释放掉垃圾空间;
public class test {
public static void main(String args[]) throws Exception {
Runtime run = Runtime.getRuntime();
System.out.println("1:MAX = " + run.maxMemory());
System.out.println("1:TOTAL = " + run.totalMemory());
System.out.println("1:FREE = " + run.freeMemory());
String str = "";
for (int x = 0; x < 2000; x++) {
str += x; // 产生大量垃圾
}
System.out.println();
System.out.println("2:MAX = " + run.maxMemory());
System.out.println("2:TOTAL = " + run.totalMemory());
System.out.println("2:FREE = " + run.freeMemory());
run.gc();// 释放垃圾空间
System.out.println();
System.out.println("2:MAX = " + run.maxMemory());
System.out.println("2:TOTAL = " + run.totalMemory());
System.out.println("2:FREE = " + run.freeMemory());
}
}
面试题:解释什么是GC?如何处理?
- GC(Garbage Collector)垃圾收集器,指的是释放无用的内存空间;
- GC会由系统不定期的进行回收,或者调用Runtime类中的gc()方法手工回收;
System类:
System类中有一个重要的方法:
- 取得当前系统时间:public static long currentTimeMillis();
范例:请统计出某项操作的耗时时间
public class test {
public static void main(String args[]) throws Exception {
long start = System.currentTimeMillis(); // 取得开始时间
String str = "";
for (int x = 0; x < 30000; x++) {
str += x;
}
long end = System.currentTimeMillis(); // 取得结束时间
System.out.println("本次操作所花费的时间:" + (end - start));
}
}
可以得到毫秒时间,经过long型数据进行数学计算得来;
在System类里面定义了一个操作方法:public static void gc(),这个方法并不是新定义的方法,而是间接调用了Runtime类中的gc()方法;
构造方法是留给对象初始化时使用的,finalize()方法是留给对象回收前使用的;
面试题:请解释final、finally、finalize的区别?
- final:关键字,定义不能被继承的类、不能覆写的方法、常量;
- finally:关键字,异常的统一出口;
- finalize:方法,Object类提供的方法:protected void finalize() throws Throwable,指的是对象回收前的收尾工作,即使出现了异常也不会导致程序中断执行;
对象克隆:
对象克隆就是对象的复制操作,在Object类里面有提供一个专门的克隆方法:
对象克隆:protected Object clone() throws CloneNotSupportedException,如果要使用对象克隆的类没有实现Cloneable接口,那么会抛出一个 “CloneNotSupportedException” 异常;Cloneable接口看不见方法,此为标识接口,表示一种能力;
范例:实现克隆
class Book implements Cloneable {
private String title;
private double price;
public Book(String title, double price) {
this.title = title;
this.price = price;
}
@Override
public String toString() {
return "书名:" + this.title + ",价格:" + this.price;
}
// 由于此类需要对象克隆操作,所以才需要方法覆写;源方法是protected类型的
@Override
public Object clone() throws CloneNotSupportedException {
return super.clone(); // 调用父类的克隆方法
}
}
public class test {
public static void main(String args[]) throws Exception {
Book bookA = new Book("Java开发", 79.8);
Book bookB = (Book) bookA.clone();
System.out.println(bookA);
System.out.println(bookB);
}
}