当前位置:网站首页>Summary of JVM interview questions (continuously updated)
Summary of JVM interview questions (continuously updated)
2022-08-01 01:11:00 【March is immortal】
JVM内存划分
Java堆: 存储对象实例. 分为新生代(伊甸园区,幸存者1区,幸存者2区)老年代
方法区: Its implementation is yuan space,存储运行时常量池,被虚拟机加载的类信息,常量等.
虚拟机栈:执行JavaMethods provide service,Storage methods runtime data needed,字符串常量池,静态变量
本地方法栈: Provide service for perform local method
程序计数器: 存储下一条指令的地址,由执行引擎读取下一条指令
类加载过程
1)加载 :The class after the corresponding binary file is loaded into the memory,类的元信息,Runtime data stored in area,And generate the class in the heapClass对象作为方法区这个类的各种数据的访问入口
2)链接:先验证class文件的正确性,Then the allocated memory to class variables and set the default initial value,Then the symbols referenced within the constant pool into direct reference
3)初始化 :javacThe compiler automatically collect all class variable assignment in class action and static block of statements merged intoclinit()方法,Then go to class variables to do display initialization.
若该类具有父类,JVM会保证子类的clinit()执行前,父类的clinit()已经执行完毕. 虚拟机必须保证一个类的clinit()方法在多线程下被同步加锁.
类加载器有哪些?
- 虚拟机自带的加载器
- 启动类加载器:C/C++实现,加载java核心类库(eg:String),不继承ClassLoader类,无父加载器,Load it extend classes and application class loader(Loader is also a class,Need to use the parent loader loading child loader)
- 扩展类加载器:java编写,派生于ClassLoader类,父类加载器–>引导类加载器,加载jre/lib/ext子目录(若用户创建的jar也在此目录下,By the extension class loader to load)
- 系统类加载器:java编写,派生于ClassLoader类,父类加载器–>引导类加载器,User-defined class by default with the system class loader to load
- 自定义加载器
双亲委派机制
如果一个类加载器收到了类加载的请求,它首先不会自己去尝试加载这个类,而是把这个请求委派给父类加载器去完成,If the parent class loader and the parent class loader,则继续向上委派,最终到达顶层启动类加载器,And then to search,When the corresponding loader can complete loading request will go to load
优势:
避免类的重复加载:The parent class loader to load,A subclass need not load
保护程序安全,防止核心API被随意篡改(如自定义类:Java.lang.StringLoading is still the core ofapi下的String)
如何判断某个对象是垃圾
可达性分析算法:
- JVM 中的垃圾回收器通过可达性分析来探索所有存活的对象
- 扫描堆中的对象,会从GC RootThe root node of start scanning,Check whether there is a reference point to the object,如果找不到,则表示可以回收
可以作为GC Root 的对象
- 虚拟机栈(栈帧中的本地变量表)中引用的对象(局部变量).
- The heap object class static attribute references
- 方法区中常量引用的对象
- 本地方法栈中 JNI(即一般说的Native方法)引用的对象
垃圾收集算法有哪些?
1)标记清除算法:通过GC可达性分析算法,Starting from the root node tag all can reach the object,Not marked as the garbage objects,Complete removal has not all been marking object.可能会产生大量碎片
2)复制算法:将原有的内存空间分为两块,每次只使用其中一块,在垃圾回收时,将正在使用的内存中的存活对象复制到未使用的内存块中,然后清除正在使用的内存块中的所有对象.
3)标记-整理:通过GC可达性分析算法,Starting from the root node tag all can reach the object.将所有的存活对象压缩到内存的一段,之后清理边界所有的空间
垃圾回收器
垃圾回收器主要分为以下几种:Serial、ParNew、Parallel Scavenge、Serial Old、Parallel Old、CMS、G1;
Serial: 单线程的收集器,使用复制算法.在进行垃圾回收时,Need to suspend all the executing thread(stop the world)
ParNew: Serial收集器的多线程版本,也需要stop the world,复制算法
Parallel Scavenge: 并发的多线程收集器,采用复制算法,目标是达到一个可控的吞吐量,虚拟机会根据系统的运行状态收集性能监控信息,动态设置这些参数,以提供最优停顿时间和最高的吞吐量;
Serial Old: Serial收集器的老年代版本,单线程收集器,使用标记整理算法.
Parallel Old: 是Parallel Scavenge收集器的老年代版本,使用多线程,标记-整理算法.
CMS: 是一种以获得最短回收停顿时间为目标的收集器,标记清除算法,运作过程:初始标记,并发标记,重新标记,并发清除,收集结束会产生大量空间碎片;
G1: 标记整理算法实现,运作流程主要包括以下:初始标记,并发标记,最终标记,筛选回收.不会产生空间碎片,可以精确地控制停顿;G1将整个堆分为大小相等的多个Region(区域),G1跟踪每个区域的垃圾大小,在后台维护一个优先级列表,每次根据允许的收集时间,优先回收价值最大的区域,已达到在有限时间内获取尽可能高的回收效率;
分代垃圾回收过程
Because most of the objects are facing life evening death,A few surviving object is longer,So the object's recovery is a generational
新创建的对象首先分配在 Eeden 区
新生代空间不足时,触发Minor GC ,eden 区 和 from 区存活的对象使用 - copy 复制算法到 to 中,存活的对象年龄加1,然后交换 from to 的位置,空出大量连续内存区,放对象,The space is insufficient and performMinor GC,如此反复.
minor gc 会引发 stop the world(SWT),暂停其他线程(垃圾回收时会涉及大量关于对象的复制,其他线程会产生不必要的麻烦,如线程不到对象了,因为地址变化了),等垃圾回收结束后,恢复用户线程运行 当幸存区对象的寿命超过阈值时,The life of the biggest default is 15(4bit)----多次回收还存在,说明价值较高,放入老年代,不用频繁回收.会晋升到老年代.Or survivors area half the size of objects of the same age group of survivors in,Or there is a large object,New generation can't fit,Will be promoted to old s.
当老年代空间不足时,会先触发 minor gc,如果空间仍然不足,那么就触发 full fc ,STW停止的时间更长!
Student student = new Student();在内存中做了哪些事情?【创建对象的步骤】
加载Student.class文件进内存(加载、链接、初始化)
In the stack memory for the variables开辟空间
在堆内存为Student对象开辟空间
对学生对象的成员变量进行默认初始化
设置对象的对象头
对学生对象的成员变量进行显示初始化
通过构造方法对学生对象的成员变量赋值
Student对象初始化完毕,把对象地址赋值给s变量
对象的内存布局
边栏推荐
- 北京突然宣布,元宇宙重大消息
- 清华大学陈建宇教授团队 | 基于接触丰富机器人操作的接触安全强化学习框架
- YOLO怎么入门?怎么实现自己的训练集?
- MYSQL事务
- 元宇宙改变人类工作模式的四种方式
- /usr/sbin/vmware-authdlauncher: error while loading shared libraries: libssl.so.1.0.2*解决办法
- Carefully summarize thirteen suggestions to help you create more suitable MySQL indexes
- Team of Professor Chen Jianyu of Tsinghua University | Contact Safety Reinforcement Learning Framework Based on Contact-rich Robot Operation
- Exam preparation plan
- 力扣二叉树
猜你喜欢
What practical projects can machine learning beginners learn?
虹科分享|如何用移动目标防御技术防范未知因素
MYSQL-批量插入数据
Detailed explanation of TCP protocol
pycaret source code analysis: download dataset\Lib\site-packages\pycaret\datasets.py
元宇宙改变人类工作模式的四种方式
Beijing suddenly announced that yuan universe big news
RTL8762DK UART(二)
WeChat applet page syntax
【数据分析】基于matlab GUI学生成绩管理系统【含Matlab源码 1981期】
随机推荐
Detailed explanation of TCP protocol
How to get started with YOLO?How to implement your own training set?
[AMEX] LGBM Optuna American Express Credit Card Fraud Contest kaggle
WindowInsetsControllerCompat is simple to use
Luogu P3373: 线段树
Introduction to machine learning how to?
mySql data view
Luogu P3373: Segment tree
An open source and easy-to-use flowchart drawing tool drawio
RTL8762DK UART (two)
Rasa 3.x 学习系列- Rasa - Issues 4918 学习笔记
RTL8762DK RTC (5)
Matlab/Arcgis processing nc data
Introduction to the five data types of Redis
Cmake introductory study notes
力扣二叉树
js 实现复制功能
cmake入门学习笔记
WebApi 打个Attribute 统一处理异常
RTL8762DK 使用DebugAnalyzer(四)