近期关于生产上JVM内存溢出问题分析和寻找问题解决问题(原创BelenChan)

近期,公司的项目在生产上跑炸了。整个服务都瘫痪了,就因为加了一个功能实现。具体如下

客户需要批量上传word,然后我们后台是多线程去处理这些word,让word转pdf。我们word转pdf,docx版本的用jacob去处理word转pdf。昨天生产客户批量上传了50篇word。随后服务就瘫痪了。然后在测试环境测了下,没有瘫痪,然后同事看了下是生产是双核单线程。确实是比较垃圾。要求升级服务器。我就想看下具体的原因到底有几方面。我看了下错误日志,服务的报错日志如下

VM Mutex/Monitor currently owned by a thread: None

heap address: 0x00000005c0000000, size: 8192 MB, Compressed Oops mode: Zero based, Oop shift amount: 3
Narrow klass base: 0x0000000000000000, Narrow klass shift: 3
Compressed class space size: 1073741824 Address: 0x00000007c0000000

Heap:
 PSYoungGen      total 2665472K, used 80501K [0x0000000715580000, 0x00000007c0000000, 0x00000007c0000000)
  eden space 2547200K, 2% used [0x0000000715580000,0x0000000718c6ab68,0x00000007b0d00000)
  from space 118272K, 20% used [0x00000007b0d00000,0x00000007b24b2908,0x00000007b8080000)
  to   space 116224K, 0% used [0x00000007b8e80000,0x00000007b8e80000,0x00000007c0000000)
 ParOldGen       total 645120K, used 166590K [0x00000005c0000000, 0x00000005e7600000, 0x0000000715580000)
  object space 645120K, 25% used [0x00000005c0000000,0x00000005ca2afb28,0x00000005e7600000)
 Metaspace       used 118240K, capacity 124698K, committed 124948K, reserved 1159168K
  class space    used 13575K, capacity 14564K, committed 14652K, reserved 1048576K

Card table byte_map: [0x0000000011ae0000,0x0000000012af0000] byte_map_base: 0x000000000ece0000

Marking Bits: (ParMarkBitMap*) 0x000000006bef5ce0
 Begin Bits: [0x0000000013900000, 0x000000001b900000)
 End Bits:   [0x000000001b900000, 0x0000000023900000)

Polling page: 0x0000000000350000

CodeCache: size=245760Kb used=61263Kb max_used=61342Kb free=184496Kb
 bounds [0x0000000002720000, 0x0000000006370000, 0x0000000011720000]
 total_blobs=18477 nmethods=17598 adapters=789
 compilation: enabled

Compilation events (10 events):
Event: 1515.420 Thread 0x00000000252c7800 23510       3       org.springframework.web.util.HierarchicalUriComponents::<init> (81 bytes)
Event: 1515.421 Thread 0x00000000252c7800 nmethod 23510 0x0000000002c0fad0 code [0x0000000002c0fcc0, 0x0000000002c103e8]
Event: 1515.422 Thread 0x00000000252ba000 nmethod 23507 0x0000000006361850 code [0x0000000006361aa0, 0x00000000063625e0]
Event: 1515.428 Thread 0x00000000252ba000 23511       4       java.util.stream.ReferencePipeline$3$1::accept (23 bytes)
Event: 1515.429 Thread 0x00000000252ba000 nmethod 23511 0x000000000332fbd0 code [0x000000000332fd20, 0x000000000332fdd8]
Event: 1515.474 Thread 0x00000000252ba000 23512       4       org.apache.tomcat.util.http.ValuesEnumerator::nextElement (5 bytes)
Event: 1515.479 Thread 0x00000000252ba000 nmethod 23512 0x0000000006359550 code [0x00000000063596e0, 0x0000000006359ac8]
Event: 1515.481 Thread 0x00000000252ba000 23513       4       org.apache.ibatis.type.IntegerTypeHandler::getNullableResult (7 bytes)
Event: 1515.485 Thread 0x00000000252ba000 nmethod 23513 0x0000000003020ad0 code [0x0000000003020c60, 0x0000000003020f88]
Event: 1515.496 Thread 0x00000000252ba000 23514   !   4       sun.reflect.GeneratedMethodAccessor316::invoke (74 bytes)

