zoukankan      html  css  js  c++  java
  • java中Runtime类详细介绍

    Runtime类描述了虚拟机一些信息。该类采用了单例设计模式,可以通过静态方法 getRuntime()获取Runtime类实例。下面演示了获取虚拟机的内存信息:

    package Main;
     
    public class Main
    {
        public static void main(String[] args) throws Exception
        {
            Runtime runtime = Runtime.getRuntime();
            System.out.println("total memory:"+runtime.totalMemory()/(1024*1024) + "M");
            System.out.println("max memory:" + runtime.maxMemory()/(1024*1024) + "M");
            System.out.println("free memory:" + runtime.freeMemory()/(1024*1024) + "M");
            System.out.println("Main Done//~~");
        }
    }
    

    输出结果:

    total memory:230M
    max memory:3403M
    free memory:226M
    Main Done//~~
    

    Runtime类提供gc()方法,用于释放Java虚拟机的一些无用空间。gc是garbage collection的缩写,就是垃圾回收的意思。

    package Main;
     
    public class Main
    {
        public static void main(String[] args) throws Exception
        {
            Runtime runtime = Runtime.getRuntime();
            runtime.gc();
            System.out.println("Main Done//~~");
        }
    }
    

    Runtime类可以调用本机的一些可执行程序,并且创建进程。exec(String cmd)方法可以打开一个可执行程序,下面代码演示了打开windows记事本程序并5秒后关闭。

    package Main;
     
    public class Main
    {
        public static void main(String[] args) throws Exception
        {
            Runtime runtime = Runtime.getRuntime();
            Process process = runtime.exec("notepad");
            Thread.sleep(1000*5);
            process.destroy();
            System.out.println("Main Done//~~");
        }
    }
    
  • 相关阅读:
    五大主流浏览器 CSS3 和 HTML5 兼容性大比拼
    httpservice 公共类
    Eclipse自动生成注释
    天生一对"Maven2+Jetty" Maven2创建并管理WebApp,并使用Maven Jetty Plugin在Eclipse中调试
    jsp checkbox不错的应用
    我们需要改变
    Sortable Table 可排序表格JS收集
    Eclipse快捷键大全()
    Ajax简单应用
    css 固定table表头
  • 原文地址:https://www.cnblogs.com/a1439775520/p/12947016.html
Copyright © 2011-2022 走看看