例子

  • -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 {

  1. public static class MyThread extends Thread{
  2. HashMap<Long,byte[]> map=new HashMap<Long, byte[]>();
  3. @Override
  4. public void run() {
  5. try {
  6. while (true){
  7. if(map.size()*512/1024/1024>=450){
  8. System.out.println("============准备清理==========:"+map.size());//大于450M时,清理内存。
  9. map.clear();
  10. System.out.println("clean map");
  11. }
  12. for(int i=0;i<100;i++){
  13. map.put(System.nanoTime(),new byte[512]);//消耗内存。
  14. }
  15. Thread.sleep(1);
  16. }
  17. }catch (Exception e){
  18. e.printStackTrace();
  19. }
  20. }
  21. }
  22. public static class PrintThread extends Thread {
  23. public static final long starttime = System.currentTimeMillis();
  24. @Override
  25. public void run() {
  26. try {
  27. while (true) {
  28. long t = System.currentTimeMillis() - starttime;
  29. System.out.println("time:" + t);
  30. Thread.sleep(100);
  31. }
  32. } catch (Exception e) {
  33. }
  34. }
  35. }
  36. public static void main(String[] args) {
  37. MyThread t=new MyThread();
  38. PrintThread p=new PrintThread();
  39. t.start();
  40. p.start();
  41. }

}

  1. gc日志:
  2. ```java
  3. 14.381
  4. [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]
  5. [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]
  6. [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]
  7. [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]
  8. 14.486
  9. [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]
  10. [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]
  11. [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]
  12. [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]
  13. 14.590
  14. [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]
  15. [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]
  16. [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]
  17. [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]
  18. 14.695
  19. [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]
  20. [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]
  21. [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]
  22. 15.212
  23. [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]
  24. [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]
  25. [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]
  26. 15.619
  27. [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]
  28. [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]
  29. [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]
  30. [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]
  31. 16.583
  32. [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]
  33. [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]
  34. [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]
  35. [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]
  36. 16.687
  37. [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]
  38. 17.141
  39. [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]
  40. [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]
  41. [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]
  42. 17.522
  43. [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]
  44. 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]
  45. [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]
  46. [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]
  47. 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),