首页 » jvm性能调优

其实这三个词关系不是很大,但是因为长的像,所以在中国是常见的面试题。哈哈

1.final 修饰变量/参数,不可更改(指针),如果时list,则里面还是能添加对象的,这点和immutable不同啊。
2.finally 和try配合使用,除非try里面用了System.exit()否则,finally一定会执行。
3.finalize 是给垃圾回收器用的。但是垃圾回收的时间是不确定的,所以可能造成OOM哦。而且jvm在调用finalize的时候会吞掉里面的任何异常,造成不可预知的结果。JDK9已经标记这个方法为depreciate。如果想用,可以用java.lang.ref.Cleaner,虽然单独出线程来处理了。不会造成死锁,但是还是建议不要用cleaner。最好的方法还是用完显式的释放。

Exception和Error都继承自Throwable类,可以被throw和catch,那么他们有什么区别呢

1.Exception是程序正常运行中,可以预料的意外情况,可能而且应该被catch,进行处理
2.Error是不大可能出现的情况,绝大部分Error会导致JVM处于非正常的不可恢复的状态,如OOM,不便于或者也不需要捕获。
3.Exception分为可检查异常和不检查异常。可检查异常是编译器检查的一部分,必须显示的进行捕获处理,如IO异常。不检查异常是运行时异常,可以再运行时用代码判断是否捕获。如空指针,数组超界。常见问题参考--https://blog.csdn.net/bryantlmm/article/details/78118763
4.注意try catch是有性能损耗的,不要用try包括大量的代码,用if else会更高效。对于追求极致性能的底层类库,会使用创建不进行栈快照的Exception。
5.Reactive 编程时,不能简单的抛异常,异常处理也要非常小心,很容易导致其他问题

说起java平台,最多的是跨平台,一次编译,到处运行。java代码用javac编译成bytecode 的class文件,然后运行时,解释器转换成最终的机器码。JIT会把热点代码编译执行,而不是解释执行。

1.类加载器,Bootstrap,Application,Extension class loader,参考周志明《深入理解java虚拟机》。经过加载,验证,链接,初始化。
2.常见的垃圾收集器,SerialGC,Parallel GC,CMS,G1
3.java分为编译期和运行时。

  • 编译生成的.class文件是平台无关的,会屏蔽具体的硬件细节和操作系统细节,这是“一次编译,到处运行”的基础
  • 运行时,通过类加载器Class Loader加载字节码(.class)文件。可以解释加载也可以编译加载。jdk 8 默认的是混合模式,即java运行指令-Xmixed,但是Client模式和Server模式有明显的区别
  • client模式以启动快速为目的,启动时 会进行1500次(上限)调用来进行编译。这里的JIT是C1
  • Server模式以长时间运行为目的,启动时 会进行上万次调用,来确定搞笑的编译,这里的JIT是C2,C2默认采用所谓的分层编译TieredComplilation
  • 当然可以再运行时指定-Xcomp,告诉jvm关闭解释器,编译运行,但这种未必是最搞笑的,而且启动会慢很多
  • 也可以指定-Xint,只解释运行
  • 除了编译和解释,还有一种aot模式(oracle jdk 9引入),将某个模块编译成机器代码,可以减少启动预热开销,使用jaotc工具来使用.注意分层编译可以和AOT一起使用,不是非此即彼的。
    jaotc --output libHelloWorld.so  HelloWorld.classs
    java -XX:AOTLibrary=./libHelloWorld.so HelloWorld

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)