欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

java获得JVM可能的总内存,最大内存,以及空闲内存,javajvm,要获得jvm相关的内存信

来源: javaer 分享于  点击 29514 次 点评:270

java获得JVM可能的总内存,最大内存,以及空闲内存,javajvm,要获得jvm相关的内存信


要获得jvm相关的内存信息,需要使用Runtime类的totalMemory(), maxMemory() 和 freeMemory()方法。

import java.text.DecimalFormat;/** * Main.java * * @author byrx.net */public class Main {    /**     * Displays the total amount of memory, the maximal amount of memory     * and the total amount of free memory in the Java Virtual Machine.     */    public void displayAvailableMemory() {        DecimalFormat df = new DecimalFormat("0.00") ;        //Display the total amount of memory in the Java virtual machine.        long totalMem = Runtime.getRuntime().totalMemory();        System.out.println(df.format(totalMem 1000000F) + " MB");        //Display the maximum amount of memory that the Java virtual machine will attempt to use.        long maxMem = Runtime.getRuntime().maxMemory();        System.out.println(df.format(maxMem 1000000F) + " MB");        //Display the amount of free memory in the Java Virtual Machine.        long freeMem = Runtime.getRuntime().freeMemory();        System.out.println(df.format(freeMem 1000000F) + " MB");    }    /**     * Starts the program     *     * @param args the command line arguments     */    public static void main(String[] args) {        new Main().displayAvailableMemory();    }}

可能的输出如下:

2,03 MB66,65 MB1,82 MB
相关栏目:

用户点评