GC Heap History (10 events):
Event: 145.021 GC heap before
{Heap before GC invocations=21 (full 3):
 PSYoungGen      total 2680832K, used 2680455K [0x0000000715580000, 0x00000007c0000000, 0x00000007c0000000)
  eden space 2590208K, 100% used [0x0000000715580000,0x00000007b3700000,0x00000007b3700000)
  from space 90624K, 99% used [0x00000007b3700000,0x00000007b8f21e98,0x00000007b8f80000)
  to   space 113664K, 0% used [0x00000007b9100000,0x00000007b9100000,0x00000007c0000000)
 ParOldGen       total 494080K, used 182860K [0x00000005c0000000, 0x00000005de280000, 0x0000000715580000)
  object space 494080K, 37% used [0x00000005c0000000,0x00000005cb2932b8,0x00000005de280000)
 Metaspace       used 88901K, capacity 92864K, committed 93076K, reserved 1130496K
  class space    used 10476K, capacity 11140K, committed 11196K, reserved 1048576K
Event: 145.103 GC heap after
Heap after GC invocations=21 (full 3):
 PSYoungGen      total 2655744K, used 102918K [0x0000000715580000, 0x00000007c0000000, 0x00000007c0000000)
  eden space 2542080K, 0% used [0x0000000715580000,0x0000000715580000,0x00000007b0800000)
  from space 113664K, 90% used [0x00000007b9100000,0x00000007bf581ae8,0x00000007c0000000)
  to   space 126976K, 0% used [0x00000007b0800000,0x00000007b0800000,0x00000007b8400000)
 ParOldGen       total 494080K, used 182868K [0x00000005c0000000, 0x00000005de280000, 0x0000000715580000)
  object space 494080K, 37% used [0x00000005c0000000,0x00000005cb2952b8,0x00000005de280000)
 Metaspace       used 88901K, capacity 92864K, committed 93076K, reserved 1130496K
  class space    used 10476K, capacity 11140K, committed 11196K, reserved 1048576K
}
Event: 166.176 GC heap before
{Heap before GC invocations=22 (full 3):
 PSYoungGen      total 2655744K, used 467587K [0x0000000715580000, 0x00000007c0000000, 0x00000007c0000000)
  eden space 2542080K, 14% used [0x0000000715580000,0x000000072b99f2a0,0x00000007b0800000)
  from space 113664K, 90% used [0x00000007b9100000,0x00000007bf581ae8,0x00000007c0000000)
  to   space 126976K, 0% used [0x00000007b0800000,0x00000007b0800000,0x00000007b8400000)
 ParOldGen       total 494080K, used 182868K [0x00000005c0000000, 0x00000005de280000, 0x0000000715580000)
  object space 494080K, 37% used [0x00000005c0000000,0x00000005cb2952b8,0x00000005de280000)
 Metaspace       used 94301K, capacity 98242K, committed 98380K, reserved 1136640K
  class space    used 11013K, capacity 11691K, committed 11708K, reserved 1048576K
Event: 166.224 GC heap after
Heap after GC invocations=22 (full 3):
 PSYoungGen      total 2669056K, used 43382K [0x0000000715580000, 0x00000007c0000000, 0x00000007c0000000)
  eden space 2542080K, 0% used [0x0000000715580000,0x0000000715580000,0x00000007b0800000)
  from space 126976K, 34% used [0x00000007b0800000,0x00000007b325da08,0x00000007b8400000)
  to   space 121856K, 0% used [0x00000007b8900000,0x00000007b8900000,0x00000007c0000000)
 ParOldGen       total 494080K, used 182868K [0x00000005c0000000, 0x00000005de280000, 0x0000000715580000)
  object space 494080K, 37% used [0x00000005c0000000,0x00000005cb2952b8,0x00000005de280000)
 Metaspace       used 94301K, capacity 98242K, committed 98380K, reserved 1136640K
  class space    used 11013K, capacity 11691K, committed 11708K, reserved 1048576K
}
Event: 166.224 GC heap before
{Heap before GC invocations=23 (full 4):
 PSYoungGen      total 2669056K, used 43382K [0x0000000715580000, 0x00000007c0000000, 0x00000007c0000000)
  eden space 2542080K, 0% used [0x0000000715580000,0x0000000715580000,0x00000007b0800000)
  from space 126976K, 34% used [0x00000007b0800000,0x00000007b325da08,0x00000007b8400000)
  to   space 121856K, 0% used [0x00000007b8900000,0x00000007b8900000,0x00000007c0000000)
 ParOldGen       total 494080K, used 182868K [0x00000005c0000000, 0x00000005de280000, 0x0000000715580000)
  object space 494080K, 37% used [0x00000005c0000000,0x00000005cb2952b8,0x00000005de280000)
 Metaspace       used 94301K, capacity 98242K, committed 98380K, reserved 1136640K
  class space    used 11013K, capacity 11691K, committed 11708K, reserved 1048576K
Event: 166.797 GC heap after
Heap after GC invocations=23 (full 4):
 PSYoungGen      total 2669056K, used 0K [0x0000000715580000, 0x00000007c0000000, 0x00000007c0000000)
  eden space 2542080K, 0% used [0x0000000715580000,0x0000000715580000,0x00000007b0800000)
  from space 126976K, 0% used [0x00000007b0800000,0x00000007b0800000,0x00000007b8400000)
  to   space 121856K, 0% used [0x00000007b8900000,0x00000007b8900000,0x00000007c0000000)
 ParOldGen       total 645120K, used 166574K [0x00000005c0000000, 0x00000005e7600000, 0x0000000715580000)
  object space 645120K, 25% used [0x00000005c0000000,0x00000005ca2abb28,0x00000005e7600000)
 Metaspace       used 94301K, capacity 98242K, committed 98380K, reserved 1136640K
  class space    used 11013K, capacity 11691K, committed 11708K, reserved 1048576K
}
Event: 512.511 GC heap before
{Heap before GC invocations=24 (full 4):
 PSYoungGen      total 2669056K, used 2542080K [0x0000000715580000, 0x00000007c0000000, 0x00000007c0000000)
  eden space 2542080K, 100% used [0x0000000715580000,0x00000007b0800000,0x00000007b0800000)
  from space 126976K, 0% used [0x00000007b0800000,0x00000007b0800000,0x00000007b8400000)
  to   space 121856K, 0% used [0x00000007b8900000,0x00000007b8900000,0x00000007c0000000)
 ParOldGen       total 645120K, used 166574K [0x00000005c0000000, 0x00000005e7600000, 0x0000000715580000)
  object space 645120K, 25% used [0x00000005c0000000,0x00000005ca2abb28,0x00000005e7600000)
 Metaspace       used 112743K, capacity 118076K, committed 118292K, reserved 1153024K
  class space    used 12959K, capacity 13789K, committed 13884K, reserved 1048576K
Event: 512.546 GC heap after
Heap after GC invocations=24 (full 4):
 PSYoungGen      total 2656768K, used 36338K [0x0000000715580000, 0x00000007bf400000, 0x00000007c0000000)
  eden space 2547200K, 0% used [0x0000000715580000,0x0000000715580000,0x00000007b0d00000)
  from space 109568K, 33% used [0x00000007b8900000,0x00000007bac7c920,0x00000007bf400000)
  to   space 118272K, 0% used [0x00000007b0d00000,0x00000007b0d00000,0x00000007b8080000)
 ParOldGen       total 645120K, used 166582K [0x00000005c0000000, 0x00000005e7600000, 0x0000000715580000)
  object space 645120K, 25% used [0x00000005c0000000,0x00000005ca2adb28,0x00000005e7600000)
 Metaspace       used 112743K, capacity 118076K, committed 118292K, reserved 1153024K
  class space    used 12959K, capacity 13789K, committed 13884K, reserved 1048576K
}
Event: 1515.359 GC heap before
{Heap before GC invocations=25 (full 4):
 PSYoungGen      total 2656768K, used 2583538K [0x0000000715580000, 0x00000007bf400000, 0x00000007c0000000)
  eden space 2547200K, 100% used [0x0000000715580000,0x00000007b0d00000,0x00000007b0d00000)
  from space 109568K, 33% used [0x00000007b8900000,0x00000007bac7c920,0x00000007bf400000)
  to   space 118272K, 0% used [0x00000007b0d00000,0x00000007b0d00000,0x00000007b8080000)
 ParOldGen       total 645120K, used 166582K [0x00000005c0000000, 0x00000005e7600000, 0x0000000715580000)
  object space 645120K, 25% used [0x00000005c0000000,0x00000005ca2adb28,0x00000005e7600000)
 Metaspace       used 118238K, capacity 124698K, committed 124948K, reserved 1159168K
  class space    used 13575K, capacity 14564K, committed 14652K, reserved 1048576K
Event: 1515.394 GC heap after
Heap after GC invocations=25 (full 4):
 PSYoungGen      total 2665472K, used 24266K [0x0000000715580000, 0x00000007c0000000, 0x00000007c0000000)
  eden space 2547200K, 0% used [0x0000000715580000,0x0000000715580000,0x00000007b0d00000)
  from space 118272K, 20% used [0x00000007b0d00000,0x00000007b24b2908,0x00000007b8080000)
  to   space 116224K, 0% used [0x00000007b8e80000,0x00000007b8e80000,0x00000007c0000000)
 ParOldGen       total 645120K, used 166590K [0x00000005c0000000, 0x00000005e7600000, 0x0000000715580000)
  object space 645120K, 25% used [0x00000005c0000000,0x00000005ca2afb28,0x00000005e7600000)
 Metaspace       used 118238K, capacity 124698K, committed 124948K, reserved 1159168K
  class space    used 13575K, capacity 14564K, committed 14652K, reserved 1048576K
}

