Three-way view of memory usage in Java Virtual runtime __JVM

Source: Internet
Author: User

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

Related Article

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.