View the current JVM memory usage

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.