Deoptimization events (10 events):
Event: 1196.060 Thread 0x0000000048a66800 Uncommon trap: reason=class_check action=maybe_recompile pc=0x000000000510b2e8 method=java.util.HashMap.removeNode(ILjava/lang/Object;Ljava/lang/Object;ZZ)Ljava/util/HashMap$Node; @ 143
Event: 1331.064 Thread 0x000000002e933000 Uncommon trap: reason=unstable_if action=reinterpret pc=0x0000000005d7f9bc method=org.springframework.web.cors.CorsUtils.isPreFlightRequest(Ljavax/servlet/http/HttpServletRequest;)Z @ 19
Event: 1331.064 Thread 0x000000002e933000 Uncommon trap: reason=unstable_if action=reinterpret pc=0x00000000046ae670 method=org.springframework.web.servlet.mvc.condition.RequestMethodsRequestCondition.getMatchingCondition(Ljavax/servlet/http/HttpServletRequest;)Lorg/springframework/web/servlet/mv
Event: 1331.447 Thread 0x000000002e931000 Uncommon trap: reason=unstable_if action=reinterpret pc=0x0000000005f4ac80 method=org.apache.coyote.AbstractProcessor.parseHost(Lorg/apache/tomcat/util/buf/MessageBytes;)V @ 45
Event: 1331.448 Thread 0x000000002e931000 Uncommon trap: reason=null_check action=make_not_entrant pc=0x0000000004902f1c method=org.apache.catalina.core.ApplicationFilterFactory.createFilterChain(Ljavax/servlet/ServletRequest;Lorg/apache/catalina/Wrapper;Ljavax/servlet/Servlet;)Lorg/apache/catali
Event: 1331.448 Thread 0x000000002e931000 Uncommon trap: reason=unstable_if action=reinterpret pc=0x0000000005ac2ae4 method=org.apache.catalina.core.ApplicationFilterChain.addFilter(Lorg/apache/catalina/core/ApplicationFilterConfig;)V @ 45
Event: 1466.097 Thread 0x0000000048a62800 Uncommon trap: reason=unstable_if action=reinterpret pc=0x0000000004604ec4 method=java.lang.String.toLowerCase(Ljava/util/Locale;)Ljava/lang/String; @ 37
Event: 1466.122 Thread 0x0000000048a62800 Uncommon trap: reason=bimorphic action=maybe_recompile pc=0x00000000046584e8 method=java.util.Collections$UnmodifiableMap.size()I @ 4
Event: 1470.226 Thread 0x0000000030b72000 Uncommon trap: reason=class_check action=maybe_recompile pc=0x0000000005323c08 method=java.util.concurrent.ThreadPoolExecutor.getTask()Ljava/lang/Runnable; @ 29
Event: 1482.222 Thread 0x0000000030b72000 Uncommon trap: reason=class_check action=maybe_recompile pc=0x0000000005323c08 method=java.util.concurrent.ThreadPoolExecutor.getTask()Ljava/lang/Runnable; @ 29

