【问题标题】:Unsafe.defineClass hangs - slow reflection callsUnsafe.defineClass 挂起 - 缓慢的反射调用
【发布时间】:2018-09-05 13:45:11
【问题描述】:

我在 Java 8 / Spring 5 / Tomcat 8.5 Web 应用程序中观察到超长时间运行的请求(>30 秒)。大多数请求都能正常响应。 值得注意的是,在正常运行几天后,长跑者的频率增加了。

线程转储显示 Unsafe.defineClass 挂起:

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)
....

有人见过这个问题吗?

【问题讨论】:

标签: java spring-mvc tomcat8


【解决方案1】:

可能是因为使用保守的-XX:MaxMetaspaceSize 或服务器上没有足够的 RAM,您的 Metaspace 已用完?由于 GC 在 Metaspace 上运行,您的缓慢可能是它努力清理它的结果。默认情况下,如果 Metaspace 内存到达MaxMetaspaceSize,则会收集它,因此这 30 秒的暂停可能是 GC。

然而,还有一个更大的问题。您的代码中分配这些类的内容以及为什么它经常发生足以填充元空间。通常,类是在启动期间创建的,之后创建的频率要低得多。也许您正在进行热部署并且ClassLoader 正在泄漏?

在 Java 8 之前,这可能是由 GC 和 PermGen 耗尽引起的,例如as asked in this question where it caused OutOfMemoryError.

【讨论】:

  • 是的,我正在限制 MaxMetaspaceSize。但只使用了 75%。超过 6GB 的 RAM 是免费的。
  • @chrempl 即使发生缓慢,75% 的使用率是否保持不变?也可能是 Metaspace 的扩展,在 Metaspace 上调试 GC 应该会给你答案。
  • 是的,元空间的使用是不变的。垃圾收集 (G1) 时间也很好。
  • 为了澄清,我必须指出这些类是由java反射内部生成的。这不是我的代码。我也怀疑类加载器锁 - 但线程转储中没有监视器。
猜你喜欢
  • 1970-01-01
  • 2013-06-02
  • 1970-01-01
  • 2010-10-20
  • 1970-01-01
  • 2018-05-19
  • 2010-09-27
  • 2019-04-12
  • 1970-01-01
相关资源
最近更新 更多