The Freememory,totalmemory,maxmemory method in the runtime class to view the memory condition

Source: Internet
Author: User
Tags xms


Turn from: http://wenku.baidu.com/link?url=hXtN8gFyOn4c-J7vkW-YKO1Hq4dgKYkwCIdE3s3BzwG6ZqwDAKyZ8-ZhlmOz_ Fdpaqawjqpuz544g5c-nfjjnains-pkaezjjbftcvrqakq

Recently on the Internet to see some people to discuss the Java.lang.Runtime class Freememory (), TotalMemory (), maxmemory () Some of the topics of these methods, many people are puzzled, why, When the Java program was just started Freememory () This method returned only one or two megabytes, and as the Java program runs forward, creating a lot of objects, freememory () The return of this method is sometimes not reduced, but will increase. These people should have some misunderstanding about the meaning of the Freememory () method, which they thought would return the remaining available memory of the manipulation system, which is not the case at all. These three methods reflect the memory of the Java process, which has nothing to do with the memory of the operating system. The following is combined with totalmemory (), maxmemory () to explain.
MaxMemory () The method returns the largest memory that the Java Virtual machine (this process) can dig from the control system, in bytes, if the Java program is running without adding-xmx parameters, then it is 64 trillion, that is, MaxMemory () Returns approximately 64*1024*1024 bytes, which is the largest memory that the Java Virtual machine can dig from the control system by default. If the-XMX parameter is added, the value followed by this parameter, such as JAVA-CP classpath-xmx512m ClassName, the maximum memory is the 512*1024*0124 byte.


TotalMemory () This method returns the size of the memory that the Java Virtual machine has now dug up from the manipulation system, which is all the memory that the Java Virtual machine was occupying at the time. If you are running Java without adding-XMS parameters, then, in the process of running the Java program, memory is always slowly from the control system dug, basically is how much to dig, dug to maxmemory () so far, so totalmemory () is slowly increasing. If the-XMS parameter is used, the program will be automatically dug from the control system when it starts-XMS the amount of memory defined later, and then dig again when the memory is very short.


Freememory () is what, just mentioned that if you do not add-XMS parameters when running Java, then, in the process of running the Java program, memory is always slowly dug from the operating system, basically is how much to dig how much, But the Java Virtual machine 100% is a bit more to dig a little bit, these dug up without the use of memory, is actually freememory (), so the value of freememory () is generally very small, But if you are running Java programs using the-XMS, this time because the program at the start of the time will be unconditionally from the control system to dig-XMS after the definition of the number of memory, this time, dug over the memory may be mostly useless, so this time freememory () may be some big.

After the following source code compiled, in the class file in the directory, respectively, with JAVA-CP. Untitled1 and JAVA-CP. -xms80m-xmx80m Untitled1 Run to see how the results will help to understand the above elaboration.

public class Untitled1 {public Untitled1 () {} public static void Main (string[] args) {  
        System.out.println (Runtime.getruntime (). Freememory ());  
        System.out.println (Runtime.getruntime (). TotalMemory ());  
        System.out.println (Runtime.getruntime (). MaxMemory ());  
        Long T = System.currenttimemillis ();  
       try {thread.sleep (30000);  
       catch (Exception ee) {ee.printstacktrace ();  
       } string[] aaa = new string[2000000];  
       System.out.println (Runtime.getruntime (). Freememory ());  
       System.out.println (Runtime.getruntime (). TotalMemory ());  
       System.out.println (Runtime.getruntime (). MaxMemory ());  
       try {thread.sleep (30000);  
       catch (Exception ee) {ee.printstacktrace ();  
       for (int i = 0; i < 2000000 i++) {Aaa[i] = new String ("AAA"); } System.out.println (Runtime.getruntime (). freememory());  
       System.out.println (Runtime.getruntime (). TotalMemory ());  
       System.out.println (Runtime.getruntime (). MaxMemory ());  
       try {thread.sleep (30000);  
       catch (Exception ee) {ee.printstacktrace ();  }  
     }  
   }
<pre class= "java" name= "code" >public class Testmemory {public
static void Main (string[] args) {
int mb =  1024 * 1024;
Getting the runtime reference from system
Runtime runtime = Runtime.getruntime ();
System.out.println ("##### Heap utilization statistics [MB] #####");
Print used memory
System.out.println ("Used Memory:"
+ (Runtime.totalmemory ()-runtime.freememory ())/MB  );
Print free Memory
System.out.println ("Free Memory:" + runtime.freememory ()/MB);
Print Total available memory
System.out.println ("Total Memory:" + runtime.totalmemory ()/MB);
Print Maximum available memory
System.out.println ("Max Memory:" + runtime.maxmemory ()/MB);
}
 

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.