Classes redefined (0 events):
No events

Internal exceptions (10 events):
Event: 1468.190 Thread 0x0000000031d92000 Exception <a 'java/net/SocketTimeoutException': Read timed out> (0x000000079eece278) thrown at [C:\re\workspace\8-2-build-windows-amd64-cygwin\jdk8u221\13320\hotspot\src\share\vm\prims\jni.cpp, line 736]
Event: 1469.626 Thread 0x0000000030b72000 Exception <a 'com/jacob/com/ComFailException'> (0x00000007a218bcb8) thrown at [C:\re\workspace\8-2-build-windows-amd64-cygwin\jdk8u221\13320\hotspot\src\share\vm\prims\jni.cpp, line 710]
Event: 1474.450 Thread 0x0000000030b72000 Exception <a 'com/jacob/com/ComFailException'> (0x00000007a255cc18) thrown at [C:\re\workspace\8-2-build-windows-amd64-cygwin\jdk8u221\13320\hotspot\src\share\vm\prims\jni.cpp, line 710]
Event: 1477.522 Thread 0x0000000030b72000 Exception <a 'com/jacob/com/ComFailException'> (0x00000007a2560378) thrown at [C:\re\workspace\8-2-build-windows-amd64-cygwin\jdk8u221\13320\hotspot\src\share\vm\prims\jni.cpp, line 710]
Event: 1478.205 Thread 0x000000002cab3000 Exception <a 'java/net/SocketTimeoutException': Read timed out> (0x000000078f523250) thrown at [C:\re\workspace\8-2-build-windows-amd64-cygwin\jdk8u221\13320\hotspot\src\share\vm\prims\jni.cpp, line 736]
Event: 1481.712 Thread 0x0000000030b72000 Exception <a 'com/jacob/com/ComFailException'> (0x00000007a2970248) thrown at [C:\re\workspace\8-2-build-windows-amd64-cygwin\jdk8u221\13320\hotspot\src\share\vm\prims\jni.cpp, line 710]
Event: 1488.279 Thread 0x000000002cab3000 Exception <a 'java/net/SocketTimeoutException': Read timed out> (0x000000078f52e048) thrown at [C:\re\workspace\8-2-build-windows-amd64-cygwin\jdk8u221\13320\hotspot\src\share\vm\prims\jni.cpp, line 736]
Event: 1498.355 Thread 0x000000002cab3000 Exception <a 'java/net/SocketTimeoutException': Read timed out> (0x000000078f538e40) thrown at [C:\re\workspace\8-2-build-windows-amd64-cygwin\jdk8u221\13320\hotspot\src\share\vm\prims\jni.cpp, line 736]
Event: 1508.421 Thread 0x000000002cab3000 Exception <a 'java/net/SocketTimeoutException': Read timed out> (0x000000078f544328) thrown at [C:\re\workspace\8-2-build-windows-amd64-cygwin\jdk8u221\13320\hotspot\src\share\vm\prims\jni.cpp, line 736]
Event: 1514.084 Thread 0x0000000030b72000 Exception <a 'java/net/SocketTimeoutException': Read timed out> (0x00000007ae3c31b8) thrown at [C:\re\workspace\8-2-build-windows-amd64-cygwin\jdk8u221\13320\hotspot\src\share\vm\prims\jni.cpp, line 736]

