What is Runtime.maxMemory() in Java?
What is the Runtime class in Java?
The Runtime class has methods that interface and interact with the environment in which the application is run.
The maxMemory() method
The maxMemory() method of the Runtime class is used to retrieve the maximum memory that the Java Runtime Environment (JVM) will attempt to use. This method returns a value in bytes.
Method signature
public native long maxMemory()
Return value
This method returns the maximum memory that the JVM will attempt to use.
Example
public class Main {public static void main(String[] args){Runtime currentRuntime = Runtime.getRuntime();System.out.println("Runtime.maxMemory() = " + currentRuntime.maxMemory() + " bytes");}}
Explanation
- Line 4: We retrieve the current runtime of the application using the
getRuntime()method of theRuntimeclass. - Line 5: We obtain the maximum memory that the JVM will attempt to use by invoking the
maxMemory()method on the current runtime, which was retrieved in line 4.