The JVM Optimization Guide


Java virtual machine has its own sound hardware architecture, such as processor, stack, registers, etc., also has a corresponding instruction. JVM mask information associated with a particular operating system platform, that just generates a target Java program Representative (byte code) running in the Java virtual machine can run without modification on multiple platforms. Java Virtual Machine byte code, when executed, in fact, eventually interpreted bytecode into machine instructions executed on a particular platform.
We begin this article with an example. Suppose you are an ordinary Java objects, you were born in the Eden area, and you almost have a lot of little brothers, little sisters, may the Eden area as kindergarten, kindergarten we play in this much time in the Eden area. Eden District can not endlessly put you in it, so when the older you are going to school to school, assuming for school district is called Survivor. When you start out in the division area inside Survivor "From" area, read the high school, and went into the region Survivor "To" area, because the middle grades unstable, often tossing back and forth. Until the time you are 18 years old, graduated from high school, go for another try on society. So you went to the old generation, old generation which a lot of people. In the old generation, you lived for 20 years (each GC plus one year of age), and finally come to an end, be recovered GC. One thing not mentioned, you encounter an older generation of students, and his name is Edward (town Twilight vampire guy), he and his family will never die, then they will live in eternity generations.
I think that optimize JVM must seize two points, the first point: how to shorten a single GC time; the second point: how to shorten GC frequency.

 

How will the new object in the young generation reserve

