我是剧作家的新人。我开发了一个简单的代码,它有 2 个线程,每个线程都有自己的 Playwright 对象以及 Browser 对象 + Page 对象。我正在使用 Java 的 1.18 Playwright 版本。
当调用关闭挂钩时,我将尝试以下列方式关闭在每个线程上创建的资源。
- 关闭页面对象
- 关闭浏览器对象
- 关闭剧作家对象
当我只用一个 Playwright 对象运行代码时,它就像一种魅力。
但是当创建两个或多个 Playwrights 对象时,就会发生异常。
如您所见,当两个 playwright 对象为 close() 时会发生异常。
如上所述,我为每个线程创建一个 Playwright 对象。
我分析了 hprof 文件,我看到了这样的东西。
使用 Java 8。
当使用 20G 的 ram 内存运行进程时,2 页对象会挂起进程,正如您在此处看到的那样,挂起需要 10 多分钟,我尝试甚至更长时间甚至 2 或 3 个小时并继续挂起。
使用标志-XX:+PrintGCDetails运行时,我可以看到
GC(分配失败)实际上是什么意思?GC 分配失败意味着垃圾收集器无法足够快地将对象从年轻一代移动到老一代,因为它在老一代中没有足够的内存。这可能会导致应用程序运行缓慢。
但是 20 GB 内存还不够吗?
Java HotSpot(TM) 64-Bit Server VM (25.181-b13) for windows-amd64 JRE (1.8.0_181-b13), built on Jul 7 2018 04:01:33 by "java_re" with MS VC++ 10.0 (VS2010)
Memory: 4k page, physical 66829096k(43322092k free), swap 87632988k(48322016k free)
CommandLine flags: -XX:+HeapDumpOnOutOfMemoryError -XX:InitialHeapSize=21474836480 -XX:MaxHeapSize=21474836480 -XX:+PrintGC -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -XX:+UnlockCommercialFeatures -XX:+UseCompressedClassPointers -XX:+UseCompressedOops -XX:-UseLargePagesIndividualAllocation -XX:+UseParallelGC
4.555: [GC (Metadata GC Threshold) [PSYoungGen: 3460646K->54963K(6116864K)] 3460646K->55051K(20098048K), 0.0298597 secs] [Times: user=0.14 sys=0.00, real=0.03 secs]
4.585: [Full GC (Metadata GC Threshold) [PSYoungGen: 54963K->0K(6116864K)] [ParOldGen: 88K->51554K(13981184K)] 55051K->51554K(20098048K), [Metaspace: 21074K->21074K(1067008K)], 0.0419001 secs] [Times: user=0.31 sys=0.06, real=0.04 secs]
5.665: [GC (Metadata GC Threshold) [PSYoungGen: 562473K->15549K(6116864K)] 614028K->67112K(20098048K), 0.0115794 secs] [Times: user=0.05 sys=0.00, real=0.01 secs]
5.677: [Full GC (Metadata GC Threshold) [PSYoungGen: 15549K->0K(6116864K)] [ParOldGen: 51562K->24900K(13981184K)] 67112K->24900K(20098048K), [Metaspace: 34762K->34762K(1079296K)], 0.0278415 secs] [Times: user=0.09 sys=0.00, real=0.03 secs]
9.262: [GC (Metadata GC Threshold) [PSYoungGen: 592661K->37509K(6116864K)] 617561K->62456K(20098048K), 0.0122229 secs] [Times: user=0.00 sys=0.00, real=0.01 secs]
9.275: [Full GC (Metadata GC Threshold) [PSYoungGen: 37509K->0K(6116864K)] [ParOldGen: 24947K->49005K(13981184K)] 62456K->49005K(20098048K), [Metaspace: 58039K->58039K(1099776K)], 0.1155353 secs] [Times: user=0.47 sys=0.01, real=0.12 secs]
225.878: [GC (Allocation Failure) [PSYoungGen: 5076063K->67377K(6116864K)] 5125069K->1909648K(20098048K), 0.6868276 secs] [Times: user=1.45 sys=0.36, real=0.68 secs]
使用 35 GB 堆,它会挂起超过 130 分钟,并依靠 Browser->shutdown 方法。 您可以看到日志时间与系统时间
gc_memory_logs
Java HotSpot(TM) 64-Bit Server VM (25.181-b13) for windows-amd64 JRE (1.8.0_181-b13), built on Jul 7 2018 04:01:33 by "java_re" with MS VC++ 10.0 (VS2010)
Memory: 4k page, physical 66829096k(47886792k free), swap 87632784k(62025452k free)
CommandLine flags: -XX:+HeapDumpOnOutOfMemoryError -XX:InitialHeapSize=37580963840 -XX:MaxHeapSize=37580963840 -XX:+PrintGC -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -XX:+UnlockCommercialFeatures -XX:-UseLargePagesIndividualAllocation -XX:+UseParallelGC
5.257: [GC (Metadata GC Threshold) [PSYoungGen: 4037245K->61178K(10704384K)] 4037245K->61226K(35171328K), 0.0350905 secs] [Times: user=0.08 sys=0.03, real=0.04 secs]
5.292: [Full GC (Metadata GC Threshold) [PSYoungGen: 61178K->0K(10704384K)] [ParOldGen: 48K->57708K(24466944K)] 61226K->57708K(35171328K), [Metaspace: 21090K->21090K(22528K)], 0.0355541 secs] [Times: user=0.11 sys=0.03, real=0.04 secs]
6.686: [GC (Metadata GC Threshold) [PSYoungGen: 642288K->18308K(10704384K)] 699997K->76025K(35171328K), 0.0102924 secs] [Times: user=0.00 sys=0.00, real=0.01 secs]
6.696: [Full GC (Metadata GC Threshold) [PSYoungGen: 18308K->0K(10704384K)] [ParOldGen: 57716K->32289K(24466944K)] 76025K->32289K(35171328K), [Metaspace: 34833K->34833K(36864K)], 0.0287175 secs] [Times: user=0.14 sys=0.00, real=0.03 secs]
10.652: [GC (Metadata GC Threshold) [PSYoungGen: 1192822K->46665K(10704384K)] 1225111K->78994K(35171328K), 0.0132830 secs] [Times: user=0.00 sys=0.00, real=0.01 secs]
10.666: [Full GC (Metadata GC Threshold) [PSYoungGen: 46665K->0K(10704384K)] [ParOldGen: 32329K->63570K(24466944K)] 78994K->63570K(35171328K), [Metaspace: 57979K->57979K(59392K)], 0.0875398 secs] [Times: user=0.48 sys=0.00, real=0.09 secs]
74.839: [GC (Allocation Failure) [PSYoungGen: 9175552K->112225K(10704384K)] 9239122K->175875K(35171328K), 0.0579027 secs] [Times: user=0.11 sys=0.02, real=0.06 secs]
当我使用System.exit(0) 5 playwrights 关闭 intellijIdea 时,我不知道为什么使用 CMD 会遇到很多麻烦-Xmx5g。