009—JAVA中的RunTime与System

RunTime是单例,可以用来测试总内存和剩余内存

public class Person {
    String name;
    int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
public class RuntimeTest {
    @Test
    public void test01(){
        //RunTime是单例
        Runtime r1 = Runtime.getRuntime();
        Runtime r2 = Runtime.getRuntime();
        System.out.println("r1==r2 = " + (r1 == r2));
        System.out.println("空闲内存 r1.freeMemory() = " + r1.freeMemory());

        System.out.println("总内存 r1.totalMemory() = " + r1.totalMemory());
    }
    @Test
    public void test02() {
        Runtime r1 = Runtime.getRuntime();
        System.out.println("r1.totalMemory() = " + r1.totalMemory());
        System.out.println("r1.freeMemory() = " + r1.freeMemory());
        for (int i = 0; i < 100000; i++) {
            new Person("张三"+i, i);
        }
        //运行垃圾会收器
//        System.gc();
        System.out.println("r1.totalMemory() = " + r1.totalMemory());
        System.out.println("r1.freeMemory() = " + r1.freeMemory());
    }
}

System

public class SystemTest {
    @Test
    public void test04() {
        System.out.println("1111");
        //结束jvm
        System.exit(0);
        System.out.println("33333");
    }

    @Test
    public void test03() {

        String[] arr = {"安琪拉", "米莱迪", "亚索"};

        String[] newArr = new String[4];//  将"米莱迪","亚索" 放到 newArr 下标 为 2  3的位置
        String string = Arrays.toString(newArr);
        System.out.println("string = " + string);

        System.arraycopy(arr, 1, newArr, 2, 2);
        String string1 = Arrays.toString(newArr);
        System.out.println("string1 = " + string1);

    }
        @Test
    public void test02() {
        //获取jdk配置信息
        Properties properties = System.getProperties();
        //展现所有的配置信息
        //输出流
        properties.list(System.out);
        String property = System.getProperty("java.version");
        System.out.println("property = " + property);
    }


    @Test
    public void test01() {
        //获取当前时间的毫秒数
        long currentTimeMillis = System.currentTimeMillis();
        System.out.println("currentTimeMillis = " + currentTimeMillis);
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值