Unsafe.defineClass hangs - slow reflection calls

。_饼干妹妹 提交于 2019-12-13 16:42:31

问题


I'm oberserving extreme long running requests (>30s) in a Java 8 / Spring 5 / Tomcat 8.5 web application. The majority of requests responds normally. It's remarkable that the frequency of long runners increases after some days of uptime.

Threaddumps show that Unsafe.defineClass is hanging:

at sun.misc.Unsafe.defineClass(Native Method) 
at sun.reflect.ClassDefiner.defineClass(ClassDefiner.java:63) 
at sun.reflect.MethodAccessorGenerator$1.run(MethodAccessorGenerator.java:399) 
at sun.reflect.MethodAccessorGenerator$1.run(MethodAccessorGenerator.java:394) 
at java.security.AccessController.doPrivileged(Native Method)  
at sun.reflect.MethodAccessorGenerator.generate(MethodAccessorGenerator.java:393) 
at sun.reflect.MethodAccessorGenerator.generateMethod(MethodAccessorGenerator.java:75) 
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:53) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
at java.lang.reflect.Method.invoke(Method.java:498) 
at org.springframework.core.annotation.AnnotationUtils.getValue(AnnotationUtils.java:1328)  
at org.springframework.core.annotation.AnnotatedElementUtils$MergedAnnotationAttributesProcessor.getAdaptedValue(AnnotatedElementUtils.java:1627) 
at org.springframework.core.annotation.AnnotatedElementUtils$MergedAnnotationAttributesProcessor.overrideAttributes(AnnotatedElementUtils.java:1610) 
at org.springframework.core.annotation.AnnotatedElementUtils$MergedAnnotationAttributesProcessor.postProcess(AnnotatedElementUtils.java:1598) 
at org.springframework.core.annotation.AnnotatedElementUtils$MergedAnnotationAttributesProcessor.postProcess(AnnotatedElementUtils.java:1515) 
at org.springframework.core.annotation.AnnotatedElementUtils.searchWithFindSemantics(AnnotatedElementUtils.java:1145) 
at org.springframework.core.annotation.AnnotatedElementUtils.searchWithFindSemantics(AnnotatedElementUtils.java:1066) 
at org.springframework.core.annotation.AnnotatedElementUtils.searchWithFindSemantics(AnnotatedElementUtils.java:1038) 
at org.springframework.core.annotation.AnnotatedElementUtils.findMergedAnnotationAttributes(AnnotatedElementUtils.java:650) 
at org.springframework.core.annotation.AnnotatedElementUtils.findMergedAnnotation(AnnotatedElementUtils.java:726)
....

Has anybody ever seen this problem?


回答1:


Could it be that you are running out of Metaspace either by using a conservative -XX:MaxMetaspaceSize or not having enough RAM on the server? Since GC runs on Metaspace your slowness might be the result of it struggling to clean it. By default the Metaspace memory is collected if it reaches the MaxMetaspaceSize so this 30 sec pause might be the GC.

There is however a bigger question. What in your code is allocating these classes and why does it happen often enough to fill Metaspace. Normally classes are created during startup and after that much less frequently. Perhaps you are doing hot deployments and the ClassLoader is leaking?

In pre Java 8 this could be caused by the GC and PermGen running out e.g. as asked in this question where it caused OutOfMemoryError.



来源:https://stackoverflow.com/questions/49507981/unsafe-defineclass-hangs-slow-reflection-calls

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!