As we all know, since the cost is much higher than FullGC Minor GC, and therefore in some cases as much as possible will need to allocate objects in the young generation, which in many cases is a wise choice. Although in most cases, JVM will attempt to allocate objects in the Eden area, but due to space constraints and other issues, is likely to have to be part of the young to the old generation compression target ahead of time. Thus, when the JVM tuning parameters can be assigned a reasonable space for the young generation applications to avoid possible new objects directly into the old generation situation occurs.
Listing 1: Code shown attempts to allocate 4MB memory space, look at its memory usage.
The same size of the memory allocation:
public class PutInEden {
public static void main (String [] args) {
byte [] B1, B2, B3, B4; // define variables
// 1MB allocated heap space, investigated the use of heap space case
b1 = byte new new [1024 1024];
B2 = new new byte [1024
1024];
B3 = new new byte [1024 1024];
B4 = new new byte [1024
1024];
}
}
using the JVM parameter -XX: + PrintGCDetails-Xmx20M run list -Xms20M 1 shown in the code, as shown in Listing 2 for all input.
[GC [DefNew: 5504K-> 640K (6144K), 0.0114236 secs] 5504K-> 5352K (19840K),
0.0114595 secs] [Times: user=0.02 sys=0.00, real=0.02 secs]
[GC [DefNew: 6144K->640K(6144K), 0.0131261 secs] 10856K->10782K(19840K),
0.0131612 secs] [Times: user=0.02 sys=0.00, real=0.02 secs]
[GC [DefNew: 6144K->6144K(6144K), 0.0000170 secs][Tenured: 10142K->13695K(13696K),
0.1069249 secs] 16286K->15966K(19840K), [Perm : 376K->376K(12288K)],
0.1070058 secs] [Times: user=0.03 sys=0.00, real=0.11 secs]
[Full GC [Tenured: 13695K->13695K(13696K), 0.0302067 secs] 19839K->19595K(19840K),
[Perm : 376K->376K(12288K)], 0.0302635 secs] [Times: user=0.03 sys=0.00, real=0.03 secs]
[Full GC [Tenured: 13695K->13695K(13696K), 0.0311986 secs] 19839K->19839K(19840K),
[Perm : 376K->376K(12288K)], 0.0312515 secs] [Times: user=0.03 sys=0.00, real=0.03 secs]
[Full GC [Tenured: 13695K->13695K(13696K), 0.0358821 secs] 19839K->19825K(19840K),
[Perm : 376K->371K(12288K)], 0.0359315 secs] [Times: user=0.05 sys=0.00, real=0.05 secs]
[Full GC [Tenured: 13695K->13695K(13696K), 0.0283080 secs] 19839K->19839K(19840K),
[Perm : 371K->371K(12288K)], 0.0283723 secs] [Times: user=0.02 sys=0.00, real=0.01 secs]
[Full GC [Tenured: 13695K->13695K(13696K), 0.0284469 secs] 19839K->19839K(19840K),
[Perm : 371K->371K(12288K)], 0.0284990 secs] [Times: user=0.03 sys=0.00, real=0.03 secs]
[Full GC [Tenured: 13695K->13695K(13696K), 0.0283005 secs] 19839K->19839K(19840K),
[Perm : 371K->371K(12288K)], 0.0283475 secs] [Times: user=0.03 sys=0.00, real=0.03 secs]
[Full GC [Tenured: 13695K->13695K(13696K), 0.0287757 secs] 19839K->19839K(19840K),
[Perm : 371K->371K(12288K)], 0.0288294 secs] [Times: user=0.03 sys=0.00, real=0.03 secs]
[Full GC [Tenured: 13695K->13695K(13696K), 0.0288219 secs] 19839K->19839K(19840K),
[Perm : 371K->371K(12288K)], 0.0288709 secs] [Times: user=0.03 sys=0.00, real=0.03 secs]
[Full GC [Tenured: 13695K->13695K(13696K), 0.0293071 secs] 19839K->19839K(19840K),
[Perm : 371K->371K(12288K)], 0.0293607 secs] [Times: user=0.03 sys=0.00, real=0.03 secs]
[Full GC [Tenured: 13695K->13695K(13696K), 0.0356141 secs] 19839K->19838K(19840K),
[Perm : 371K->371K(12288K)], 0.0356654 secs] [Times: user=0.01 sys=0.00, real=0.03 secs]
Heap
def new generation total 6144K, used 6143K [0x35c10000, 0x362b0000, 0x362b0000)
eden space 5504K, 100% used [0x35c10000, 0x36170000, 0x36170000)
from space 640K, 99% used [0x36170000, 0x3620fc80, 0x36210000)
to space 640K, 0% used [0x36210000, 0x36210000, 0x362b0000)
tenured generation total 13696K, used 13695K [0x362b0000, 0x37010000, 0x37010000)
the space 13696K, 99% used [0x362b0000, 0x3700fff8, 0x37010000, 0x37010000)
compacting perm gen total 12288K, used 371K [0x37010000, 0x37c10000, 0x3b010000)
the space 12288K, 3% used [0x37010000, 0x3706cd20, 0x3706ce00, 0x37c10000)
Space 10240K RO, 51 is% Used [0x3b010000, 0x3b543000, 0x3b543000, 0x3ba10000)
RW Space 12288K, 55% Used [0x3ba10000, 0x3c0ae4f8, 0x3c0ae600, 0x3c610000)
Listing 2 shows the log output shown in the young generation Eden size there are about 5MB. Dispensing the young generation space large enough, using the JVM parameter -XX: PrintGCDetails -Xmx20M -Xms20M -Xmn6M the code shown in Figure 1, the output operation list as shown in Listing 2.
After increasing the size of the list of running Eden output
[the GC [DefNew: 4992K-> 576K (5568K), 0.0116036 secs] 4992K-> 4829K (19904K),
0.0116439 secs] [Times: User SYS = 0.02 = 0.00, = 0.02 secs Real ]
[the GC [DefNew: 5568K-> 576K (5568K), 0.0130929 secs] 9821K-> 9653K (19904K),
0.0131336 secs] [Times: User SYS = 0.02 = 0.00, 0.02 secs = Real]
[the GC [DefNew: 5568K- > 575K (5568K), .0154148 secs] 14645K-> 14500K (19904K),
.0154531 secs] [Times: User SYS = 0.00 = 0.01, 0.01 secs = Real]
[GC [DefNew: 5567K->5567K(5568K), 0.0000197 secs][Tenured: 13924K->14335K(14336K),
0.0330724 secs] 19492K->19265K(19904K), [Perm : 376K->376K(12288K)],
0.0331624 secs] [Times: user=0.03 sys=0.00, real=0.03 secs]
[Full GC [Tenured: 14335K->14335K(14336K), 0.0292459 secs] 19903K->19902K(19904K),
[Perm : 376K->376K(12288K)], 0.0293000 secs] [Times: user=0.03 sys=0.00, real=0.03 secs]
[Full GC [Tenured: 14335K->14335K(14336K), 0.0278675 secs] 19903K->19903K(19904K),
[Perm : 376K->376K(12288K)], 0.0279215 secs] [Times: user=0.03 sys=0.00, real=0.03 secs]
[Full GC [Tenured: 14335K->14335K(14336K), 0.0348408 secs] 19903K->19889K(19904K),
[Perm : 376K->371K(12288K)], 0.0348945 secs] [Times: user=0.05 sys=0.00, real=0.05 secs]
[Full GC [Tenured: 14335K->14335K(14336K), 0.0299813 secs] 19903K->19903K(19904K),
[Perm : 371K->371K(12288K)], 0.0300349 secs] [Times: user=0.01 sys=0.00, real=0.02 secs]
[Full GC [Tenured: 14335K->14335K(14336K), 0.0298178 secs] 19903K->19903K(19904K),
[Perm : 371K->371K(12288K)], 0.0298688 secs] [Times: user=0.03 sys=0.00, real=0.03 secs]
Exception in thread “main” java.lang.OutOfMemoryError: Java heap space[Full GC [Tenured:
14335K->14335K(14336K), 0.0294953 secs] 19903K->19903K(19904K),
[Perm : 371K->371K(12288K)], 0.0295474 secs] [Times: user=0.03 sys=0.00, real=0.03 secs]
[Full GC [Tenured
: 14335K->14335K(14336K), 0.0287742 secs] 19903K->19903K(19904K),
[Perm : 371K->371K(12288K)], 0.0288239 secs] [Times: user=0.03 sys=0.00, real=0.03 secs]
[Full GC [Tenuredat GCTimeTest.main(GCTimeTest.java:16)
: 14335K->14335K(14336K), 0.0287102 secs] 19903K->19903K(19904K),
[Perm : 371K->371K(12288K)], 0.0287627 secs] [Times: user=0.03 sys=0.00, real=0.03 secs]
Heap
def new generation total 5568K, used 5567K [0x35c10000, 0x36210000, 0x36210000)
eden space 4992K, 100% used [0x35c10000, 0x360f0000, 0x360f0000)
from space 576K, 99% used [0x36180000, 0x3620ffe8, 0x36210000)
to space 576K, 0% used [0x360f0000, 0x360f0000, 0x36180000)
tenured generation total 14336K, used 14335K [0x36210000, 0x37010000, 0x37010000)
The Space 14336K, 99% Used [0x36210000, 0x3700ffd8, 0x37010000, 0x37010000)
Compacting Perm Gen Total 12288K, Used 371K [0x37010000, 0x37c10000, 0x3b010000)
The Space 12288K,. 3% Used [0x37010000, 0x3706ce28, 0x3706d000, 0x37c10000)
RO Space 10240K ,% 51 is Used [0x3b010000, 0x3b543000, 0x3b543000, 0x3ba10000)
RW Space 12288K, 55% Used [0x3ba10000, 0x3c0ae4f8, 0x3c0ae600, 0x3c610000)
by comparison Listings 2 and 3 can be found by setting a reservation a new generation of large Young object, set reasonable Survivor areas and provide usage Survivor region, you can save the young objects in the young generation. In general, not enough space Survivor area, or occupancy reached 50%, it will make the object into the old generation (no matter how old it is). Listing 4 creates three objects are allocated certain amount of memory space.
Different size of the memory allocated
public class PutInEden2 {
public static void main (String [] args) {
byte [] B1, B2, B3;
// 0.5MB allocated heap space
B1 = new new byte [1024 512];
// allocate 4MB heap space
= new new byte B2 [1024
1024 . 4];
b3 = new new byte [1024
1024 . 4];
// enable b3 can be recovered
b3 = null;
// allocate stack space 4MB
b3 = new new byte [1024
1024 *. 4];
}
}
parameters -XX: PrintGCDetails -Xmx1000M -Xms500M -Xmn100M -XX: FIG. 4 code output SurvivorRatio = 8 run list is shown in Listing 5.
Listing 5. Listing 4 running output
Heap
DEF new new Generation Total 92160K, 11878K Used [0x0f010000, 0x15410000, 0x15410000)
Eden Space 81920K, 2% Used [0x0f010000, 0x0f1a9a20, 0x14010000)
from Space 10240K, 99% Used [0x14a10000, 0x1540fff8, 0x15410000 )
to Space 10240K, 0% Used [0x14010000, 0x14010000, 0x14a10000)
the tenured Generation Total 409600K, Used 86434K [0x15410000, 0x2e410000, 0x4d810000)
The Space 409600K, 21 is% Used [0x15410000, 0x1a878b18, 0x1a878c00, 0x2e410000)
Compacting Perm Gen Total 12288K, Used 2062K [0x4d810000, 0x4e410000, 0x51810000)
The Space 12288K, 16% Used [0x4d810000, 0x4da13b18, 0x4da13c00, 0x4e410000)
No Shared Spaces configured.
log output in Listing 5 shows that the young generation is allocated 8M, the old generation have allocated 8M. We can try to add -XX: when TargetSurvivorRatio = 90 parameter, which can improve the utilization of the region from the region from 90% to use, then the object into the old generation, operation code in Listing 4, the output 6 Listing shows.
After modifying the operating parameters Listing 6. Listing 4 output
Heap
DEF new new Generation Total 9216K, 9215K Used [0x35c10000, 0x36610000, 0x36610000)
Eden Space 8192K, 100% Used [0x35c10000, 0x36410000, 0x36410000)
from Space 1024K, 99% Used [0x36510000, 0x3660fc50, 0x36610000)
to 1024K Space, 0% Used [0x36410000, 0x36410000, 0x36510000)
the tenured Generation Total 10240K, Used 10239K [0x36610000, 0x37010000, 0x37010000)
The Space 10240K, 99% Used [0x36610000, 0x3700ff70, 0x37010000, 0x37010000)
Compacting Perm Gen Total 12288K, Used 371K [0x37010000, 0x37c10000, 0x3b010000)
The Space 12288K,. 3 % Used [0x37010000, 0x3706cd90, 0x3706ce00, 0x37c10000)
RO Space 10240K, 51 is% Used [0x3b010000, 0x3b543000, 0x3b543000, 0x3ba10000)
RW Space 12288K, 55% Used [0x3ba10000, 0x3c0ae4f8, 0x3c0ae600, 0x3c610000)
if SurvivorRatio is set to 2, b1 objects stored in the younger generations. Output shown in Listing 7.
7. After modifying the operating parameters again Listing 4 Listing output
Heap
DEF new new Generation Total 7680K, 7679K Used [0x35c10000, 0x36610000, 0x36610000)
Eden Space 5120K, 100% Used [0x35c10000, 0x36110000, 0x36110000)
from space 2560K, 99% used [0x36110000, 0x3638fff0, 0x36390000)
to space 2560K, 0% used [0x36390000, 0x36390000, 0x36610000)
tenured generation total 10240K, used 10239K [0x36610000, 0x37010000, 0x37010000)
the space 10240K, 99% used [0x36610000, 0x3700fff0, 0x37010000, 0x37010000)
compacting perm gen total 12288K, used 371K [0x37010000, 0x37c10000, 0x3b010000)
the space 12288K, 3% used [0x37010000, 0x3706ce28, 0x3706d000, 0x37c10000)
ro space 10240K, 51% used [0x3b010000, 0x3b543000, 0x3b543000, 0x3ba10000)
rw space 12288K, 55% used [0x3ba10000, 0x3c0ae4f8, 0x3c0ae600, 0x3c610000)