Events (10 events):
Event: 1515.397 Executing VM operation: RevokeBias done
Event: 1515.397 Executing VM operation: RevokeBias
Event: 1515.398 Executing VM operation: RevokeBias done
Event: 1515.398 Executing VM operation: RevokeBias
Event: 1515.398 Executing VM operation: RevokeBias done
Event: 1515.420 Thread 0x00000000252c7800 flushing nmethod 0x0000000002c0f790
Event: 1515.421 Thread 0x00000000252c7800 flushing nmethod 0x0000000002c5b490
Event: 1515.430 Thread 0x00000000252ba000 flushing nmethod 0x0000000003020ad0
Event: 1515.485 Thread 0x00000000252ba000 flushing nmethod 0x000000000352a2d0
Event: 1515.485 Thread 0x00000000252ba000 flushing nmethod 0x0000000003564b90

如上图所示的日志。

接下来我花了一点时间去阅读了下日志。

给大家抓一下重点,第一:GC Heap History (10 events):

堆内存区10个事件,关于GC的。

第一个事件:

Heap before GC invocations=21 (full 3):
 PSYoungGen      total 2680832K, used 2680455K [0x0000000715580000, 0x00000007c0000000, 0x00000007c0000000)
  eden space 2590208K, 100% used [0x0000000715580000,0x00000007b3700000,0x00000007b3700000)
  from space 90624K, 99% used [0x00000007b3700000,0x00000007b8f21e98,0x00000007b8f80000)
  to   space 113664K, 0% used [0x00000007b9100000,0x00000007b9100000,0x00000007c0000000)
 ParOldGen       total 494080K, used 182860K [0x00000005c0000000, 0x00000005de280000, 0x0000000715580000)
  object space 494080K, 37% used [0x00000005c0000000,0x00000005cb2932b8,0x00000005de280000)
 Metaspace       used 88901K, capacity 92864K, committed 93076K, reserved 1130496K
  class space    used 10476K, capacity 11140K, committed 11196K, reserved 1048576K

