Tuning involved in Java development

Source: Internet
Author: User
Tags xms

Tuning of JVM Memory

The default Java virtual machine size is relatively small, when the big data processing, Java will be error: Java.lang.OutOfMemoryError.

1. Heap Settings and garbage collection Java heap are divided into 3 zones, young,old and permanent. Young saves the object that was just instantiated. When the area is filled, the GC moves the object to the old area. The permanent area is responsible for saving the reflected object, which is not discussed in this article. the heap allocation for the JVM can be set using the-x parameter,


-xms Initial Heap Size
-xmx Java Heap Maximum Value
-xmn The heap size of young generation


The JVM has 2 GC threads. The first thread is responsible for recovering the young area of the heap. The second thread traverses the heap when the heap is insufficient and upgrades the young area to the older area. The size of the older area is equal to-xmx minus-xmn, and the value of the-XMS cannot be set too large because the second thread is forced to run to degrade the performance of the JVM. Why do some programs frequently occur in GC? There are the following reasons:l System.GC () or RUNTIME.GC () is called within the program. L Some middleware software calls its own GC method, at which time you need to set parameters to prohibit these GC. The heap in Java is too small, and the default heap values are generally small. L Frequent instantiation of objects, release objects. At this point, try to save and reuse objects, such as using StringBuffer () and string ().          If you find that after each GC, the remaining space in the heap will be 50% of the total space, which means your heap is in a healthy state. Many server-side Java programs have a better 65% space left after each GC. experience: < Span style= "color:red" >server end jvm best to -xms and -xmx Set to the same value ( in order to prevent the garbage collector from shrinking the heap between the minimum and maximum, we usually set the maximum and minimum to the same value ). In order to optimize gc-xmn -xmx 1 /3[2] Note:1. Increasing the size of the heap decreases the frequency of the GC, but it also increases the time of each GC. And when the GC runs, all the user threads are paused, that is, during the GC, the Java application does not do any work. 2. The heap size does not determine the amount of memory used by the process. The memory usage of the process is greater than the value defined by-XMX because Java allocates memory for other tasks, such as the stack for each thread.

2. Stack Settings

Each thread has his own stack.


-xss Stack size per thread


The size of the stack limits the number of threads. If the stack is too large, it will cause memory leaks. The-XSS parameter determines the stack size, such as-xss1024k. If the stack is too small, it can also cause the stack to spill.

3. Hardware Environmentthe hardware environment also affects the efficiency of the GC, such as the type of machine, memory, swap space, and number of CPUs. If your program needs to create many transient objects frequently, it will cause the JVM to frequent GC. In this case you can increase the machine's memory to reduce the use of swap space [2]. 4. 4 GC TypesThe first is a single-threaded GC, which is also the default GC. , the GC applies to single-CPU machines. The second is the throughput GC, which is a multi- threaded GC that is suitable for multi-CPU applications that use a large number of threads. The second GC is similar to the first GC, except that the GC is multithreaded in the collection of young, but in the old area it is still single-threaded, as in the first type. The-XX:+USEPARALLELGC parameter starts the GC. The third, concurrent low Pause GC, is similar to the first, applies to multiple CPUs, and requires shortening the time that the GC causes the program to stall. This GC can run the application at the same time as the recycle in the old area. The-XX:+USECONCMARKSWEEPGC parameter starts the GC. The fourth, incremental low Pause GC, is intended to shorten the time that the GC causes the program to stall. This GC can reclaim part of the old object while recovering from the young area. The-XINCGC parameter starts the GC. examples in Projects#!/bin/sh
Java-xms8192m-xmx8192m-xmn2800m-xss128k-xx:+useparallelgc-xx:parallelgcthreads=8-xx:+useparalleloldgc-jar P Arsefile-1.1.jar-xx:parallelgcthreads=8 applicationcontext-parsefile.xml log4j.xml 60003

See the specific description of the 4 GC. http://blog.csdn.net/rodesad/article/details/51544977;http://www.cnblogs.com/moonandstar08/p/5628463.htmlhttps://my.oschina.net/feichexia/blog/196575http://visionsky.blog.51cto.com/733317/566844/

Tuning involved in Java development

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.