How to make large objects into the old generation

We would choose to assign objects in the young generation in most cases. However, for large objects occupy more memory, it may choose not the case. Because large objects appear in the young generation is likely to disturb the young generation GC, and destroy the young generation of the original object structure. Because the attempt to allocate large objects in the young generation, is likely to lead to lack of space, in order to have enough space to accommodate large objects, JVM younger generation had to target young moved to the old generation. Because large object space and more, it may be necessary to move a large number of young small objects into the old generation, which is quite detrimental to the GC. For these reasons, large objects can be directly assigned to the old generation, the young generation to maintain the integrity of the object structure, which can improve the efficiency of the GC. If a large object while the object is a short-lived, assuming that this happens very frequently, it is for the GC would be a disaster. Which should be used to store persistent objects of the old generation, it is filled with short-lived objects, which means a reshuffle of the heap space, disrupting the basic idea of generational garbage collection. Therefore, in the software development process, you should avoid the use of short-lived large objects as possible.
You can use parameters
-XX: PetenureSizeThreshold large objects disposed directly into old generation threshold. When the size of the object exceeds this value, the old generation will be dispensed directly.
-XX: PetenureSizeThreshold collector only valid serial parallel collector and the young generation, parallel collector recovery does not recognize this parameter.
Listing 8 ,. Create a large object
public class BigObj20Ld {
public static void main (String [] args) {
byte [] B;
// assignment of 1MB
B = new new byte [1024 * 1024];
}
}
using the JVM parameter -XX: PrintGCDetails -Xmx20M -Xms20MB running, you can get a list of all log output 9.
清单9.清单8运行输出
Heap
def new generation total 6144K, used 1378K [0x35c10000, 0x362b0000, 0x362b0000)
eden space 5504K, 25% used [0x35c10000, 0x35d689e8, 0x36170000)
from space 640K, 0% used [0x36170000, 0x36170000, 0x36210000)
to space 640K, 0% used [0x36210000, 0x36210000, 0x362b0000)
tenured generation total 13696K, used 0K [0x362b0000, 0x37010000, 0x37010000)
the space 13696K, 0% used [0x362b0000, 0x362b0000, 0x362b0200, 0x37010000)
compacting perm gen total 12288K, used 374K [0x37010000, 0x37c10000, 0x3b010000)
the space 12288K, 3% used [0x37010000, 0x3706dac8, 0x3706dc00, 0x37c10000)
ro space 10240K, 51% used [0x3b010000, 0x3b543000, 0x3b543000, 0x3ba10000)
rw space 12288K, 55% used [ 0x3ba10000, 0x3c0ae4f8, 0x3c0ae600, 0x3c610000)
can see that the object is assigned in the younger generations, taking up 25 percent of the space. If you need more than 1MB allocated directly in the old generation of objects, provided
-XX: PetenureSizeThreshold = 1000000, the program running output Listing 10
Listing 10. 8 outputs the modified operating parameter list
Heap
DEF new new Generation Total 6144K, 354K Used [0x35c10000, 0x362b0000, 0x362b0000)
Eden Space 5504K,. 6% Used [0x35c10000, 0x35c689d8, 0x36170000)
from Space 640K, 0% Used [0x36170000, 0x36170000, 0x36210000)
to Space 640K, 0% Used [0x36210000, 0x36210000, 0x362b0000)
the tenured Total 13696K Generation, 1024K Used [0x362b0000, 0x37010000, 0x37010000)
The Space 13696K, Used. 7% [0x362b0000, 0x363b0010, 0x363b0200, 0x37010000)
Compacting Perm Gen Total 12288K, Used 374K [0x37010000, 0x37c10000, 0x3b010000)
The Space 12288K,. 3% Used [0x37010000, 0x3706dac8, 0x3706dc00, 0x37c10000)
RO Space 10240K, 51 is% Used [0x3b010000, 0x3b543000, 0x3b543000, 0x3ba10000)
RW Space 12288K, 55% Used [0x3ba10000, 0x3c0ae4f8, 0x3c0ae600, 0x3c610000)
list 10 which you can see into the full 1MB time when the old generation.