堆内存区触发了21次GC,其中fullGc有3次。3次,啥概念。整个服务停了3次。  他说PSYoungGen      total 2680832K, used 2680455K   年轻代区总共2680832K  被用了2680455K  可以说100%了。eden区也是100% from区也是100%,但是ParOldGen  老年代区只用了37%。

接下来的第二段和第三段也是这样。但是老年代都是基本上保持在37%。   

注意:为啥老年代只用了37%呢?说明,在年轻代区里。每次触发了minorGC后,但是还是有超过百分之80%的对象不可回收。但是如果这个对象一直在被调用,就无法进入老年代。导致年轻代触发了多次GC,最后到年轻代爆满了,触发了FullGC,STOP THE WORLD。最后多次FullGC却发现还是解决不了问题,整个服务瘫痪。

现在我们把问题分析完了,所以接下来去寻找一下原因。日志里也提到了相关原因,往下找可以看到这段

Internal exceptions (10 events):
Event: 1468.190 Thread 0x0000000031d92000 Exception <a 'java/net/SocketTimeoutException': Read timed out> (0x000000079eece278) thrown at [C:\re\workspace\8-2-build-windows-amd64-cygwin\jdk8u221\13320\hotspot\src\share\vm\prims\jni.cpp, line 736]
Event: 1469.626 Thread 0x0000000030b72000 Exception <a 'com/jacob/com/ComFailException'> (0x00000007a218bcb8) thrown at [C:\re\workspace\8-2-build-windows-amd64-cygwin\jdk8u221\13320\hotspot\src\share\vm\prims\jni.cpp, line 710]
Event: 1474.450 Thread 0x0000000030b72000 Exception <a 'com/jacob/com/ComFailException'> (0x00000007a255cc18) thrown at [C:\re\workspace\8-2-build-windows-amd64-cygwin\jdk8u221\13320\hotspot\src\share\vm\prims\jni.cpp, line 710]
Event: 1477.522 Thread 0x0000000030b72000 Exception <a 'com/jacob/com/ComFailException'> (0x00000007a2560378) thrown at [C:\re\workspace\8-2-build-windows-amd64-cygwin\jdk8u221\13320\hotspot\src\share\vm\prims\jni.cpp, line 710]
Event: 1478.205 Thread 0x000000002cab3000 Exception <a 'java/net/SocketTimeoutException': Read timed out> (0x000000078f523250) thrown at [C:\re\workspace\8-2-build-windows-amd64-cygwin\jdk8u221\13320\hotspot\src\share\vm\prims\jni.cpp, line 736]
Event: 1481.712 Thread 0x0000000030b72000 Exception <a 'com/jacob/com/ComFailException'> (0x00000007a2970248) thrown at [C:\re\workspace\8-2-build-windows-amd64-cygwin\jdk8u221\13320\hotspot\src\share\vm\prims\jni.cpp, line 710]
Event: 1488.279 Thread 0x000000002cab3000 Exception <a 'java/net/SocketTimeoutException': Read timed out> (0x000000078f52e048) thrown at [C:\re\workspace\8-2-build-windows-amd64-cygwin\jdk8u221\13320\hotspot\src\share\vm\prims\jni.cpp, line 736]
Event: 1498.355 Thread 0x000000002cab3000 Exception <a 'java/net/SocketTimeoutException': Read timed out> (0x000000078f538e40) thrown at [C:\re\workspace\8-2-build-windows-amd64-cygwin\jdk8u221\13320\hotspot\src\share\vm\prims\jni.cpp, line 736]
Event: 1508.421 Thread 0x000000002cab3000 Exception <a 'java/net/SocketTimeoutException': Read timed out> (0x000000078f544328) thrown at [C:\re\workspace\8-2-build-windows-amd64-cygwin\jdk8u221\13320\hotspot\src\share\vm\prims\jni.cpp, line 736]
Event: 1514.084 Thread 0x0000000030b72000 Exception <a 'java/net/SocketTimeoutException': Read timed out> (0x00000007ae3c31b8) thrown at [C:\re\workspace\8-2-build-windows-amd64-cygwin\jdk8u221\13320\hotspot\src\share\vm\prims\jni.cpp, line 736]

