long freeMemory = Runtime.getRuntime().freeMemory(); System.out.println("Free memory: " + freeMemory + " bytes");
long freeMemory = Runtime.getRuntime().freeMemory() / (1024 * 1024); System.out.println("Free memory: " + freeMemory + " MB");This example calculates the amount of free memory in megabytes by dividing the value returned by freeMemory() by 1,048,576 (the number of bytes in a megabyte). These examples belong to the java.lang package in the Java library as the Runtime class is part of this package.