How to set the object into the old generation age

Heap Each object has its own age. Under normal circumstances, the young objects stored in the young generation, old objects stored in the old generation. To do this, a virtual machine for each object maintains an age. If the object in the Eden area, still alive after the first GC, were moved to the area in Survivor, target age plus one. Later, if the object each time through GC still alive, then the age plus one. When the target age threshold is reached, it moved to the old generation, become elderly subjects. The maximum threshold may parameter -XX: setting MaxTenuringThreshold, the default value is 15. Although -XX: MaxTenuringThreshold value may be 15 or more, but this does not mean that new objects have to reach this age to enter the old generation. In fact, the object actually entering the age old generation virtual machine is dynamically computed at run time based on memory usage, this parameter specifies the maximum threshold of age. That is, the actual promotion of the old generation ages equal to the calculated dynamic age and -XX: MaxTenuringThreshold the smaller one. 11 application code in Listing 3 a number of memory objects.

11. The application list memory
public MaxTenuringThreshold class {
public static void main (String args []) {
byte [] B1, B2, B3;
B1 = new new byte [1024 512];
B2 = new new byte [1024
1024 2];
B3 = byte new new [1024
1024 . 4];
B3 = null;
B3 = new new byte [1024
1024 *. 4];
}
}
parameter is set to: -XX: + PrintGCDetails -Xmx20M -Xms20M -Xmn10M -XX: SurvivorRatio = 2