Internal exceptions (10 events):内部异常,这里很关键,指向了关于jacob,核心问题所在。就是因为jacob处理的时候导致了SocketTimeOut。

然后我又迅速找到关于多线程的代码。如下

@Override
    public void run() {
        ActiveXComponent app = null;
        Dispatch document = null;
        Dispatch doc = null;
        try {
            ComThread.InitSTA();
            app = new ActiveXComponent("Word.Application");
            app.setProperty("Visible", new Variant(false));
            // 禁用宏
            app.setProperty("AutomationSecurity", new Variant(3));
            document = app.getProperty("Documents").toDispatch();
            doc = Dispatch.invoke(document, "Open", Dispatch.Method,
                    new Object[] { srcPath, new Variant(true), new Variant(true) }, new int[1]).toDispatch();
            Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[] { descPath, new Variant(fmt) }, new int[1]);
            File newFile = new File(descPath);
            JacobWordToPDF.wordToPDF(newFile.getAbsolutePath(), outStreamUrl);
            PdfToImgUtil.pdfToImgAndDelOld(attachments, imgName, uploadPath);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            Dispatch.call(doc, "Close", new Variant(true));
//            Dispatch.call(document, "Close", new Variant(true));
//            app.invoke("Quit", new Variant[] {});
            if (app != null) {
                app.invoke("Quit", new Variant[] {});
            }
            ComThread.Release();
        }
    }

不知道大家有没有看出什么问题出来。有一段代码写的确实是有问题。

File newFile = new File(descPath);

注意,今天同事和我说了一句,一个线程出栈后,对象就不会被引用  就会被销毁,我想说的是,java分几种引用

(1:强引用

   2:软引用

   3:弱引用

   4:虚引用

什么是强引用?  File newFile = new File()   这里的new就是强引用!   那肯定又会问,为什么叫强引用?我在《深入理解java虚拟机》里看到,强引用就是强在“JVM即使内存溢出,也不会让GC去随便的回收强引用对象所占用的内存空间”。其原因也容易理解,如果GC将上述的newFile回收了,那么new Object()这个对象将毫无意义,后续所有使用newFile的代码也将出错。

这段代码是在线程里面。这段代码是根本致命原因。这里50条线程进来去处理word转PDF。但是每次进来都是new一个File对象,但是使用完了并没有销毁。但是下次来了还是依然new一个对象,每次还能进来。所以结合之前jvm年轻代区内存溢出。就可以看出来问题就是出现在这。

最后我把代码改完,再在测试环境上测,打出GC日志,很明显每次heap区都不会有超过百分之50的内存被占用。

最后,服务器配置虽然能解决一大部分性能问题。但是如果代码优化到极致了。再加上一台好服务器,是否能大大提高客户的使用感呢?

感谢大家,如果觉得有帮助可以点赞,如果有什么需要补充的也可以提出来!

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值