zoukankan      html  css  js  c++  java
  • Java中的Runtime类

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

     1 package Main;
     2  
     3 public class Main
     4 {
     5     public static void main(String[] args) throws Exception
     6     {
     7         Runtime runtime = Runtime.getRuntime();
     8         System.out.println("total memory:"+runtime.totalMemory()/(1024*1024) + "M");
     9         System.out.println("max memory:" + runtime.maxMemory()/(1024*1024) + "M");
    10         System.out.println("free memory:" + runtime.freeMemory()/(1024*1024) + "M");
    11         System.out.println("Main Done//~~");
    12     }
    13 }

    输出结果:

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

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

     1 package Main;
     2  
     3 public class Main
     4 {
     5     public static void main(String[] args) throws Exception
     6     {
     7         Runtime runtime = Runtime.getRuntime();
     8         runtime.gc();
     9         System.out.println("Main Done//~~");
    10     }
    11 }

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

     1 package Main;
     2  
     3 public class Main
     4 {
     5     public static void main(String[] args) throws Exception
     6     {
     7         Runtime runtime = Runtime.getRuntime();
     8         Process process = runtime.exec("notepad");
     9         Thread.sleep(1000*5);
    10         process.destroy();
    11         System.out.println("Main Done//~~");
    12     }
    13 }
  • 相关阅读:
    处理火星文重温vchar,char,nvchar,nchar
    删除文件
    js常用正则表达式
    安装iis 配置iis
    无题
    js函数大全
    常用正则表达式
    QQ在线客服
    获取系统文字字体
    无限级删除的存储过程
  • 原文地址:https://www.cnblogs.com/kuillldan/p/6287345.html
Copyright © 2011-2022 走看看