如何在java中找到RAM中使用的内存量

时间:2014-07-24 09:45:18

标签: java

我想要打印下列打印方法中使用的内存的代码

import java.*.*;
     public static void main(String[] args)
    {
    System.out.println('The memory used is');
    }

2 个答案:

答案 0 :(得分:5)

使用以下代码

float mb = 1024*1024; 
    //Getting the runtime reference from system
    Runtime runtime = Runtime.getRuntime();

    System.out.println("##### Heap utilization statistics [MB] #####");

    //Print used memory
    System.out.println("Used Memory:"+ (runtime.totalMemory() - runtime.freeMemory()) / mb);

    //Print free memory
    System.out.println("Free Memory:"+ runtime.freeMemory() / mb);

    //Print total available memory
    System.out.println("Total Memory:" + runtime.totalMemory() / mb);

    //Print Maximum available memory
    System.out.println("Max Memory:" + runtime.maxMemory() / mb);

答案 1 :(得分:1)

有计算

的方法
  

**total memory and free-memory**

在运行时。

class temp
    {
    public static void main(String args[])
    {
    Runtime rt = Runtime.getRuntime();


    System.out.println(rt.totalMemory());
    System.out.println(rt.freeMemory());
    System.out.println(rt.totalMemory() - rt.freeMemory());
    }}