Mention of the memory usage when the Java process is running is a confusing place for many beginners, which is how the JVM applies the memory to the operating system when we run the compiled. class file.
You can use the JDK's own run-time system parameters to describe the three methods in class runtime to view
Runtime.getruntime (). MaxMemory (); View the maximum memory that the JVM virtual machine can request to the operating system hosting the current host; If the program runs with the-XMX parameter set, the value is equal to the user setting, if no default is set to 64m;
Runtime.getruntime (). TotalMemory (), view the amount of memory currently requested by the JVM virtual machine, and if the program is running with the-XMS parameter set, this value is equal to the user setting value. If it is not set then the program automatically applies according to the current system operation, but this value must be less than or equal to Runtime.gettime (). MaxMemory;
Runtime.getruntime (). Freememory (); View the unused memory portion of the memory that is currently being requested in the JVM; This value will be smaller if more and more memory is being used by the current JVM request;
/*
The maximum number of memory that the *-xmx10m:java process can request to the operating system
*-xms1m:java the default requested memory when the process is initialized (as the system runs it will grow larger until it equals-XMX value)
*/
public class Heapconfig {
public static void Main (string[] args) {
SYSTEM.OUT.PRINTLN (maximum memory that the Java process can request to the operating system: + (Runtime.getruntime (). MaxMemory ())/(1024*1024) + "M");
SYSTEM.OUT.PRINTLN ("Java Process free Memory:" + (Runtime.getruntime (). Freememory ())/(1024*1024) + "M");
System.out.println ("The Java process has now requested memory from the operating system:" + (Runtime.getruntime (). TotalMemory ())/(1024*1024) + "M");
byte[] bys = new byte[1024*1024];//request 1 M memory
SYSTEM.OUT.PRINTLN (maximum memory that the Java process can request to the operating system: + (Runtime.getruntime (). MaxMemory ())/(1024*1024) + "M");
SYSTEM.OUT.PRINTLN ("Java Process free Memory:" + (Runtime.getruntime (). Freememory ())/(1024*1024) + "M");
System.out.println ("The Java process has now requested memory from the operating system:" + (Runtime.getruntime (). TotalMemory ())/(1024*1024) + "M");
}
}
Console output Results
Maximum memory that the Java process can request to the operating system: 9M
Java Process free Memory: 5M
The Java process has now requested memory from the operating system: 5M
Maximum memory that the Java process can request to the operating system: 9M
Java Process free Memory: 4M
The Java process has now requested memory from the operating system: 5M