首页 » jvm性能调优

OOm错误

1.配置gradle.properties

org.gradle.jvmargs=-Xmx4096m -XX:MaxPermSize=4096m -XX:+HeapDumpOnOutOfMemoryError

2.完整报错:

ERROR: Exception while analyzing expression at (54,16) in /home/shenfeng/dev1/thor-android/ModuleUser/src/main/kotlin/com/rr/biz/user/ui/me/adapter/MineAdapter.kt
org.jetbrains.kotlin.utils.KotlinExceptionWithAttachments: Exception while analyzing expression at (54,16) in /home/shenfeng/dev1/thor-android/ModuleUser/src/main/kotlin/com/rr/biz/user/ui/me/adapter/MineAdapter.kt
    at org.jetbrains.kotlin.types.expressions.ExpressionTypingVisitorDispatcher.logOrThrowException(ExpressionTypingVisitorDispatcher.java:233)
    at org.jetbrains.kotlin.types.expressions.ExpressionTypingVisitorDispatcher.lambda$getTypeInfo$0(ExpressionTypingVisitorDispatcher.java:211)
    at org.jetbrains.kotlin.util.PerformanceCounter.time(PerformanceCounter.kt:90)
    at org.jetbrains.kotlin.types.expressions.ExpressionTypingVisitorDispatcher.getTypeInfo(ExpressionTypingVisitorDispatcher.java:161)
    at org.jetbrains.kotlin.types.expressions.ExpressionTypingVisitorDispatcher.getTypeInfo(ExpressionTypingVisitorDispatcher.java:132)
    at org.jetbrains.kotlin.types.expressions.ControlStructureTypingVisitor.visitReturnExpression(ControlStructureTypingVisitor.java:676)
    at org.jetbrains.kotlin.types.expressions.ExpressionTypingVisitorDispatcher.visitReturnExpression(ExpressionTypingVisitorDispatcher.java:266)
    at org.jetbrains.kotlin.types.expressions.ExpressionTypingVisitorDispatcher$ForBlock.visitReturnExpression(ExpressionTypingVisitorDispatcher.java:57)
    at org.jetbrains.kotlin.psi.KtReturnExpression.accept(KtReturnExpression.java:33)
    at org.jetbrains.kotlin.types.expressions.ExpressionTypingVisitorDispatcher.lambda$getTypeInfo$0(ExpressionTypingVisitorDispatcher.java:172)
    at org.jetbrains.kotlin.util.PerformanceCounter.time(PerformanceCounter.kt:90)
    at org.jetbrains.kotlin.types.expressions.ExpressionTypingVisitorDispatcher.getTypeInfo(ExpressionTypingVisitorDispatcher.java:161)
    at org.jetbrains.kotlin.types.expressions.ExpressionTypingVisitorDispatcher.getTypeInfo(ExpressionTypingVisitorDispatcher.java:132)
    at org.jetbrains.kotlin.types.expressions.ExpressionTypingVisitorForStatements.visitExpression(ExpressionTypingVisitorForStatements.java:373)
    at org.jetbrains.kotlin.types.expressions.ExpressionTypingVisitorForStatements.visitExpression(ExpressionTypingVisitorForStatements.java:62)
    at org.jetbrains.kotlin.psi.KtVisitor.visitExpressionWithLabel(KtVisitor.java:226)
    at org.jetbrains.kotlin.psi.KtVisitor.visitReturnExpression(KtVisitor.java:222)
    at org.jetbrains.kotlin.psi.KtReturnExpression.accept(KtReturnExpression.java:33)
    at org.jetbrains.kotlin.types.expressions.ExpressionTypingVisitorDispatcher.lambda$getTypeInfo$0(ExpressionTypingVisitorDispatcher.java:172)
    at org.jetbrains.kotlin.util.PerformanceCounter.time(PerformanceCounter.kt:90)
    at org.jetbrains.kotlin.types.expressions.ExpressionTypingVisitorDispatcher.getTypeInfo(ExpressionTypingVisitorDispatcher.java:161)
    at org.jetbrains.kotlin.types.expressions.ExpressionTypingVisitorDispatcher.getTypeInfo(ExpressionTypingVisitorDispatcher.java:145)
    at org.jetbrains.kotlin.types.expressions.ExpressionTypingServices.getTypeOfLastExpressionInBlock(ExpressionTypingServices.java:345)
    at org.jetbrains.kotlin.types.expressions.ExpressionTypingServices.getBlockReturnedTypeWithWritableScope(ExpressionTypingServices.java:284)
    at org.jetbrains.kotlin.types.expressions.ExpressionTypingServices.getBlockReturnedType(ExpressionTypingServices.java:202)
    at org.jetbrains.kotlin.types.expressions.ExpressionTypingServices.getBlockReturnedType(ExpressionTypingServices.java:179)
    at org.jetbrains.kotlin.types.expressions.ExpressionTypingVisitorForStatements.visitBlockExpression(ExpressionTypingVisitorForStatements.java:416)
    at org.jetbrains.kotlin.types.expressions.ExpressionTypingVisitorForStatements.visitBlockExpression(ExpressionTypingVisitorForStatements.java:62)
    at org.jetbrains.kotlin.psi.KtBlockExpression.accept(KtBlockExpression.java:44)
    at org.jetbrains.kotlin.types.expressions.ExpressionTypingVisitorDispatcher.lambda$getTypeInfo$0(ExpressionTypingVisitorDispatcher.java:172)
    at org.jetbrains.kotlin.util.PerformanceCounter.time(PerformanceCounter.kt:90)
    at org.jetbrains.kotlin.types.expressions.ExpressionTypingVisitorDispatcher.getTypeInfo(ExpressionTypingVisitorDispatcher.java:161)
    at org.jetbrains.kotlin.types.expressions.ExpressionTypingVisitorDispatcher.getTypeInfo(ExpressionTypingVisitorDispatcher.java:145)
    at org.jetbrains.kotlin.types.expressions.ExpressionTypingServices.checkFunctionReturnType(ExpressionTypingServices.java:174)
    at org.jetbrains.kotlin.types.expressions.ExpressionTypingServices.checkFunctionReturnType(ExpressionTypingServices.java:158)
    at org.jetbrains.kotlin.resolve.BodyResolver.resolveFunctionBody(BodyResolver.java:916)
    at org.jetbrains.kotlin.resolve.BodyResolver.resolveFunctionBody(BodyResolver.java:865)
    at org.jetbrains.kotlin.resolve.BodyResolver.resolveFunctionBodies(BodyResolver.java:851)
    at org.jetbrains.kotlin.resolve.BodyResolver.resolveBehaviorDeclarationBodies(BodyResolver.java:122)
    at org.jetbrains.kotlin.resolve.BodyResolver.resolveBodies(BodyResolver.java:241)
    at org.jetbrains.kotlin.resolve.LazyTopDownAnalyzer.analyzeDeclarations(LazyTopDownAnalyzer.kt:215)
    at org.jetbrains.kotlin.resolve.LazyTopDownAnalyzer.analyzeDeclarations$default(LazyTopDownAnalyzer.kt:57)
    at org.jetbrains.kotlin.cli.jvm.compiler.TopDownAnalyzerFacadeForJVM.analyzeFilesWithJavaIntegration(TopDownAnalyzerFacadeForJVM.kt:112)
    at org.jetbrains.kotlin.cli.jvm.compiler.TopDownAnalyzerFacadeForJVM.analyzeFilesWithJavaIntegration$default(TopDownAnalyzerFacadeForJVM.kt:84)
    at com.android.tools.lint.KotlinLintAnalyzerFacade.analyzePsi(KotlinLintAnalyzerFacade.kt:208)
    at com.android.tools.lint.KotlinLintAnalyzerFacade.analyze(KotlinLintAnalyzerFacade.kt:102)
    at com.android.tools.lint.LintCliClient$LintCliUastParser.prepare(LintCliClient.java:1341)
    at com.android.tools.lint.client.api.UElementVisitor.prepare(UElementVisitor.kt:294)
    at com.android.tools.lint.client.api.LintDriver.visitJavaFiles(LintDriver.kt:1471)
    at com.android.tools.lint.client.api.LintDriver.visitJavaFiles(LintDriver.kt:1445)
    at com.android.tools.lint.client.api.LintDriver.checkJava(LintDriver.kt:1423)
    at com.android.tools.lint.client.api.LintDriver.runFileDetectors(LintDriver.kt:962)
    at com.android.tools.lint.client.api.LintDriver.checkProject(LintDriver.kt:812)
    at com.android.tools.lint.client.api.LintDriver.analyze(LintDriver.kt:345)
    at com.android.tools.lint.LintCliClient.run(LintCliClient.java:179)
    at com.android.tools.lint.gradle.LintGradleClient.run(LintGradleClient.java:254)
    at com.android.tools.lint.gradle.LintGradleExecution.runLint(LintGradleExecution.java:257)
    at com.android.tools.lint.gradle.LintGradleExecution.lintAllVariants(LintGradleExecution.java:392)
    at com.android.tools.lint.gradle.LintGradleExecution.analyze(LintGradleExecution.java:98)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.android.tools.lint.gradle.api.ReflectiveLintRunner.runLint(ReflectiveLintRunner.kt:38)
    at com.android.build.gradle.tasks.LintBaseTask.runLint(LintBaseTask.java:87)
    at com.android.build.gradle.tasks.LintGlobalTask.lint(LintGlobalTask.java:47)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.gradle.internal.reflect.JavaMethod.invoke(JavaMethod.java:73)
    at org.gradle.api.internal.project.taskfactory.StandardTaskAction.doExecute(StandardTaskAction.java:46)
    at org.gradle.api.internal.project.taskfactory.StandardTaskAction.execute(StandardTaskAction.java:39)
    at org.gradle.api.internal.project.taskfactory.StandardTaskAction.execute(StandardTaskAction.java:26)
    at org.gradle.api.internal.AbstractTask$TaskActionWrapper.execute(AbstractTask.java:780)
    at org.gradle.api.internal.AbstractTask$TaskActionWrapper.execute(AbstractTask.java:747)
    at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter$1.run(ExecuteActionsTaskExecuter.java:121)
    at org.gradle.internal.progress.DefaultBuildOperationExecutor$RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:336)
    at org.gradle.internal.progress.DefaultBuildOperationExecutor$RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:328)
    at org.gradle.internal.progress.DefaultBuildOperationExecutor.execute(DefaultBuildOperationExecutor.java:199)
    at org.gradle.internal.progress.DefaultBuildOperationExecutor.run(DefaultBuildOperationExecutor.java:110)
    at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeAction(ExecuteActionsTaskExecuter.java:110)
    at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeActions(ExecuteActionsTaskExecuter.java:92)
    at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.execute(ExecuteActionsTaskExecuter.java:70)
    at org.gradle.api.internal.tasks.execution.OutputDirectoryCreatingTaskExecuter.execute(OutputDirectoryCreatingTaskExecuter.java:51)
    at org.gradle.api.internal.tasks.execution.SkipUpToDateTaskExecuter.execute(SkipUpToDateTaskExecuter.java:62)
    at org.gradle.api.internal.tasks.execution.ResolveTaskOutputCachingStateExecuter.execute(ResolveTaskOutputCachingStateExecuter.java:54)
    at org.gradle.api.internal.tasks.execution.ValidatingTaskExecuter.execute(ValidatingTaskExecuter.java:60)
    at org.gradle.api.internal.tasks.execution.SkipEmptySourceFilesTaskExecuter.execute(SkipEmptySourceFilesTaskExecuter.java:97)
    at org.gradle.api.internal.tasks.execution.CleanupStaleOutputsExecuter.execute(CleanupStaleOutputsExecuter.java:87)
    at org.gradle.api.internal.tasks.execution.ResolveTaskArtifactStateTaskExecuter.execute(ResolveTaskArtifactStateTaskExecuter.java:52)
    at org.gradle.api.internal.tasks.execution.SkipTaskWithNoActionsExecuter.execute(SkipTaskWithNoActionsExecuter.java:52)
    at org.gradle.api.internal.tasks.execution.SkipOnlyIfTaskExecuter.execute(SkipOnlyIfTaskExecuter.java:54)
    at org.gradle.api.internal.tasks.execution.ExecuteAtMostOnceTaskExecuter.execute(ExecuteAtMostOnceTaskExecuter.java:43)
    at org.gradle.api.internal.tasks.execution.CatchExceptionTaskExecuter.execute(CatchExceptionTaskExecuter.java:34)
    at org.gradle.execution.taskgraph.DefaultTaskGraphExecuter$EventFiringTaskWorker$1.run(DefaultTaskGraphExecuter.java:248)
    at org.gradle.internal.progress.DefaultBuildOperationExecutor$RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:336)
    at org.gradle.internal.progress.DefaultBuildOperationExecutor$RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:328)
    at org.gradle.internal.progress.DefaultBuildOperationExecutor.execute(DefaultBuildOperationExecutor.java:199)
    at org.gradle.internal.progress.DefaultBuildOperationExecutor.run(DefaultBuildOperationExecutor.java:110)
    at org.gradle.execution.taskgraph.DefaultTaskGraphExecuter$EventFiringTaskWorker.execute(DefaultTaskGraphExecuter.java:241)
    at org.gradle.execution.taskgraph.DefaultTaskGraphExecuter$EventFiringTaskWorker.execute(DefaultTaskGraphExecuter.java:230)
    at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor$TaskExecutorWorker.processTask(DefaultTaskPlanExecutor.java:123)
    at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor$TaskExecutorWorker.access$200(DefaultTaskPlanExecutor.java:79)
    at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor$TaskExecutorWorker$1.execute(DefaultTaskPlanExecutor.java:104)
    at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor$TaskExecutorWorker$1.execute(DefaultTaskPlanExecutor.java:98)
    at org.gradle.execution.taskgraph.DefaultTaskExecutionPlan.execute(DefaultTaskExecutionPlan.java:626)
    at org.gradle.execution.taskgraph.DefaultTaskExecutionPlan.executeWithTask(DefaultTaskExecutionPlan.java:581)
    at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor$TaskExecutorWorker.run(DefaultTaskPlanExecutor.java:98)
    at org.gradle.internal.concurrent.ExecutorPolicy$CatchAndRecordFailures.onExecute(ExecutorPolicy.java:63)
    at org.gradle.internal.concurrent.ManagedExecutorImpl$1.run(ManagedExecutorImpl.java:46)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at org.gradle.internal.concurrent.ThreadFactoryImpl$ManagedThreadRunnable.run(ThreadFactoryImpl.java:55)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.OutOfMemoryError: GC overhead limit exceeded
    at com.intellij.lang.impl.MarkerPool.allocateStartMarker(MarkerPool.java:40)
    at com.intellij.lang.impl.PsiBuilderImpl.createMarker(PsiBuilderImpl.java:829)
    at com.intellij.lang.impl.PsiBuilderImpl.mark(PsiBuilderImpl.java:822)
    at com.intellij.lang.impl.PsiBuilderAdapter.mark(PsiBuilderAdapter.java:107)
    at org.jetbrains.kotlin.parsing.AbstractKotlinParsing.mark(AbstractKotlinParsing.java:73)
    at org.jetbrains.kotlin.parsing.KotlinExpressionParsing.parsePostfixExpression(KotlinExpressionParsing.java:438)
    at org.jetbrains.kotlin.parsing.KotlinExpressionParsing.parsePrefixExpression(KotlinExpressionParsing.java:367)
    at org.jetbrains.kotlin.parsing.KotlinExpressionParsing.access$200(KotlinExpressionParsing.java:41)
    at org.jetbrains.kotlin.parsing.KotlinExpressionParsing$Precedence$2.parseHigherPrecedence(KotlinExpressionParsing.java:165)
    at org.jetbrains.kotlin.parsing.KotlinExpressionParsing.parseBinaryExpression(KotlinExpressionParsing.java:312)
    at org.jetbrains.kotlin.parsing.KotlinExpressionParsing.access$300(KotlinExpressionParsing.java:41)
    at org.jetbrains.kotlin.parsing.KotlinExpressionParsing$Precedence.parseHigherPrecedence(KotlinExpressionParsing.java:210)
    at org.jetbrains.kotlin.parsing.KotlinExpressionParsing.parseBinaryExpression(KotlinExpressionParsing.java:312)
    at org.jetbrains.kotlin.parsing.KotlinExpressionParsing.access$300(KotlinExpressionParsing.java:41)
    at org.jetbrains.kotlin.parsing.KotlinExpressionParsing$Precedence.parseHigherPrecedence(KotlinExpressionParsing.java:210)
    at org.jetbrains.kotlin.parsing.KotlinExpressionParsing.parseBinaryExpression(KotlinExpressionParsing.java:312)
    at org.jetbrains.kotlin.parsing.KotlinExpressionParsing.access$300(KotlinExpressionParsing.java:41)
    at org.jetbrains.kotlin.parsing.KotlinExpressionParsing$Precedence.parseHigherPrecedence(KotlinExpressionParsing.java:210)
    at org.jetbrains.kotlin.parsing.KotlinExpressionParsing.parseBinaryExpression(KotlinExpressionParsing.java:312)
    at org.jetbrains.kotlin.parsing.KotlinExpressionParsing.access$300(KotlinExpressionParsing.java:41)
    at org.jetbrains.kotlin.parsing.KotlinExpressionParsing$Precedence.parseHigherPrecedence(KotlinExpressionParsing.java:210)
    at org.jetbrains.kotlin.parsing.KotlinExpressionParsing.parseBinaryExpression(KotlinExpressionParsing.java:312)
    at org.jetbrains.kotlin.parsing.KotlinExpressionParsing.access$300(KotlinExpressionParsing.java:41)
    at org.jetbrains.kotlin.parsing.KotlinExpressionParsing$Precedence.parseHigherPrecedence(KotlinExpressionParsing.java:210)
    at org.jetbrains.kotlin.parsing.KotlinExpressionParsing.parseBinaryExpression(KotlinExpressionParsing.java:312)
    at org.jetbrains.kotlin.parsing.KotlinExpressionParsing.access$300(KotlinExpressionParsing.java:41)
    at org.jetbrains.kotlin.parsing.KotlinExpressionParsing$Precedence.parseHigherPrecedence(KotlinExpressionParsing.java:210)
    at org.jetbrains.kotlin.parsing.KotlinExpressionParsing.parseBinaryExpression(KotlinExpressionParsing.java:312)
    at org.jetbrains.kotlin.parsing.KotlinExpressionParsing.access$300(KotlinExpressionParsing.java:41)
    at org.jetbrains.kotlin.parsing.KotlinExpressionParsing$Precedence.parseHigherPrecedence(KotlinExpressionParsing.java:210)
    at org.jetbrains.kotlin.parsing.KotlinExpressionParsing.parseBinaryExpression(KotlinExpressionParsing.java:312)
    at org.jetbrains.kotlin.parsing.KotlinExpressionParsing.access$300(KotlinExpressionParsing.java:41)

