当前位置:网站首页>Take care of JVM performance optimization (own note version)
Take care of JVM performance optimization (own note version)
2022-08-04 03:44:00 【It's old Xue】
1.什么是JVM
是一种用于计算设备的规范,它是一个虚构出来的计算机,是通过在实际的计算机上仿真模拟各种计算机功能来实现的. 主流虚拟机
为什么要进行优化JVM?
因为我们的java代码都是运行在jvm上面的,So make the code run efficiently,那就让jvmOptimized to run fast,The so-called optimization is to configure some parameters
2.JVM的组成
From the figure above, it can be seen that roughly:
3类加载器子系统
是什么?
It's just a subsystem for loading classes
3.1About the loading process of the class loader subsystem(常见的一道面试题)
A加载----我(first person class loader)To get to the first字节码文件Then read it in memory,I have two ways of loading,方式一 ,隐式加载只要你是newto create objects,I have implicit class loading of corresponding tojvm里,方式二,显示加载I go straight throughclass.forName()method loads this class intojvm里
B验证---When I load this class intojvm里的时候,I want to verify that this bytecode file is real bytecode,I pass metadata,字节码,Symbols such as reference for validation
C准备--After verifying that you are a real bytecode file,I will go to see you in classstatic修饰的变量,I'll give it to you first分配内存空间and the initial value is set to0或null,If here you have your own assignment,I still give you memory space and initial value here,Because the fifth step is assigning your own value,这里just allocate memory space for you,如果你即有final和static修饰,I'll give it to you right here赋值
D解析--Just at the time of validation verified symbol references,Here is to put some inside your class符号引用的(For example, referencing the fully qualified name)直接去引用
E初始化--For some variables in the preparation stageinitialized assignment
3.2What are the class loaders
3.3加载的顺序:
4What is the loading order of the loader??
双亲委派:
大致意思是:Layer by layer using classloaders,Start the class loader from the boss first to find this class and load it,If not, go to the second extension class loader to find and load,Loaded but not loaded into the third application class,有加载,None loaded into custom loader,Is there anything else,就报ClassNotFountException错误.
为什么要这样呢?
为了安全,so that classes in the system will not be overridden,Load it on the virtual machine first,It also ensures that we extend our functionality
4.运行时数据区
4.1程序计数器
注意:it's not for counting,It is used to determine the order of execution of instructions,是唯一 一个jvm没有规定任何OOM的区块(So the area where memory overflow does not occur)My understanding is like4x100the relay race,It is the guide between each player,guide the next step,通过程序计数器,Complete the relay one by one,Can be linked into a complete relay race,so as to complete the execution of the entire game
包括任何的分支、循环、跳转、异常处理、Thread recovery operations such as all need to rely on the program counter to guide the current thread
4.2虚拟机栈
每个线程在创建时都会创建一个虚拟机栈,其内部保存一个个的栈帧(Stack Frame),对应着一次次的Java方法调用,是线程私有的
虚拟机会为每个线程分配一个虚拟机栈,每个虚拟机栈中都有若干个栈帧,A local variable table is stored in each stack frame,操作数栈,动态链接,返回地址等
每个方法执行,伴随着进栈
方法执行结束后,伴随着出栈.
There will be a memory overflow problem for the stack
The native method stack is the same as the virtual machine stack,The difference is that the virtual machine stack executesjava方法,本地方法栈执行的是本地方法(Native Method)
4.3方法区
Methods area mainly to store the virtual machine shelf ah class information,常量,静态变量,And some of the compiled code, etc,
在jdk1.3-1.6The between method area is a logical part of the heap
In order to distinguish it from the heap, it is also called permanent
在jdk1.7Move the static variables and string constants originally in the method area to the heap memory
在jdk1.8After the method area does not exist,Move the original stuff to the metaspace,Metaspace does not exist on heap memory,in local memory
4.4堆空间
Heap space is a all threads are Shared,存放对象的区域
5 垃圾回收机制-GC
5.1Garbage Collection Identification Algorithm
Because not everything is trash,All must first identify whether it is garbage to be recycled
A-引用计数算法(标记):
give each object a counter,on the counter when someone uses it+1,when there is a place not using it-1
弊端:If two objects refer to each other,Neither object is being referenced anywhere else,Then it is impossible to judge whether these two objects are useful or not.
B-可达性分析算法:
Accessibility is according to the algorithmGC Root 的对象为起点出发 GC Rootsis the starting point for garbage collection,For example, the objects referenced in the local variable table in the virtual machine stack,Or is the method to the object referenced by the static property,方法区中常量引用的对象,Objects in native methods are available asGC Root
When an object has no reference chain directly or indirectly toGC Roots Indicates that this object is a dead object and can be recycled
如图:D,E没有任何的引用链,所以会被回收
注意:虽然D E 没有到GC的引用链 But when reaching the reachability analysis algorithm,会先判断对象是否执行了 finalize 方法,如果未执行,则会先执行 finalize 方法,我们可以在此方法里将当前对象与 GC Roots 关联,这样执行 finalize 方法之后,GC 会再次判断对象是否可达,如果不可达,则会被回收,如果可达,则不回收!(finalize 方法只会被执行一次)
5.2垃圾回收算法
A-标记清除
两步走:1.To remove the object to mark first,2.clear the marked object
弊端:两个过程,效率不高,After clearing, there are still a lot of memory fragments that are not clean
B-复制算法
First divide the memory into equal memory spacesA和B,当A的空间用完,Copy the surviving objects on this side toB,再把AAll memory space is cleared
弊端:Less than half the space actually used
C-标记整理算法
First mark the objects that can be used,Then move all marked objects to a segment,最后清除可用对象边界以外的内存
D-分代收集算法
新生代使用minor gc 老年代使用full gc
E-Minor GC 和Full GC
Minor GC Is the new generation using recovery algorithm,This recycling is fast,
Full GC is the recycling algorithm used in the old age,and will be accompanied by at least oneMinor GC This kind of pause time is relatively long,
5.3常见的垃圾收集器
A-Serial 新生代收集器
采用复制算法进行垃圾收集,when garbage collection,will cause all threads to be suspended
B-ParNew 新生代收集器
是Serialmultithreaded version,基本和Serial一样,相比于Serial It is possible to shorten the waiting time of all threads
C-Parallel Scavenge 新生代收集器
The collector is multi-threaded,It is possible to achieve a manageable throughput,(吞吐量=运行用户代码时间/(运行用户代码时间+垃圾收集时间))
D-Serial Old 老年代收集器
是单线程收集器,采用的是标记整理算法
E-CMS 老年代收集器
The main purpose is the shortest collection pause time for the purpose of the collector
F-G1 收集器
G1 Unlike other collectors, it is not young and old,is the entire heap memory
5.4JVMThe optimized main core:使用较小的内存占用来获得较高的吞吐量或者较低的延迟
后续更新,The specific way of optimization and setting related parameters,The purpose of this is to give the reader a general understanding ofJVMand why to optimize
欢迎进行学习交流,不足之处请指出,喜欢麻烦点赞+收藏,谢谢各位大佬了
边栏推荐
- 2022 Hangzhou Electric Power Multi-School League Game 5 Solution
- Reproduce 20-character short domain name bypass
- base address: environment variable
- tkmapper的crud示例:
- [Medical Insurance Science] To maintain the safety of medical insurance funds, we can do this
- 机器学习之视频学习【更新】
- 6口全千兆二层网管型工业以太网交换机千兆2光4电光纤自愈ERPS环网交换机
- 创新互融|华秋赋能助力OpenHarmony生态硬件开发落地
- 2022支付宝C2C现金红包PHP源码DEMO/兼容苹果/安卓浏览器和扫码形式
- FPGA解析B码----连载3
猜你喜欢
随机推荐
力扣(LeetCode)215. 数组中的第K个最大元素(2022.08.03)
SQL注入中 #、 --+、 --%20、 %23是什么意思?
Pine Script | How to display and typeset a plot switch?
PHP高级开发案例(1):使用MYSQL语句跨表查询无法导出全部记录的解决方案
Homemade bluetooth mobile app to control stm8/stm32/C51 onboard LED
Enterprise live broadcast is on the rise: Witnessing focused products, micro-like embracing ecology
sql语句查询String类型字段小于10的怎么查
类如何只能静态分配和只能动态分配
机器学习模型的“可解释性”
STM8S project creation (STVD creation) --- use COSMIC to create a C language project
JVM的内存模型简介
【Ryerson情感说话/歌唱视听数据集(RAVDESS) 】
"Introduction to nlp + actual combat: Chapter 8: Using Pytorch to realize handwritten digit recognition"
FPGA parsing B code----serial 3
Architecture of the actual combat camp module three operations
【观察】超聚变:首提“算网九阶”评估模型,共建开放繁荣的算力网络
[Medical Insurance Science] To maintain the safety of medical insurance funds, we can do this
【翻译】Terraform和Kubernetes的交集
Returns the maximum number of palindromes in a string
Mockito unit testing