0

i want the code which should print the memory used in the following print method

import java.*.*;
     public static void main(String[] args)
    {
    System.out.println('The memory used is');
    }
user3682903
  • 49
  • 1
  • 4
  • 2
    'memory used' is a very broad concept. Memory used by the JVM, Memory allocated to the JVM, memory used overall in the system? All of these would be valid interpretations. – Sinkingpoint Jul 24 '14 at 09:47
  • Look into java.lang.management, everything that has "Memory" in its class name and try whatever you think will answer your question. – laune Jul 24 '14 at 09:49

2 Answers2

6

Use following code

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);
Darshan Lila
  • 5,772
  • 2
  • 24
  • 34
1

There are methods to calculate the

**total memory and free-memory**

at the runtime.

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());
    }}
Deepanshu J bedi
  • 1,530
  • 1
  • 11
  • 23