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');
}
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');
}
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);
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());
}}