1.java性能调优面临的问题

  • 能运行大内存堆的机器越来越多。催生了大内存堆的垃圾回收器G1
  • 云计算又提升了单CPU和小内存的能力,这个时候又需要针对小内存和单CPU进行优化

2.java性能调优包含的领域

  • 调整tuning flags ----编译器和垃圾收集器等。
  • API的最佳实践

3.JVM tuning flags 调优参数

  • Boolean类型:-XX:+FlagName表示启用,-xx:-FlagName表示禁用。
  • key value类型:-XX:FlagName=Param,一般Param是任意值。
  • 使用-XX:+PrintFlagsFinal 打印当前环境下所有flag的默认值。
  • 这些基于环境自动优化的过程称为ergonomics(自动优化).自动优化的前提----JVM会把机器识别为Client类或者Server类。32位的windows不考虑CPU核数,直接判定为Client类。32位的+单核的话。也是判定为Client类,不考虑操作系统。其他都认为是Server类。

4.JVM调优的瓶颈---数据库。

  • 有时候JVM调优后负载可以上升,但是数据库是瓶颈,JVM性能好反而导致数据库性能进一步下降。这样会得出一个错误的结论:JVM性能越好,整体性能反而更糟。
  • 所以负载能力和响应时间有时候需要权衡。如CPU密集型,增加线程数,等待锁的时间增加,上下文切换增加,延时增加。

