package finalizer; import java.lang.reflect.Field; import java.util.concurrent.ForkJoinPool; import java.util.concurrent.atomic.LongAdder; /* Original: Construction threads: 1 ~5s interval cumulative active pool queued t[ms] constr finaliz in-flight con./ms fin./ms con./ms fin./ms thr. size tasks ------- ---------- ---------- ---------- ------- ------- ------- ------- ----- ----- ---------- 2733 4552255 495356 4056899 1665 181 1665 181 4437 6869350 590684 6278666 1360 55 1548 133 7937 11343371 796067 10547304 1278 58 1429 100 9036 16204571 950087 15254484 4422 140 1793 105 10110 18482860 1042374 17440486 2121 85 1828 103 12258 18594655 1045190 17549465 52 1 1516 85 13330 21936489 1561325 20375164 3119 481 1645 117 14331 25278892 1633066 23645826 3339 71 1763 113 20899 29406705 1742676 27664029 628 16 1407 83 21900 32924441 1899495 31024946 3515 156 1503 86 22940 36174941 2490431 33684510 3122 567 1576 108 28852 39142647 2618761 36523886 502 21 1356 90 29905 42362955 4382309 37980646 3058 1674 1416 146 30926 45714466 4871099 40843367 3282 478 1478 157 31936 47455558 5069885 42385673 1724 196 1485 158 40455 48138858 5090117 43048741 80 2 1189 125 41471 51686700 5197400 46489300 3493 105 1246 125 42471 55249319 5329676 49919643 3560 132 1300 125 53835 57668662 5423956 52244706 212 8 1071 100 CHOKED! Construction threads: 4 ~5s interval cumulative active pool queued t[ms] constr finaliz in-flight con./ms fin./ms con./ms fin./ms thr. size tasks ------- ---------- ---------- ---------- ------- ------- ------- ------- ----- ----- ---------- 1000 3475552 494504 2981048 3475 494 3475 494 2851 4444318 494504 3949814 523 0 1558 173 4929 6724067 616577 6107490 1096 58 1364 125 8281 11127734 964418 10163316 1313 103 1343 116 9282 14517890 1155531 13362359 3387 190 1564 124 10283 17080201 1299314 15780887 2560 143 1660 126 12621 17153264 1377808 15775456 31 33 1359 109 13641 19804609 1580725 18223884 2600 198 1451 115 14675 22565086 1717606 20847480 2670 132 1537 117 15676 25207830 1835236 23372594 2640 117 1608 117 19534 28163360 1970044 26193316 765 34 1441 100 20622 30418854 2084196 28334658 2073 104 1475 101 24055 32448231 2255737 30192494 591 49 1348 93 25089 35294904 2529136 32765768 2752 264 1406 100 26090 37548690 2891623 34657067 2252 362 1439 110 27115 40210619 3094582 37116037 2597 198 1482 114 28166 42855729 3320392 39535337 2515 214 1521 117 37121 43932296 3421177 40511119 120 11 1183 92 38166 46203759 3655243 42548516 2174 224 1210 95 39168 48010571 3786531 44224040 1803 131 1225 96 40168 50878795 3991861 46886934 2867 205 1266 99 41168 53418122 4446871 48971251 2538 454 1297 108 51497 54204364 4525862 49678502 76 7 1052 87 54399 54875490 4593485 50282005 231 23 1008 84 57409 55117771 4607255 50510516 80 4 960 80 101983 55120975 4615248 50505727 0 0 540 45 CHOKED! Patched: Construction threads: 1 ~5s interval cumulative active pool queued t[ms] constr finaliz in-flight con./ms fin./ms con./ms fin./ms thr. size tasks ------- ---------- ---------- ---------- ------- ------- ------- ------- ----- ----- ---------- 1986 4286675 492209 3794466 2157 247 2157 247 7 7 1249 3298 8022389 4548433 3473956 2847 3092 2432 1379 7 7 795 4299 12636846 10913943 1722903 4609 6358 2939 2538 1 7 0 5300 17407671 14033589 3374082 4764 3115 3284 2647 1 7 0 6301 20178013 18431772 1746241 2767 4393 3202 2924 1 7 0 7302 23641069 22334864 1306205 3460 3899 3237 3058 1 7 0 8609 28135705 22334864 5800841 3439 0 3268 2594 7 7 1205 9962 33121763 28135705 4986058 3683 4285 3324 2824 7 7 790 11810 40251928 33121763 7130165 3859 2699 3408 2804 7 7 836 13466 46457260 40251928 6205332 3746 4304 3449 2989 1 7 0 14928 51997352 46457260 5540092 3789 4244 3483 3112 7 7 1009 16472 57839500 51997352 5842148 3782 3587 3511 3156 7 7 1332 17966 63550060 57839500 5710560 3823 3911 3537 3219 7 7 943 19486 69315399 63550060 5765339 3793 3757 3557 3261 7 7 898 21066 75057527 69315399 5742128 3633 3648 3562 3290 7 7 965 22585 80802664 75057527 5745137 3782 3780 3577 3323 7 7 1010 24102 86564450 80802664 5761786 3798 3787 3591 3352 7 7 477 25617 92307680 86564450 5743230 3789 3801 3603 3379 7 7 654 27125 98069607 92307680 5761927 3822 3809 3615 3402 7 7 858 28645 103809048 98069607 5739441 3774 3789 3623 3423 7 7 748 30152 109526655 103809048 5717607 3795 3809 3632 3442 7 7 732 31682 115298900 109526655 5772245 3773 3738 3639 3457 7 7 900 33191 121040296 115298900 5741396 3802 3823 3646 3473 7 7 586 34716 126793919 121040296 5753623 3774 3766 3652 3486 7 7 872 36233 132531801 126793919 5737882 3780 3791 3657 3499 7 7 486 37756 138287618 132531801 5755817 3780 3769 3662 3510 7 7 591 39313 144172147 138287618 5884529 3779 3696 3667 3517 7 7 619 40845 149999795 144172147 5827648 3802 3839 3672 3529 7 7 450 42424 156015069 149999795 6015274 3810 3691 3677 3535 7 7 685 44000 161958803 156015066 5943737 3769 3815 3680 3545 7 7 676 45663 168272607 161958803 6313804 3796 3574 3685 3546 7 7 446 47301 174432612 168272607 6160005 3761 3855 3687 3557 7 7 665 48991 180824319 174432612 6391707 3780 3643 3690 3560 7 7 884 50657 187112519 180824319 6288200 3775 3837 3693 3569 7 7 739 52310 193378444 187112518 6265926 3789 3803 3696 3576 7 7 450 53968 199666501 193378444 6288057 3792 3778 3699 3583 7 7 929 55629 205980380 199666501 6313879 3801 3786 3702 3589 7 7 706 Construction stopped 56630 205982427 205980380 2047 2 6309 3637 3637 1 7 0 57642 205982427 205982427 0 0 2 3573 3573 1 7 0 Construction threads: 4 ~5s interval cumulative active pool queued t[ms] constr finaliz in-flight con./ms fin./ms con./ms fin./ms thr. size tasks ------- ---------- ---------- ---------- ------- ------- ------- ------- ----- ----- ---------- 1834 4202393 489975 3712418 2290 267 2290 267 7 7 916 2982 8058211 4440811 3617400 3359 3442 2702 1489 7 7 1081 4477 14664660 11244912 3419748 4418 4550 3275 2511 7 7 865 5550 19504589 14664656 4839933 4510 3187 3514 2642 7 7 752 6551 24047522 20288779 3758743 4538 5618 3670 3096 7 7 14113 8003 30448556 23704201 6744355 4407 2351 3804 2961 7 7 627 9306 36133139 30448552 5684587 4361 5175 3882 3271 7 7 575 10944 43549751 36133134 7416617 4529 3471 3979 3301 7 7 638 12481 50252827 43549747 6703080 4358 4822 4026 3489 7 7 640 13688 55564728 50252821 5311907 4403 5556 4059 3671 7 7 635 14985 61475818 55564722 5911096 4557 4095 4102 3707 7 7 866 16232 67140606 61475810 5664796 4541 4739 4136 3787 7 7 468 17483 72884297 67140595 5743702 4590 4527 4168 3840 7 7 726 18738 78598905 72884264 5714641 4554 4577 4194 3889 7 7 602 20011 84353466 78598889 5754577 4520 4489 4215 3927 7 7 600 21261 90061124 84353450 5707674 4565 4603 4235 3967 7 7 485 22488 95784720 90061105 5723615 4665 4652 4259 4004 7 7 649 23717 101487418 95784671 5702747 4638 4655 4278 4038 7 7 346 25031 107228773 101487393 5741380 4370 4340 4283 4054 7 7 611 26263 112943768 107228747 5715021 4641 4662 4300 4082 7 7 419 27516 118662084 112943739 5718345 4561 4558 4312 4104 7 7 329 28825 124398704 118662054 5736650 4383 4369 4315 4116 7 7 589 30113 130154385 124398673 5755712 4470 4455 4322 4131 7 7 697 31362 135867229 130154342 5712887 4572 4606 4332 4149 7 7 423 32616 141569164 135867194 5701970 4546 4555 4340 4165 7 7 430 33965 147559412 141569109 5990303 4440 4226 4344 4168 7 7 461 35283 153417394 147559371 5858023 4443 4544 4348 4182 7 7 634 36752 159816413 153417351 6399062 4356 3987 4348 4174 7 7 424 38172 165950408 159816368 6134040 4322 4509 4347 4186 7 7 389 39568 172256536 165950360 6306176 4514 4391 4353 4193 7 7 388 40946 178464484 172256488 6207996 4507 4578 4358 4206 7 7 383 42341 184727903 178464434 6263469 4488 4448 4362 4214 7 7 456 43755 190966063 184727850 6238213 4412 4429 4364 4221 7 7 356 45131 197271695 190966009 6305686 4582 4533 4371 4231 7 7 337 46512 203560951 197271637 6289314 4555 4567 4376 4241 7 7 397 Construction stopped 47512 203563982 203560885 3097 3 6285 4284 4284 1 7 0 48523 203563982 203563982 0 0 3 4195 4195 1 7 0 Construction threads: 8 ~5s interval cumulative active pool queued t[ms] constr finaliz in-flight con./ms fin./ms con./ms fin./ms thr. size tasks ------- ---------- ---------- ---------- ------- ------- ------- ------- ----- ----- ---------- 1375 4125340 429092 3696248 2999 311 2999 311 7 7 823 3203 10376899 6011840 4365059 3419 3053 3238 1876 7 7 544 4344 15879249 10376878 5502371 4823 3826 3655 2388 7 7 1141 5437 20909477 15879241 5030236 4603 5036 3845 2920 7 7 500 7028 28557383 20909462 7647921 4805 3160 4063 2974 7 7 982 8442 35114174 28557376 6556798 4638 5410 4159 3382 7 7 661 9573 40455922 35114092 5341830 4720 5794 4225 3667 7 7 591 10802 46285922 40455905 5830017 4742 4345 4284 3744 7 7 494 12003 52031488 46131972 5899516 4786 4728 4334 3843 7 7 1908 13188 57674553 52031478 5643075 4760 4977 4373 3945 7 7 580 14390 63419863 57674538 5745325 4782 4696 4407 4007 7 7 726 15581 69111414 63419841 5691573 4777 4822 4435 4070 7 7 529 16784 74843134 69111392 5731742 4763 4730 4459 4117 1 7 0 17987 80547299 74843106 5704193 4743 4766 4478 4160 1 7 0 19203 86280774 80547265 5733509 4713 4689 4492 4194 1 7 0 20417 91992701 86280735 5711966 4706 4724 4505 4225 7 7 508 21653 97714160 91992657 5721503 4628 4620 4512 4248 1 7 0 22856 103444033 97659646 5784387 4761 4708 4525 4272 7 7 912 24068 109146063 103443981 5702082 4705 4773 4534 4297 3 7 264 25282 114882790 109146003 5736787 4725 4696 4543 4317 7 7 489 26477 120568234 114882723 5685511 4759 4802 4553 4338 7 7 464 27682 126302339 120568163 5734176 4757 4717 4562 4355 7 7 334 28926 132211197 126302262 5908935 4749 4609 4570 4366 7 7 800 30156 138044100 132211085 5833015 4741 4802 4577 4384 1 7 0 31441 144207697 138044016 6163681 4795 4538 4586 4390 7 7 335 32719 150276272 144207608 6068664 4750 4824 4592 4407 3 7 174 34089 156821733 149910774 6910959 4775 4161 4600 4397 7 7 2319 35368 162827442 156821634 6005808 4697 5405 4603 4433 1 7 0 36696 169148508 162827338 6321170 4760 4522 4609 4437 1 7 0 38013 175334032 169148399 6185633 4697 4800 4612 4449 7 7 331 39336 181627394 175333919 6293475 4755 4673 4617 4457 7 7 749 40649 187878002 181627276 6250726 4759 4792 4621 4468 7 7 401 42012 194311011 187877872 6433139 4721 4588 4625 4471 3 7 219 43344 200662735 194310881 6351854 4766 4827 4629 4482 7 7 376 Construction stopped 44345 200666585 200662596 3989 3 6348 4525 4524 1 7 0 45358 200666585 200666585 0 0 3 4424 4424 1 7 0 */ public class FinalizerThroughput { static final LongAdder constructions = new LongAdder(); static final LongAdder finalizations = new LongAdder(); static final Object poolInfo; static { Object pInfo; try { Class rhpc = Class.forName("java.lang.ref.ReferenceHandlerPool"); Field f = rhpc.getDeclaredField("pool"); f.setAccessible(true); ForkJoinPool p = (ForkJoinPool) f.get(null); pInfo = new Object() { @Override public String toString() { return String.format("%5d %5d %10d", p.getActiveThreadCount(), p.getPoolSize(), p.getQueuedTaskCount()); } }; } catch (Exception e) { pInfo = ""; } poolInfo = pInfo; } static final class Finalizable { public Finalizable() { constructions.increment(); } @Override protected void finalize() throws Throwable { finalizations.increment(); } } static class ConstructionThread extends Thread { static volatile boolean stop; @Override public void run() { while (!stop) { new Finalizable(); } } } public static void main(String[] args) throws Exception { int constructionThreads = Integer.parseInt(args[0]); for (int i = 0; i < constructionThreads; i++) { new ConstructionThread().start(); } System.out.printf("Construction threads: %3d\n", constructionThreads); System.out.printf(" ~5s interval cumulative active pool queued\n"); System.out.printf(" t[ms] constr finaliz in-flight con./ms fin./ms con./ms fin./ms thr. size tasks\n"); System.out.printf(" ------- ---------- ---------- ---------- ------- ------- ------- ------- ----- ----- ----------\n"); long c0 = 0L; long f0 = 0L; long t00 = System.nanoTime(); long t0 = t00; while (!ConstructionThread.stop || c0 != f0) { Thread.sleep(1000L); long t = System.nanoTime(); long c = constructions.longValue(); long f = finalizations.longValue(); long dc = c - c0; long df = f - f0; long dt = t - t0; long tc = t - t00; System.out.printf("%8d %10d %10d %10d %7d %7d %7d %7d %s\n", (t - t00) / 1000_000L, c, f, c - f, dc * 1000_000L / dt, df * 1000_000L / dt, c * 1000_000L / tc, f * 1000_000L / tc, poolInfo); c0 = c; f0 = f; t0 = t; if (c > 200_000_000L) { if (!ConstructionThread.stop) { ConstructionThread.stop = true; System.out.println("Construction stopped"); } else { System.gc(); } } } } }