Every Java application has a single instance of class Runtime that allows the application to interface with the environment in which the application is running.
Run the following code on your machine to know total, free and max memory size:
public class MemorySize {
public static void main(String[] args){
long kb = 1024;
long totalSize = Runtime.getRuntime().totalMemory();
long maxSize = Runtime.getRuntime().maxMemory();
long freeMemorySize=Runtime.getRuntime().freeMemory();
System.out.println("Total Size (KB): " + totalSize/1024);
System.out.println("Max Heap Size (KB): " + maxSize/1024);
System.out.println("Free Size (KB)"+ freeMemorySize/1024);
}
}
Output by my system:
Total Size (KB): 15872
Max Size (KB): 253440
Free Size (KB)15590
Explanation:
Run the following code on your machine to know total, free and max memory size:
public class MemorySize {
public static void main(String[] args){
long kb = 1024;
long totalSize = Runtime.getRuntime().totalMemory();
long maxSize = Runtime.getRuntime().maxMemory();
long freeMemorySize=Runtime.getRuntime().freeMemory();
System.out.println("Total Size (KB): " + totalSize/1024);
System.out.println("Max Heap Size (KB): " + maxSize/1024);
System.out.println("Free Size (KB)"+ freeMemorySize/1024);
}
}
Output by my system:
Total Size (KB): 15872
Max Size (KB): 253440
Free Size (KB)15590
Explanation:
- Using the getRuntime() method of the Runtime class we obtain the runtime object associated with the current Java application..
- The totalMemory() method returns how much memory is currently used.
- The maxMemory() method gives the maximum memory that the JVM could ever reach. It's the upper limit of the total memory.
- freeMemory() returns the amount of free memory in the Java Virtual Machine.