Serial collector for JAVA Garbage Collector
1. Features
The Serial collector is the most basic and oldest collector in the Java Virtual Machine. It was the only choice for collecting new generations of Java virtual machines before JDK 1.3.1. The Serial collector is a single-threaded collector, but its "Single-threaded" doesn't just mean that it only uses one CPU or one collection thread to complete garbage collection, more importantly, when collecting garbage, you must pause all other working threads until the collection ends.
If the server is suspended for five minutes every hour, what kind of mood will the boss feel?
2. Development
From JDK 1.3 to JDK 1.7, which has not yet been officially released, the HotSpot Virtual Machine Development team has been working hard to eliminate or reduce the pause caused by memory collection by working threads, from the Serial collector to the Parallel collector, to the Concurrent Mark Sweep (CMS collector) Garbage First (G1) Collector that has not yet been officially released, we see more and more excellent (and more complex) collectors) the pause time of the user thread is continuously shortened, but there is still no way to completely eliminate it.
3. Status quo
Until now, the Serial collector is still the default New Generation collector for Java virtual machines running in Client mode. It also has advantages over other collectors: simple and efficient (compared with the single thread of other collectors). For a single CPU environment, the Serial collector has no thread interaction overhead, focusing on garbage collection can naturally achieve the highest single-thread collection efficiency. In users' desktop application scenarios, the memory allocated to Virtual Machine Management is generally not very large, and the new generation that collects dozens or even 100 or 200 MB (only the memory used by the new generation, the pause time can be controlled within dozens of milliseconds up to one hundred milliseconds. As long as it does not happen frequently, this pause is acceptable. Therefore, the Serial collector is a good choice for virtual machines running in Client mode.
4. Code and example (fixed size of the Java Virtual Machine)
Package com. gc;
Import java. util. ArrayList;
Import java. util. List;
/**
* Simple memory recovery for Java virtual machines and use of the serial collector
* Parameter:-Xms30m-Xmx30m-Xmn10m-XX: + UseSerialGC-XX: + PrintGCDetails
* @ Author fan fangming
*/
Public class EasySerial {
Public byte [] placeHolder = new byte [64*1024]; // placeHolder
Public static voidmain (String [] args) throws Exception {
OutOfMemoryByFixSize ();
}
/**
* Fixed the size of the Java VM.
* Parameter:-Xms30m-Xmx30m-Xmn10m-XX: + UseSerialGC-XX: + PrintGCDetails
* @ Author fan fangming
*/
Private static voidoutOfMemoryByFixSize () throws Exception {
List <EasySerial> list = new ArrayList <EasySerial> ();
While (true ){
EasySerialserial = new EasySerial ();
List. add (serial );
Thread. sleep (10); // pause for 10 ms
}
}
/**
* The size of the Java Virtual Machine can be expanded as appropriate, including Xms30m and Xmx40m.
* Parameter:-Xms30m-Xmx40m-XX: + UseSerialGC-XX: + PrintGCDetails
* @ Author fan fangming
*/
Private static voidoutOfMemoryByExpansionSize () throws Exception {
List <EasySerial> list = new ArrayList <EasySerial> ();
While (true ){
EasySerialserial = new EasySerial ();
List. add (serial );
Thread. sleep (10); // pause for 10 ms
}
}
}
The parameter:-Xms30m-Xmx30m-Xmn10m-XX: + UseSerialGC-XX: + PrintGCDetails
-XX: + UseSerialGC is the Serial collector. Xms30m-Xmx30m specifies a fixed size of 30 M for the JAVA Virtual Machine.-Xmn10m indicates that the size of the new JAVA generation is 10 M.
The running result is as follows:
[GC [DefNew: 8137 K-> 1023 K (9216 K), 0.0054427 secs] 8137 K-> 8019 K (29696 K), 0.0054763 secs] [Times: user = 0.00sys = 0.00, real = 0.01 secs]
[GC [DefNew: 9175 K-> 963 K (9216 K), 0.0056574 secs] 16170 K-> 16151 K (29696 K), 0.0056820 secs] [Times: user = 0.00sys = 0.00, real = 0.00 secs]
[GC [DefNew: 9149 K-> 9149 K (9216 K), 0.0000197 secs] [Tenured: 15188 K-> 20441 K (20480 K ), 0.0065753 secs] 24337 K-> 24284 K (29696 K), [Perm: 2086 K-> 2086 K (12288 K)], 0.0066451 secs] [Times: user = 0.00 sys = 0.00, real = 0.01 secs]
[Full GC [Tenured: 20441 K-> 20441 K (20480 K), 0.0038802 secs] 29570 K-> 29537 K (29696 K), [Perm: 2086 K-> 2086 K (12288 K)], 0.0039147 secs] [Times: user = 0.00 sys = 0.00, real = 0.00 secs]
[Full GC [Tenured: 20441 K-> 20436 K (20480 K), 0.0061865 secs] 29537 K-> 29532 K (29696 K), [Perm: 2086 K-> 2084 K (12288 K)], 0.0062235 secs] [Times: user = 0.00 sys = 0.00, real = 0.01 secs]
Exception in thread "main" java. lang. OutOfMemoryError: Java heap space
Atcom. gc. EasySerial. <init> (EasySerial. java: 12)
Atcom. gc. EasySerial. outOfMemoryByFixSize (EasySerial. java: 25)
Atcom. gc. EasySerial. main (EasySerial. java: 14)
Heap
Def new generation total 9216 K, used 9152 K [0x03bd0000, 0x045d0000, 0x045d0000)
Eden space 8192 K, 100% used [0x03bd0000, 0x043d0000, 0x043d0000)
From space 1024 K, 93% used [0x043d0000, 0x044c00f0, 0x044d0000)
To space 1024 K, 0% used [0x044d0000, 0x044d0000, 0x045d0000)
Tenured generation total 20480 K, used 20436 K [0x045d0000, 0x059d0000, 0x059d0000)
The space 20480 K, 99% used [0x045d0000, 0x059c52b8, 0x059c5400, 0x059d0000)
Compacting perm gen total 12288 K, used 2105 K [0x059d0000, 0x065d0000, 0x099d0000)
...
GC [DefNew: it is a collection of new generations. It is clear that the space of the new generation is quickly used up.
Full GC [Tenured is a Full GC collection in the old age, and the space is quickly used up. Then, an OutOfMemoryError occurs, resulting in a system crash.
5. Code and examples (Scalable Java Virtual Machine size)
Package com. gc;
Import java. util. ArrayList;
Import java. util. List;
/**
* Simple memory recovery for Java virtual machines and use of the serial collector
* Parameter:-Xms30m-Xmx30m-Xmn10m-XX: + UseSerialGC-XX: + PrintGCDetails
* @ Author fan fangming
*/
Public class EasySerial {
Public byte [] placeHolder = new byte [64*1024]; // placeHolder
Public static voidmain (String [] args) throws Exception {
OutOfMemoryByExpansionSize ();
}
/**
* Fixed the size of the Java VM.
* Parameter:-Xms30m-Xmx30m-Xmn10m-XX: + UseSerialGC-XX: + PrintGCDetails
* @ Author fan fangming
*/
Private static voidoutOfMemoryByFixSize () throws Exception {
List <EasySerial> list = new ArrayList <EasySerial> ();
While (true ){
EasySerialserial = new EasySerial ();
List. add (serial );
Thread. sleep (10); // pause for 10 ms
}
}
/**
* The size of the Java Virtual Machine can be expanded as appropriate, including Xms30m and Xmx40m.
* Parameter:-Xms30m-Xmx40m-XX: + UseSerialGC-XX: + PrintGCDetails
* @ Author fan fangming
*/
Private static voidoutOfMemoryByExpansionSize () throws Exception {
List <EasySerial> list = new ArrayList <EasySerial> ();
While (true ){
EasySerialserial = new EasySerial ();
List. add (serial );
Thread. sleep (10); // pause for 10 ms
}
}
}
The parameter:-Xms30m-Xmx30m-Xmn10m-XX: + UseSerialGC-XX: + PrintGCDetails
-XX: + UseSerialGC is the Serial collector. Xms30m-Xmx40m specifies that the size of the JAVA Virtual Machine can be increased from 30 M to 40 M, without specifying the size of the new JAVA generation space.
Status after running:
[GC [DefNew: 1986 K-> 128 K (2112 K), 0.0011191 secs] 27809 K-> 27808 K (30528 K), 0.0011425 secs] [Times: user = 0.00sys = 0.01, real = 0.00 secs]
[GC [DefNew: 1989 K-> 131 K (2112 K), 0.0011326 secs] [Tenured: 29536 K-> 29539 K (29568 K ), 0.0032603 secs] 29669 K-> 29667 K (31680 K), [Perm: 2086 K-> 2086 K (12288 K)], 0.0044714 secs] [Times: user = 0.00 sys = 0.00, real = 0.00 secs]
[GC [DefNew: 2562 K-> 192 K (2880 K), 0.0024077 secs] 32102 K-> 32100 K (40704 K), 0.0024426 secs] [Times: user = 0.00sys = 0.00, real = 0.00 secs]
[GC [DefNew: 2755 K-> 192 K (2880 K), 0.0015362 secs] 34663 K-> 34662 K (40704 K), 0.0015731 secs] [Times: user = 0.00sys = 0.00, real = 0.00 secs]
[GC [DefNew: 2755 K-> 192 K (2880 K), 0.0015842 secs] 37224 K-> 37223 K (40704 K), 0.0016187 secs] [Times: user = 0.00sys = 0.00, real = 0.00 secs]
[GC [DefNew: 2755 K-> 2755 K (2880 K), 0.0000144 secs] [Tenured: 37030 K-> 37799 K (37824 K ), 0.0039907 secs] 39786 K-> 39784 K (40704 K), [Perm: 2086 K-> 2086 K (12288 K)], 0.0040547 secs] [Times: user = 0.00 sys = 0.00, real = 0.00 secs]
[Full GC [Tenured: 37799 K-> 37799 K (37824 K), 0.0032501 secs] 40554 K-> 40553 K (40704 K), [Perm: 2086 K-> 2086 K (12288 K)], 0.0032940 secs] [Times: user = 0.00 sys = 0.00, real = 0.00 secs]
[Full GC [Tenured: 37799 K-> 37792 K (37824 K), 0.0107478 secs] 40553 K-> 40546 K (40704 K), [Perm: 2086 K-> 2084 K (12288 K)], 0.0107782 secs] [Times: user = 0.02 sys = 0.00, real = 0.01 secs]
Exception in thread "main" java. lang. OutOfMemoryError: Java heap space
Atcom. gc. EasySerial. <init> (EasySerial. java: 12)
Atcom. gc. EasySerial. outOfMemoryByExpansionSize (EasySerial. java: 39)
Atcom. gc. EasySerial. main (EasySerial. java: 14)
Heap
Def new generation total 2880 K, used 2810 K [0x03ca0000, 0x03fb0000, 0x03fb0000)
Eden space 2624 K, 99% used [0x03ca0000, 0x03f2e8e8, 0x03f30000)
From space 256 K, 75% used [0x03f70000, 0x03fa0030, 0x03fb0000)
To space 256 K, 0% used [0x03f30000, 0x03f30000, 0x03f70000)
Tenured generation total 37824 K, used 37792 K [0x03fb0000, 0x064a0000, 0x064a0000)
The space 37824 K, 99% used [0x03fb0000, 0x064982e0, 0x06498400, 0x064a0000)
Compacting perm gen total 12288 K, used 2105 K [0x064a0000, 0x070a0000, 0x0a4a0000)
The space 12288 K, 17% used [0x064a0000, 0x066ae4a8, 0x066ae600, 0x070a0000)
The other content is basically the same as the fixed size. There is one more case:
[GC [DefNew: 2755 K-> 2755 K (2880 K), 0.0000144 secs] [Tenured: 37030 K-> 37799 K (37824 K ), 0.0039907 secs] 39786 K-> 39784 K (40704 K), [Perm: 2086 K-> 2086 K (12288 K)], 0.0040547 secs] [Times: user = 0.00 sys = 0.00, real = 0.00 secs]
This is the secondary garbage collection of new generation, old generation, and permanent generation.