Functions of java_opts parameters-XMS and-xmx

Source: Internet
Author: User
Tags xms

Due to the jprofile debugging, the initial Tomcat memory parameters are constantly modified. Usually, the servlet has not been crash, and the Environment crashes first, and the initialization parameter is suspected. Let's talk a little bit about it and start research. There is not much good information on the Internet, and there is a clear explanation, so I will take it.

I recently saw some people discussing Java on the Internet. lang. many people are wondering why, in Java, freememory (), totalmemory (), and maxmemory ()ProgramThe freememory () method returns only one or two megabytes at the time of startup. As the Java program runs forward, a lot of objects are created, freememory () the return value of this method is sometimes not reduced, but increased. These people should have some misunderstandings about the significance of the freememory () method. They think that this method returns the remaining available memory of the operating system, but this is not the case at all. These three methods reflect the memory of the Java Process and have nothing to do with the memory of the operating system. The following is a combination of totalmemory () and maxmemory.

The maxmemory () method returns the maximum memory that the Java Virtual Machine (this process) can dig from the operating system, in bytes. If the Java program is running, if the-xmx parameter is not added, it is 64 mb. That is to say, maxmemory () returns about 64*1024*1024 bytes, this is the maximum memory that the Java Virtual Machine can dig from the operating system by default. If the-xmx parameter is added, the value following this parameter prevails. For example, Java-CP you_classpath-xmx512m your_class, the maximum memory size is 512*1024*1024 bytes.

The totalmemory () method returns the memory size that the Java Virtual Machine has dug from the operating system, that is, all the memory occupied by the Java Virtual Machine process at that time. If the-XMS parameter is not added when running Java, the memory is always slowly dug from the operating system during the Java program running process, basically how much is used to dig, as it reaches maxmemory (), totalmemory () increases slowly. If the-XMS parameter is used, the program will unconditionally dig the memory size defined after-XMS from the operating system at startup, and then when the memory usage is similar, then dig.

What is freememory ()? If the-XMS parameter is not added while running Java, the process of running Java is as follows, the memory is always dug slowly from the operating system. Basically, it is used to dig much, but when Java Virtual Machine 100% is used, it will dig a little more, the memory is actually freememory (), so the value of freememory () is usually very small, however, if you use-XMS when running a Java program, the number of memory defined after XMS is extracted from the operating system unconditionally at startup of the program, at this time, most of the dug memory may be useless, so freememory () may be a little large at this time.

A small program written by myself to help you understand it! Comparison between Java-CP. getmem and Java-CP.-xms80m-xmx80m getmem!

Code
Public   Class Getmem {
Static   Int Limit =   2000000 ;
Public Getmem () {
}

Public   Static   Void Main (string ARGs []) {
Getcurmem ();
Waitfor5s ();

String tmparray [] =   New String [limit];
Getcurmem ();
Waitfor5s ();

For ( Int I =   0 ; I < Limit; I ++ ) {
Tmparray [I]= NewString ("ABCDE");
}
Getcurmem ();
Waitfor5s ();
}

Static   Float Bittomega ( Long Bit) {
Return(Float) Bit/1024/1024;
}

Static   Void Getcurmem () {
Simpledateformat tmpdate =   New Simpledateformat ( " Yyyy-mm-dd "   +   "   "   +   " Hh: mm: SS " );
System. Out. println (tmpdate. Format ( New Date ()));
System. Out. println ( " Current memory: "   + Bittomega (runtime. getruntime (). totalmemory ()) +   " M " );
System. Out. println ( " Max memory: "   + Bittomega (runtime. getruntime (). maxmemory ()) +   " M " );
System. Out. println ( " Free memory: "   + Bittomega (runtime. getruntime (). freememory ()) +   " M " );
}

Static   Void Waitfor5s () {
Try   {
Thread. Sleep (5000);
}   Catch (Exception E) {
E. printstacktrace ();
}
}
}

PS: the task manager of the XP system is not very reliable in Java memory. The new version is good, process explorer!

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.