The Java. Lang. runtime class provides the java. Lang. runtime class to view the current JVM memory usage. Each Java instance (Virtual Machine instance) has only one runtime instance. You can use runtime. getruntime () to obtain the runtime Instance Object of the current virtual machine.
Runtime provides three methods to obtain the maximum memory available for the current JVM, the maximum memory available for the current JVM, and the remaining unused memory. These three memories refer to the JVM heap memory.
Runtime. maxmemory ();
Runtime. totalmemory ();
Runtime. freememory ();
First, add the-xmx100m parameter to the Java startup parameters and set the JVM heap to use a maximum of MB of memory.
Runtime run = runtime. getruntime ();
Long max = run. maxmemory ();
Long Total = run. totalmemory ();
Long free = run. freememory ();
Long usable = max-Total + free;
System. Out. println ("maximum memory =" + max );
System. Out. println ("allocated memory =" + total );
System. Out. println ("available space in allocated memory =" + free );
System. Out. println ("maximum available memory =" + usable );
The output result of the above Code is as follows (the memory size is related to the runtime environment)
Maximum memory = 104071168
Allocated memory = 5177344
Remaining space in allocated memory = 4937280
Maximum available memory = 103831104
Maxmemory is specified by the parameter-xmx100m when the Java VM is started, and the output is indeed 100 MB. This indicates that the JVM heap memory can use a maximum of 104071168 bytes.
The memory used by the allocated memory (totalmemory) JVM is obtained from the local system. However, when the JVM is started, it does not apply for all the memory from the system. It is determined by the loaded class and the capacity of related resources. In this example, only the preceding lines of simple code are executed in the main () method. Therefore, the JVM only applies for 5177344 bytes of memory.
The remaining space (freememory) in the allocated memory is calculated relative to the allocated memory (totalmemeory), which is equivalent to the totalmemory-used memory. When freememory is close to 0, the allocated memory is about to run out. JVM will decide to apply for more memory from the system.
The maximum available memory (usable) is the memory that the JVM can continue to use (excluding the memory that will be reclaimed again after garbage collection ). Calculated by [maximum memory-allocated memory + remaining space in allocated memory.
. Each Java instance (Virtual Machine instance) has only one runtime instance. You can use runtime. getruntime () to obtain the runtime Instance Object of the current virtual machine.
Runtime provides three methods to obtain the maximum memory available for the current JVM, the maximum memory available for the current JVM, and the remaining unused memory. These three memories refer to the JVM heap memory.
Runtime. maxmemory ();
Runtime. totalmemory ();
Runtime. freememory ();
First, add the-xmx100m parameter to the Java startup parameters and set the JVM heap to use a maximum of MB of memory.
Runtime run = runtime. getruntime ();
Long max = run. maxmemory ();
Long Total = run. totalmemory ();
Long free = run. freememory ();
Long usable = max-Total + free;
System. Out. println ("maximum memory =" + max );
System. Out. println ("allocated memory =" + total );
System. Out. println ("available space in allocated memory =" + free );
System. Out. println ("maximum available memory =" + usable );
The output result of the above Code is as follows (the memory size is related to the runtime environment)
Maximum memory = 104071168
Allocated memory = 5177344
Remaining space in allocated memory = 4937280
Maximum available memory = 103831104
Maxmemory is specified by the parameter-xmx100m when the Java VM is started, and the output is indeed 100 MB. This indicates that the JVM heap memory can use a maximum of 104071168 bytes.
The memory used by the allocated memory (totalmemory) JVM is obtained from the local system. However, when the JVM is started, it does not apply for all the memory from the system. It is determined by the loaded class and the capacity of related resources. In this example, only the preceding lines of simple code are executed in the main () method. Therefore, the JVM only applies for 5177344 bytes of memory.
The remaining space (freememory) in the allocated memory is calculated relative to the allocated memory (totalmemeory), which is equivalent to the totalmemory-used memory. When freememory is close to 0, the allocated memory is about to run out. JVM will decide to apply for more memory from the system.
The maximum available memory (usable) is the memory that the JVM can continue to use (excluding the memory that will be reclaimed again after garbage collection ). Calculated by [maximum memory-allocated memory + remaining space in allocated memory.