当前位置:网站首页>JVM internal structure and various modules operation mechanism
JVM internal structure and various modules operation mechanism
2022-08-03 03:02:00 【find bugs】
1.JVMInternal model diagram
JVM执行流程:
Take advantage of front-end compilers(javac)将.java文件编译为.class文件(字节码文件),Then the subsystem will be loaded through the class.class文件加载到运行时数据区,Then pass the execution engine pair.class文件进行解释执行.
2 类加载子系统
The class loading subsystem has seven operations:加载、链接(验证、准备、解析)、初始化、使用、卸载
2.1 加载
把.classThe file is read into the runtime data area,详细步骤为:
(1)Read a binary stream to this class by its fully qualified name(即,.class文件);
(2)Read the static structure of the binary stream into the method area in the runtime data area;
(3)Create an instance object of the class on the heap.
注意:When loading, the parent delegation mechanism is used,其原理如下图所示,When a class loader receives a class loading request,This request is delegated to its parent classloader bottom-up,If the top-level startup class loader(Bootstrap ClassLoader)This class cannot be loaded,Then the request is handed back to its subclass loader from the top down,There are classloaders on the way to load this class,则进行加载,Otherwise, it has been passed down.If reaching the bottommost custom class loader(If there is no custom class loader,It is the application class loader(Application ClassLoader))Still can't load this class,则报出异常.
2.2 链接
2.2.1 验证
验证字节码文件是否符合JVM规范,For example, the beginning of the file for a bytecode file is “CA FE BA BE”(称为魔数),This identifies it isJVM的字节码文件.
2.2.2 准备
为静态变量(static修饰)分配内存并赋初始值.
2.2.3 解析
将常量池中的符号引用(如#1,#2May represent a class,A symbolic reference to a method)变为直接引用(内存地址).
2.3 初始化
The assignment of static variables and static code blocks(static{})The operations that take place integrate as clinit方法进行执行.In fact, it is a real assignment to a static variable.
2.4 使用
2.5 卸载
3 运行时数据区
3.1线程私有区域
Java虚拟机栈:
保存局部变量表、操作数栈、动态链接、方法出口;Each thread has a separate one,用于执行程序;The stack frame is taken as a unit,A stack frame is equivalent to a method;执行流程:当调用main方法时,会将mainThe method is pushed to the top of the stack as a stack frame,若在执行mainmethod calls other methods,Other methods are also pushed to the top of the stack as stack frames,When the method on the top of the stack is executed, the stack is popped.If all stack frames are popped,Then the program operation ends.
本地方法栈:
本地方法栈与Java虚拟机栈类似,But it is for executing native methods(Native Methods).When the native method stack executes,Need to call to native method interface(也叫JNI/Java Native Interface),The native method interface needs to call the native method library.
程序计数器:
控制程序指令的执行顺序,程序该怎么执行,哪个方法先执行,哪个方法后执行.
3.2 线程共享区域
方法区:
保存类的信息、常量池、方法数据、方法代码、JIT代码缓存等等;Method area is a logical concept(即,JVM规范),在JDK1.7The implementation of the previous method area is called the permanent generation(PermGen space),it's in the heap.在JDK1.8永久代被移除,Turn it into a metaspace(Metaspace).The biggest difference between metaspace and permanent generation is that,元空间使用的是直接内存(本地内存),The permanent generation uses heap memory.
Java堆:
保存new出来的实例对象;
JDK1.6时字符串常量池in the runtime constant pool,Part of the runtime constant pool;
JDK1.6到JDK1.7The change is to put static constants and 字符串常量Moved from the method area to the heap;
JDK1.7到JDK1.8The change is that the implementation of the method area is changed from permanent generation to meta space.
That is, the string constant pool is inJDK1.7Then moved to the heap,And it stores a reference to a string object,而不是对象本身.
在JDK1.8版本中,Java堆划分为新生代(NewGen)和老生代(OldGen).
新生代:
- Cenozoic by Eden(Eden space)、幸存from区(Survival from)、幸存to区(Survival to)构成.
- 新生代发生的GC是轻GC(Minor GC),使用的GC算法是复制算法,The principle of the replication algorithm is:当触发Minor GC时,Using the reachability analysis algorithmEdenZone live objects are moved toSurvival to区域,同时也将Survival fromThe live objects of the area are also moved toSurvival to区域,然后将Survival toArea live objects are copied toSurvival from区域,And clear those invalid objects.即,每次GCAll are stored in the surviving objectSurvival to区域,然后将Survival from区域和Survival toArea swaps positions(It is actually the copying process of the object)来保证Survival to区域是空的.
- The replication algorithm is suitable for garbage collection of objects with short lifetimes,The advantage is that the algorithm complexity is low,执行效率快,But the memory space is wasted.
老生代:
- What happened in the Old Generation was heavyGC(Full GC),使用的是标记清除和标记整理A hybrid version of the algorithm,When less memory fragmentation occurs,使用标记清除算法.较多时,使用标记整理算法.
- 标记清楚算法:Use the reachability analysis algorithm to mark surviving objects(可达对象),Then unreachable objects are removed uniformly.共两次操作,执行效率较低,And may generate more memory fragmentation,相比复制算法,内存浪费较少.
- 标记整理算法:Use the reachability analysis algorithm to mark surviving objects(可达对象),Then move them to one end,Remove unreachable objects other than the end.In this way, the continuity of memory can be guaranteed,不会有内存碎片,Memory usage is greatly improved,But the execution is less efficient.
4 执行引擎
The execution engine is run by the interpreter、JIT编译器、Garbage collector composition.
解释器:
Interpreted execution of program code.
JIT编译器:
Used to place hotspot code(Code that is executed a lot of times)提前编译,存储在方法区中,以便下次使用.This is for the efficiency of program execution.
垃圾回收器:
新生代垃圾回收器:Serial、ParNew、Parallel Scavenge
Old generation garbage collector:Serial Old、Parallel Old、CMS
边栏推荐
猜你喜欢
Brute force recursion to dynamic programming 07 (516. Longest palindrome subsequence)
增删改查这么多年,最后栽在MySQL的架构设计上!
【SQL】—数据库操作、表操作
吴恩达深度学习deeplearning.ai——第一门课:神经网络与深度学习——第二节:神经网络基础(上)
【面经】被虐了之后,我翻烂了equals源码,总结如下
v-if条件判断及v-show
torchvision.datasets.ImageFolder使用详解
236. The binary tree in recent common ancestor
2022-08-02:小红拿到了一个大立方体,该大立方体由1*1*1的小方块拼成,初始每个小方块都是白色。 小红可以每次选择一个小方块染成红色, 每次小红可能选择同一个小方块重复染色, 每次染色以后,
软件定义网络实验之自定义拓扑开发
随机推荐
9-WebUtil工具类.md
iNFTnews | 元宇宙的潜力:一股推动社会进步的力量
一篇文章玩明白Stack-migration
apache-activemq-5.14.1
如何准备考pmp?
YYGH-BUG-06
无法启动服务 错误 193 0xc1
南瓜科学新品上线 开辟益智玩具新世界
ssh(sshd)安全配置
Greenplum数据库故障分析——can not listen port
VS2010 组件列表与对应名称
php一维数组合并
为什么要使用 playwright 做浏览器自动化测试?
[QNX Hypervisor 2.2用户手册]10 虚拟设备参考
openCV第二篇
10-security登录
公司代码学习笔记
SAP ABAP Gateway Client 里 OData 测试的 PUT, PATCH, MERGE 请求有什么区别
[Example构造方法增加notNull参数,默认false,允许值为null,值为null的时候不加入到条件中
【静态类型和动态类型 编译检查和运行检查 Objective-C中】