As shown in the code 11, the output operation list as shown in Listing 12.

清单 12. 清单 11 运行输出
[GC [DefNew: 2986K->690K(7680K), 0.0246816 secs] 2986K->2738K(17920K),
0.0247226 secs] [Times: user=0.00 sys=0.02, real=0.03 secs]
[GC [DefNew: 4786K->690K(7680K), 0.0016073 secs] 6834K->2738K(17920K),
0.0016436 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
Heap
def new generation total 7680K, used 4888K [0x35c10000, 0x36610000, 0x36610000)
eden space 5120K, 82% used [0x35c10000, 0x36029a18, 0x36110000)
from space 2560K, 26% used [0x36110000, 0x361bc950, 0x36390000)
to space 2560K, 0% used [0x36390000, 0x36390000, 0x36610000)
tenured generation total 10240K, used 2048K [0x36610000, 0x37010000, 0x37010000)
the space 10240K, 20% used [0x36610000, 0x36810010, 0x36810200, 0x37010000)
Compacting Perm Gen Total 12288K, Used 374K [0x37010000, 0x37c10000, 0x3b010000)
The Space 12288K,. 3% Used [0x37010000, 0x3706db50, 0x3706dc00, 0x37c10000)
RO Space 10240K, 51 is% Used [0x3b010000, 0x3b543000, 0x3b543000, 0x3ba10000)
RW Space 12288K , 55% used [0x3ba10000, 0x3c0ae4f8 , 0x3c0ae600, 0x3c610000)
changing the parameter -XX: + PrintGCDetails -Xmx20M -Xms20M -Xmn10M -XX : SurvivorRatio = 2 -XX: MaxTenuringThreshold = 1, as shown in the code 11 running list, as the list of output 13 FIG.

清单 13. 修改运行参数后清单 11 输出
[GC [DefNew: 2986K->690K(7680K), 0.0047778 secs] 2986K->2738K(17920K),
0.0048161 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[GC [DefNew: 4888K->0K(7680K), 0.0016271 secs] 6936K->2738K(17920K),
0.0016630 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
Heap
def new generation total 7680K, used 4198K [0x35c10000, 0x36610000, 0x36610000)
eden space 5120K, 82% used [0x35c10000, 0x36029a18, 0x36110000)
from space 2560K, 0% used [0x36110000, 0x36110088, 0x36390000)
to space 2560K, 0% used [0x36390000, 0x36390000, 0x36610000)
tenured generation total 10240K, used 2738K [0x36610000, 0x37010000, 0x37010000)
the space 10240K, 26% used [0x36610000, 0x368bc890, 0x368bca00, 0x37010000)
Compacting Perm Gen Total 12288K, Used 374K [0x37010000, 0x37c10000, 0x3b010000)
The Space 12288K,. 3% Used [0x37010000, 0x3706db50, 0x3706dc00, 0x37c10000)
RO Space 10240K, 51 is% Used [0x3b010000, 0x3b543000, 0x3b543000, 0x3ba10000)
RW Space 12288K , 55% used [0x3ba10000, 0x3c0ae4f8 , 0x3c0ae600, 0x3c610000)
as shown in Listing 13, the first run b1 objects at the end of the program still preserved in the young generation. Before the second run, we reduce the age of the subject promoted to the old generation, is set to 1. That is, all through a GC objects can be directly into the old generation. Running the program can be found b1 object has been assigned to the old generation. If you want to target the young generation to stay in as long as possible, you can set a larger threshold.

Stable Java heap VS turbulent Java heap

In general, the stability of the heap size is beneficial for garbage collection. Obtain a stable heap size is to make the same size and -Xmx -Xms, i.e., the maximum and minimum heap stack (the initial heap) the same. If so provided, the size of the heap at run-time system is theoretically constant, stable GC heap space can reduce the number of times. Thus, many server applications are the maximum and minimum heap stack to the same value. However, an unstable heap is not useless. Although stable GC heap size can reduce the number, but also increases the GC every time. Let heap size in a range of shocks, when the system does not require large memory, compression heap, the GC to deal with a smaller stack, can accelerate the speed of a single GC. Based on this consideration, JVM also provides compression and expansion heap space for two parameters.

-XX: MinHeapFreeRatio parameter used to set the ratio of the minimum free heap space, the default value is 40. When the free space in the heap memory is less than this value, JVM will expand the heap space.

-XX: MaxHeapFreeRatio parameter used to set the maximum idle heap space ratio, the default value is 70. When the free space in the heap memory is greater than this value, it will heap space compression to give a smaller heap.

When equal -Xmx and -Xms, -XX: MinHeapFreeRatio and -XX: MaxHeapFreeRatio two parameter is invalid.

Listing 14. heap size
import java.util.Vector;

HeapSize {class public
public static void main (String args []) {throws InterruptedException
the Vector new new V = the Vector ();
the while (to true) {
byte [] = new new byte B [1024 * 1024];
v.add (B);
IF (v.size () == 10) {
V = new new the Vector ();
}
the Thread.sleep (. 1);
}
}
}
Listing test code 14 -XX: MinHeapFreeRatio and -XX: MaxHeapFreeRatio action is provided operating parameters -XX: + PrintGCDetails -Xms10M -Xmx40M -XX: MinHeapFreeRatio = 40 -XX: MaxHeapFreeRatio = 50, output 15 as shown in Listing.

Outputs the modified operating parameter list 15. The list 14
[the GC [DefNew: 2418K-> 178K (3072K), 0.0034827 secs] 2418K-> 2226K (9920K),
0.0035249 secs] [Times: User SYS = 0.00 = 0.00, = 0.03 Real secs]
[the GC [DefNew: 2312K-> 0K (3072K), .0028263 secs] 4360K-> 4274K (9920K),
.0029905 secs] [Times: User SYS = 0.00 = 0.00, 0.03 secs = Real]
[the GC [DefNew: 2068K -> 0K (3072K), .0024363 secs] 6342K-> 6322K (9920K),
0.0024836 secs] [Times: User SYS = 0.00 = 0.00, 0.03 secs = Real]
[the GC [DefNew: 2061K-> 0K (3072K), .0017376 secs] [a position of tenured: 8370K-> 8370K (8904K),
.1392692 secs] 8384K-> 8370K (11976K), [Perm: 374K-> 374K (12288K)],
.1411363 secs] [Times: User SYS = 0.00 = 0.02, Real 0.16 secs =]
[the GC [DefNew: 5138K-> 0K (6336K), 0.0038237 secs] 13508K-> 13490K (20288K),
0.0038632 secs] [Times: user = 0.00 sys = 0.00, real = 0.03 secs]
use parameters: -XX: + PrintGCDetails -Xms40M -Xmx40M -XX : MinHeapFreeRatio = 40 -XX: MaxHeapFreeRatio = 50, operating as an output list 16 shows.

Listing 16. Output After modifying the operating parameters list again 14
[GC [DefNew: 10678K-> 178K (12288K), .0019448 secs] 10678K-> 178K (39616K),
0.0019851 secs] [Times: the User = 0.00 = 0.00 SYS, Real = secs 0.03]
[the GC [DefNew: 10751K-> 178K (12288K), .0010295 secs] 10751K-> 178K (39616K),
0.0010697 secs] [Times: User SYS = 0.00 = 0.00, 0.02 secs = Real]
[the GC [DefNew: 10493K-> 178K (12288K), 0.0008301 secs] 10493K-> 178K (39616K),
0.0008672 secs] [Times: User SYS = 0.00 = 0.00, 0.02 secs = Real]
[the GC [DefNew: 10467K-> 178K (12288K), secs .0008522] 10467K-> 178K (39616K),
0.0008905 secs] [Times: User SYS = 0.00 = 0.00, 0.02 secs = Real]
[the GC [DefNew: 10450K-> 178K (12288K), 0.0008964 secs] 10450K-> 178K ( 39616K),
.0009339 secs] [Times: User SYS = 0.00 = 0.00, 0.01 secs = Real]
[The GC [DefNew: 10439K-> 178K (12288K), .0009876 secs] 10439K-> 178K (39616K),
0.0010279 secs] [Times: User SYS = 0.00 = 0.00, 0.02 secs = Real]
from the list 16 can be seen, this when garbage collection heap space stabilized at a fixed range. In a stable stack, heap space is always the same, every time the GC, must deal with a 40MB of space. Thus, while reducing the number of GC, GC but slower than a single shock heap.

Increasing throughput improve system performance

Throughput priority programs will minimize the total execution time garbage collection system, it can be considered concerns the recovery system throughput parallel collector. On computers with a high-performance, optimization for throughput of priority parameters may be used:
Java -Xmx3800m -Xms3800m -Xmn2G -Xss128k -XX: + UseParallelGC
-XX: Threads ParallelGC--XX = 20 is: + UseParallelOldGC
-Xmx380m -Xms3800m: setting an initial value and a maximum value of the Java heap. In general, in order to avoid frequent shocks heap memory, resulting in system performance degradation, our approach is to set the maximum heap equal to the minimum heap. The assumption here is reduced to the minimum heap heap of half the maximum, that is 1900m, the JVM will try to run at 1900MB heap space, and if so, the likelihood of the occurrence of GC will be higher;
-Xss128k: reduce the thread stack size, so the remaining system memory can support more threads;
-Xmn2g: the young generation area size is set 2GB;
-XX: + UseParallelGC: the young generation using parallel garbage collector. This is a concern throughput collector can reduce the GC time as possible.
-XX: ParallelGC-Threads: the number of threads provided for garbage collection, in general, may be provided and an equal number of CPU. However, in the case where more the number of CPU, a relatively small set of values is reasonable;
-XX: + UseParallelOldGC: disposed parallel using recycled old generation collector.

Try to use large memory pages

CPU is by addressing to access memory. CPU address width of 32 bits is 0 ~ 0xFFFFFFFF, the calculated size 4G, that is to support the maximum physical memory is 4G. But in practice, I encountered such a problem, the program requires the use of 4G memory, available physical memory less than 4G, causing the program had to reduce the memory footprint. To solve such problems, the introduction of modern CPU MMU (Memory Management Unit memory management unit). MMU's core idea is to use alternate virtual address physical address, namely the use of virtual addresses when the CPU is not addressed by the MMU is responsible for mapping virtual addresses to physical addresses. MMU introduced to address the limitations of physical memory, the program is, as they're using the same 4G memory. Paging (Paging) is a memory management mechanism based on the use of the MMU, raised. It virtual addresses and physical addresses is divided by a fixed size (4K) to a page (page) and a page frame (page frame), and to ensure that the same page size of the page frame. This mechanism, from the data structure, to ensure efficient memory access, and non-continuous support OS memory allocation. When the program memory is not enough, you can also less commonly used physical memory pages transferred to other storage devices such as disk, which is familiar to everyone virtual memory.
On Solaris systems, JVM can support the use of Large Page Size. Using the large memory pages can be enhanced addressing capability of the CPU memory, so as to enhance system performance.
-Xmx2506m -Xms2506m -Xmn1536m -Xss128k -XX Java: ++ UseParallelGC
-XX: ParallelGCThreads = 20 is -XX: + UseParallelOldGC -XX: 256M + LargePageSizeInBytes =
-XX: + LargePageSizeInBytes: provided the large page size.
Excessive paging can cause the JVM Heap calculated internal partition (perm, new, old) memory footprint when the ratio exceeds the nominal value will be divided, occupied an area the size of a page will be more of the worst case.

Use of non-possession of the garbage collector

To reduce garbage collection pauses when the application software, the first consideration is the use of the system pauses attention of CMS collector, secondly, to reduce the number of Full GC, as far as possible to reserve objects in the young generation, because the cost of the young generation Minor GC far less than the old generation of Full GC.
-Xmx3550m -Xms3550m -Xmn2g -Xss128k -XX Java: ParallelGCThreads = 20 is
-XX: + UseConcMarkSweepGC -XX: + UseParNewGC -XX: + = SurvivorRatio -XX. 8: 90 TargetSurvivorRatio =
-XX: MaxTenuringThreshold = 31 is
-XX: = 20 is ParallelGCThreads : set 20 threads for garbage collection;
-XX: + UseParNewGC: the young generation using parallel collector;
-XX: + UseConcMarkSweepGC: use CMS old generation collector reduces pause;
-XX: + SurvivorRatio: set the Eden area and Survivor areas a ratio of 8: 1. Survivor slightly larger space can increase the likelihood of recovery is short life cycle of objects in the young generation, if Survivor is not big enough, some of the short-lived objects may go directly to the old generation, which is detrimental to the system.
-XX: TargetSurvivorRatio = 90: Usage Survivor setting area. Here set to 90%, 90% Survivor allows space to be used. The default value is 50%. Therefore, this arrangement increases the area utilization Survivor. When the object is located more than this percentage, the object will be compressed to the old generation. Therefore, this option is more useful objects left in the younger generations.
-XX: MaxTenuringThreshold: Set objects promoted to the old generation of young age. The default value is 15 times that target after 15 Minor GC still alive, then enter the old generation. Here is set to 31, the purpose is to make the object as much as possible to save the young generation in the region.

Guess you like

Origin www.cnblogs.com/haizai/p/11247060.html