As we all know, the memory of the JVM is limited, one is the architecture of the machine and the other is the operating system itself.
X86,x86-64,sparc,..... Memory mappings are different, and the memory management mechanisms of each operating system differ.
The following is from http://fengyouhua.javaeye.com/blog/58170
1. Heap Settings and garbage collection Java heap are divided into 3 districts, Young,old and permanent. Young saves a newly instantiated object. When the area is filled, the GC moves the object to the old area. The permanent area is responsible for saving reflection objects, and this article does not discuss the area. The heap allocation of the JVM can be set using the-x parameter.
|
-xms
|
Initial Heap Size
|
|
-xmx
|
Java Heap max value
|
|
-xmn
|
Young generation's heap size
|
The JVM has 2 GC threads. The first thread is responsible for reclaiming the young area of heap. The second thread, when heap is insufficient, traverses the heap and upgrades young to the older area. The size of the older area equals-xmx minus-xmn, and the-XMS value cannot be set too large because the second thread is forced to run, which degrades the performance of the JVM.
Why do some programs frequently occur with GC? There are the following reasons:
L the System.GC () or RUNTIME.GC () is called within the program.
L Some middleware software calls its own GC method, at which point you need to set parameters to prohibit these GC.
L Java's heap is too small, the general default heap values are very small.
L frequently instantiate objects, release objects. At this point, try to save and reuse the objects, such as using StringBuffer () and string ().
If you find that after each GC, the remaining space of the heap will be 50% of the total space, which means that your heap is in a healthy state. Many server-side Java programs have a 65% of the remaining space after each GC. Experience:
1. the Server -side JVM is best to set-XMS and -xmx to the same value. In order to optimize the GC, it is best to make the-XMN value approximately equal to the-xmx 1/3[2].
2. A GUI program is best to run a GC every ten to 2 times in half a second .
1. Increasing the size of the heap may reduce the frequency of the GC, but it also increases the time for each GC. And when the GC runs, all the user threads will be paused, i.e., 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 a process is greater than the value defined by-XMX, because Java allocates memory for other tasks, such as stack for each thread.
Each thread has his own stack.
|
-xss
|
Stack size for each thread
|
The size of the stack limits the number of threads. If the stack is too large, it can cause memory leaks. The-XSS parameter determines the stack size, such as-xss1024k. If the stack is too small, it can also cause stack overflow.
The 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 memory of the machine to reduce the use of swap space [2].
The first is a single-threaded GC and is also the default GC. , the GC applies to a single CPU machine.
The second is the throughput GC, which is a multi-threaded GC, suitable for multiple CPUs, and uses a large number of threads for programs. The second GC is similar to the first GC, except that the GC is multi-threaded in the collection of young, but in the old and the first, it still takes a single thread. The-XX:+USEPARALLELGC parameter starts the GC.
The third is the concurrent low Pause GC, similar to the first, for 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 old zone's recycle. The-XX:+USECONCMARKSWEEPGC parameter starts the GC.
The fourth type is the incremental low Pause GC, which is used to shorten the time that the GC causes the program to stall. This GC can reclaim some old area objects while recovering from the young area. The-XINCGC parameter starts the GC.