例子
- -Xmn1m代表年轻代几乎没有空间。对象都到老年代了。
- 类PrintThread是打印线程,每100毫秒打印一次,如何应用线程暂停了,那么会延迟。
- 类MyThread是消耗jvm内存的线程,每到450M,会让对象失去引用。
```java
/*
- 启动参数:-Xmx512m -Xms512m -XX:+UseSerialGC -XX:+PrintGCDetails -Xmn1m *
- -XX:+UseSerialGC:新生代和老年代都用单线程的串行回收器。适合单核并发能力差得处理器。
- -XX:+UseParNewGC:新生代用并行的ParNew回收期,老年代都用单线程的串行回收器。适合多核,并发能力强的处理器。
- -XX:+UseParallelGC:新生代使用ParallelGC回收器,老年代使用串行回收器。 -XX:+UseParallelOldGC:新生代使用ParallelGC回收器,老年代使用ParallelOldGC回收器。
- 1)—
- -XX:+UseConclMarkSweepGC:老年代使用CMS回收器。 *
- Created by chenyang on 20
- 17/2/2. */
public class StopWorldTest {
public static class MyThread extends Thread{
HashMap<Long,byte[]> map=new HashMap<Long, byte[]>();
@Override
public void run() {
try {
while (true){
if(map.size()*512/1024/1024>=450){
System.out.println("============准备清理==========:"+map.size());//大于450M时,清理内存。
map.clear();
System.out.println("clean map");
}
for(int i=0;i<100;i++){
map.put(System.nanoTime(),new byte[512]);//消耗内存。
}
Thread.sleep(1);
}
}catch (Exception e){
e.printStackTrace();
}
}
}
public static class PrintThread extends Thread {
public static final long starttime = System.currentTimeMillis();
@Override
public void run() {
try {
while (true) {
long t = System.currentTimeMillis() - starttime;
System.out.println("time:" + t);
Thread.sleep(100);
}
} catch (Exception e) {
}
}
}
public static void main(String[] args) {
MyThread t=new MyThread();
PrintThread p=new PrintThread();
t.start();
p.start();
}
}
gc日志:
```java
14.381
[GC (Allocation Failure) [DefNew: 959K->63K(960K), 0.0044628 secs] 511175K->511171K(524224K), 0.0044856 secs] [Times: user=0.00 sys=0.00, real=0.01 secs]
[GC (Allocation Failure) [DefNew: 959K->64K(960K), 0.0048768 secs] 512067K->512064K(524224K), 0.0048978 secs] [Times: user=0.01 sys=0.00, real=0.00 secs]
[GC (Allocation Failure) [DefNew: 960K->63K(960K), 0.0049033 secs] 512960K->512956K(524224K), 0.0049314 secs] [Times: user=0.00 sys=0.00, real=0.01 secs]
[GC (Allocation Failure) [DefNew: 959K->63K(960K), 0.0047410 secs] 513852K->513848K(524224K), 0.0047609 secs] [Times: user=0.01 sys=0.00, real=0.00 secs]
14.486
[GC (Allocation Failure) [DefNew: 959K->64K(960K), 0.0058544 secs] 514744K->514740K(524224K), 0.0058874 secs] [Times: user=0.00 sys=0.00, real=0.01 secs]
[GC (Allocation Failure) [DefNew: 960K->64K(960K), 0.0045730 secs] 515636K->515635K(524224K), 0.0045907 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[GC (Allocation Failure) [DefNew: 960K->63K(960K), 0.0053326 secs] 516531K->516527K(524224K), 0.0053546 secs] [Times: user=0.01 sys=0.00, real=0.00 secs]
[GC (Allocation Failure) [DefNew: 959K->64K(960K), 0.0049539 secs] 517423K->517418K(524224K), 0.0049781 secs] [Times: user=0.00 sys=0.00, real=0.01 secs]
14.590
[GC (Allocation Failure) [DefNew: 960K->63K(960K), 0.0046786 secs] 518314K->518311K(524224K), 0.0047022 secs] [Times: user=0.01 sys=0.00, real=0.00 secs]
[GC (Allocation Failure) [DefNew: 959K->63K(960K), 0.0041492 secs] 519207K->519204K(524224K), 0.0041714 secs] [Times: user=0.00 sys=0.00, real=0.01 secs]
[GC (Allocation Failure) [DefNew: 959K->63K(960K), 0.0050918 secs] 520100K->520096K(524224K), 0.0051230 secs] [Times: user=0.01 sys=0.00, real=0.00 secs]
[GC (Allocation Failure) [DefNew: 959K->63K(960K), 0.0041798 secs] 520992K->520987K(524224K), 0.0042189 secs] [Times: user=0.00 sys=0.01, real=0.01 secs]
14.695
[GC (Allocation Failure) [DefNew: 959K->63K(960K), 0.0037032 secs] 521883K->521879K(524224K), 0.0037310 secs] [Times: user=0.01 sys=0.00, real=0.00 secs]
[GC (Allocation Failure) [DefNew: 959K->64K(960K), 0.0052917 secs] 522775K->522774K(524224K), 0.0053151 secs] [Times: user=0.00 sys=0.00, real=0.01 secs]
[GC (Allocation Failure) [DefNew: 960K->960K(960K), 0.0000131 secs][Tenured: 522710K->523263K(523264K), 0.4556888 secs] 523670K->523666K(524224K), [Metaspace: 3131K->3131K(1056768K)], 0.4557355 secs] [Times: user=0.45 sys=0.00, real=0.46 secs]
15.212
[Full GC (Allocation Failure) [Tenured: 523263K->523263K(523264K), 0.3951695 secs] 524223K->524218K(524224K), [Metaspace: 3131K->3131K(1056768K)], 0.3951971 secs] [Times: user=0.40 sys=0.00, real=0.39 secs]
[Full GC (Allocation Failure) [Tenured: 523263K->523263K(523264K), 0.3942671 secs] 524223K->524222K(524224K), [Metaspace: 3131K->3131K(1056768K)], 0.3942906 secs] [Times: user=0.39 sys=0.01, real=0.40 secs]
[Full GC (Allocation Failure) [Tenured: 523263K->516027K(523264K), 0.4641148 secs] 524222K->516027K(524224K), [Metaspace: 3131K->3131K(1056768K)], 0.4641369 secs] [Times: user=0.46 sys=0.00, real=0.46 secs]
15.619
[GC (Allocation Failure) [DefNew: 896K->64K(960K), 0.0025171 secs] 516923K->516887K(524224K), 0.0025344 secs] [Times: user=0.01 sys=0.00, real=0.01 secs]
[GC (Allocation Failure) [DefNew: 960K->63K(960K), 0.0042796 secs] 517783K->517781K(524224K), 0.0043039 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[GC (Allocation Failure) [DefNew: 959K->63K(960K), 0.0053306 secs] 518677K->518670K(524224K), 0.0053572 secs] [Times: user=0.01 sys=0.00, real=0.00 secs]
[GC (Allocation Failure) [DefNew: 959K->63K(960K), 0.0052464 secs] 519566K->519564K(524224K), 0.0052719 secs] [Times: user=0.00 sys=0.00, real=0.01 secs]
16.583
[GC (Allocation Failure) [DefNew: 959K->64K(960K), 0.0050433 secs] 520460K->520444K(524224K), 0.0050696 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[GC (Allocation Failure) [DefNew: 960K->64K(960K), 0.0050287 secs] 521340K->521333K(524224K), 0.0050566 secs] [Times: user=0.01 sys=0.00, real=0.01 secs]
[GC (Allocation Failure) [DefNew: 960K->63K(960K), 0.0059352 secs] 522229K->522227K(524224K), 0.0059838 secs] [Times: user=0.00 sys=0.00, real=0.01 secs]
[GC (Allocation Failure) [DefNew: 959K->63K(960K), 0.0051672 secs] 523123K->523111K(524224K), 0.0051959 secs] [Times: user=0.00 sys=0.00, real=0.01 secs]
16.687
[GC (Allocation Failure) [DefNew: 959K->959K(960K), 0.0000174 secs][Tenured: 523047K->523263K(523264K), 0.4289996 secs] 524007K->523991K(524224K), [Metaspace: 3131K->3131K(1056768K)], 0.4290569 secs] [Times: user=0.43 sys=0.00, real=0.43 secs]
17.141
[Full GC (Allocation Failure) [Tenured: 523263K->523263K(523264K), 0.3752676 secs] 524223K->524219K(524224K), [Metaspace: 3131K->3131K(1056768K)], 0.3753102 secs] [Times: user=0.37 sys=0.01, real=0.38 secs]
[Full GC (Allocation Failure) [Tenured: 523263K->523263K(523264K), 0.3974346 secs] 524223K->524223K(524224K), [Metaspace: 3131K->3131K(1056768K)], 0.3974563 secs] [Times: user=0.40 sys=0.00, real=0.39 secs]
[Full GC (Allocation Failure) [Tenured: 523263K->523263K(523264K), 0.4509967 secs] 524223K->524223K(524224K), [Metaspace: 3131K->3131K(1056768K)], 0.4510171 secs] [Times: user=0.45 sys=0.00, real=0.46 secs]
17.522
[Full GC (Allocation Failure) [Tenured: 523263K->523263K(523264K), 0.3911804 secs] 524223K->524223K(524224K), [Metaspace: 3131K->3131K(1056768K)], 0.3912023 secs] [Times: user=0.39 sys=0.00, real=0.39 secs]
18.762[Full GC (Allocation Failure) [Tenured: 523263K->523263K(523264K), 0.4247627 secs] 524223K->524223K(524224K), [Metaspace: 3131K->3131K(1056768K)], 0.4247850 secs] [Times: user=0.42 sys=0.00, real=0.42 secs]
[Full GC (Allocation Failure) [Tenured: 523263K->523263K(523264K), 0.4053384 secs] 524223K->524223K(524224K), [Metaspace: 3131K->3131K(1056768K)], 0.4053664 secs] [Times: user=0.41 sys=0.00, real=0.41 secs]
[Full GC (Allocation Failure) [Tenured: 523263K->523263K(523264K), 0.3821490 secs] 524223K->524223K(524224K), [Metaspace: 3131K->3131K(1056768K)], 0.3821753 secs] [Times: user=0.38 sys=0.00, real=0.38 secs]
19.593[Full GC (Allocation Failure) [TenuredException in thread "Thread-0" : 523263K->523263K(523264K), 0.3890329 secs] 524223K->524223K(524224K), [Metaspace: 3131K->3131K(1056768K)], 0.3890547 secs] [Times: user=0.39 sys=0.01, real=0.39 secs]
说明:
- 17.141秒前都是100ms打印一次,线程没有停顿。但是之后发生了因full gc引发零点几秒甚至将近1s的停顿。
- 因为对象的引用还在,老年代的对象不会被回收。如:
[Full GC (Allocation Failure) [Tenured: 523263K->523263K(523264K),