5.性能条有的常见手段。

  • 借助性能分析,来优化代码。重点关注性能分析中最耗时的操作。但是这个不意味着只看叶子方法(请把调用链路看成一棵树)
  • 使用奥卡姆剃刀原理()分析性能问题。新代码比机器配置更可能引起性能问题。机器配置比JVM或者操作系统的bug更容易引起性能问题。即----最可能的原因往往最容易解释。不要一上来就跳到不太可能的场景。
  • 为最常用的操作,编写简单的算法。

-------------------------------------性能测试的方法-----------------
1.原则1---测试真实的应用,应该在产品实际的使用环境中进行测试

  • 微基准测试microbenchmarks,微小代码单元的测试。但是要注意两个计时之间的代码是否被使用过,如下面的代码
public void doTest() {
// Main Loop
double l;
long then = System.currentTimeMillis();
11
www.it-ebooks.infofor (int i = 0; i < nLoops; i++) {
l = fibImpl1(50);
}
long now = System.currentTimeMillis();
System.out.println("Elapsed time: " + (now - then));
}
...
private double fibImpl1(int n) {
if (n < 0) throw new IllegalArgumentException("Must be > 0");
if (n == 0) return 0d;
if (n == 1) return 1d;
double d = fibImpl1(n - 2) + fibImpl(n - 1);
if (Double.isInfinite(d)) throw new ArithmeticException("Overflow");
return d;
}

由于斐波那契的结果没被使用,智能编译器会优化成如下代码

long then = System.currentTimeMillis();
long now = System.currentTimeMillis();
System.out.println("Elapsed time: " + (now - then));

那么会得到错误的结果为几毫秒。
解决办法是---把l提升为实例变量,并且标记为volatile(强制每次写以后有一次的同步刷新---实际使用了cpu栅栏)
即确保读取被测试的结果,而不是i简单的写入
+不要包括无关的操作

  • 必须输入正确的参数,提前预知参数的正确性,而不是等运行出错了才知道.这个和我们常说的快速失败很像。
public double fibImplSlow(int n) {
if (n < 0) throw new IllegalArgumentException("Must be > 0");
if (n > 1476) throw new ArithmeticException("Must be < 1476");
return verySlowImpl(n);
}

  • 热点代码比较快,就是一段代码执行的越多,他的速度就越快

2.宏基准测试MacroBenchmarks

  • 必要性,JVM共用cpu的时候,GC效率不一样

3.Mesobenchmarks 介基准测试