174k views
3 votes
What are the methods used to get the amount of free memory and total memory available at runtime?

1) getFreeMemory() and getTotalMemory()
2) freeMemory() and totalMemory()
3) getMemory() and getTotalMemory()
4) getFreeMemory() and memoryTotal()

1 Answer

7 votes

Final answer:

The correct methods to get the amount of free memory and total memory available at runtime in Java are freeMemory() and totalMemory().

Step-by-step explanation:

The correct methods to get the amount of free memory and total memory available at runtime in Java are freeMemory() and totalMemory().

The freeMemory() method returns the amount of free memory in bytes that the Java Virtual Machine currently has. The totalMemory() method returns the total amount of memory currently available to the Java Virtual Machine.

Here's an example:

long freeMemory = Runtime.getRuntime().freeMemory();
long totalMemory = Runtime.getRuntime().totalMemory();
System.out.println("Free memory: " + freeMemory + " bytes");
System.out.println("Total memory: " + totalMemory + " bytes");

User Jon Harmon
by
9.0k points