当前位置:网站首页>JVM简单入门-02
JVM简单入门-02
2022-06-23 10:07:00 【cfcoolya】
一.JVM体系结构图
在整个JVM学习过程中,这张图要深深记住,这样才不会迷失方向

二.Native方法
在多线程Thread的start方法源码中,有一个方法【private native void start0()】,其中有一个关键字native。
- 凡是带了native关键字的,说明Java的作用范围达不到了,回去调用底层C语言的库。
- 会进入本地方法栈,调用本地方法接口JNI(Java native interface)

历史原因:Java诞生的时候是C、C++横行的时候,想要立足,必须调用C、C++的程序,于是就在内存中专门开辟了一块区域标记为native的代码,它的具体做法是在Navitive Method Stack中登记navtive方法,在执行引擎的时候加载Native Libraries。
三.方法区
方法区(Method Area)是被所有线程共享,所有定义的方法的信息都保存在该区域。
静态变量、常量、类信息(构造方法、接口定义)、运行时的常量池存在方法中,但实例变量存在堆内存中和方法区无关(static、final、class、常量池)
四.栈
- 用一种通俗的语言来解释就是:喝多了吐就是栈,吃多了拉就是队列。
- 栈的存储速度比堆要快,仅次于寄存器,栈数据可以共享。
- 栈不存在垃圾回收问题,只要线程一旦结束,该栈就Over,生命周期跟线程一致,是线程私有的。
五.堆(sun-hotspot)
一个JVM只有一个堆内存,堆内存的大小是可以调节的;类加载器读取了类文件后,需要把类、方法、常变量放到堆内存中,保存所有引用类型的真实信息。
5.1堆
- 新生区 Young/New
- 养老区 Tenure/Old
- 永久区 Perm

GC垃圾回收主要在新生区和养老区,又分为轻GC和重GC,如果内存不够,或者存在死循环,就会导致java.lang.OutOfMemoryError:Java heap space.
5.2新生区
新生区又分为两部分:伊甸区(Eden)和幸存区(Survivor Space),所有类都是在Eden区被new出来的。
当Eden的空间用完时,程序又要创建对象,JVM的垃圾回收器将对Eden区进行垃圾回收(Minor GC),类似操作0区满了,移动到1区,养老区。若养老区执行了Full GC后依然无法进行对象的保存,就会产生OOM异常“Out of MemoryError”。
若出现OOM,说明Java虚拟机的堆内存不够,原因如下:
- Java虚拟机的堆内存设置不够,可以通过参数 -Xms(初始值大小),-Xmx(最大大小)来调整
- 代码中创建了大量的对象,并且长时间不能被垃圾收集器收集或者死循环
5.3永久区
- 用于存放JDK自身所携带的Class,Interface的元数据,存储的是运行环境必须的类信息
- 此区域的数据是不会被垃圾回收器收掉的,关闭JVM才会释放此区域所占用的内存
- Java.lang.OutOfMemoryError:PerGen space说明Java虚拟机对永久代Perm内存设置不够
- JDK1.8及之后无永久代,常量池1.8在元空间
5.4堆内存调优
-Xms:设置初始分配大小,默认物理内存的1/64
-Xmx:最大分配内存,默认物理内存的1/4
-XX:PrintGCDetails:输出详细的GC处理日志
六.方法区+堆+栈交互

图解:https://blog.csdn.net/thera_qing/article/details/110544168
学习来自:B站-狂神说
边栏推荐
- 解决audio自动播放无效问题
- 2021-05-11 static keyword
- Gstore weekly gstore source code analysis (IV): black and white list configuration analysis of security mechanism
- 搭建一个QQ机器人叫女友起床
- Build the information and innovation industry ecology, and make mobile cloud based on the whole stack of independent innovation
- 2021-04-16 array
- MySQL optimistic lock and pessimistic lock
- Golang 快速上手 (1)
- AI system frontier dynamics issue 38: Google has abandoned tensorflow?; Four GPU parallel strategies for training large models; Father of llvm: modular design determines AI future
- Successful experience of postgraduate entrance examination in materials and Chemical Engineering (metal) of Beijing University of Aeronautics and Astronautics in 2023
猜你喜欢

云原生数据库-Amazon RDS

太无奈!微软停售 AI 情绪识别等技术,直言:“法律跟不上 AI 的发展”

安装typescript环境并开启VSCode自动监视编译ts文件为js文件

Build the information and innovation industry ecology, and make mobile cloud based on the whole stack of independent innovation

Fill the pit for repvgg? In fact, it is the repoptimizer open source of repvgg2

RPC kernel details you must know (worth collecting)!!!

Go语言JSON 处理

Numerical calculation method

Comic | code review is driving me crazy!

SQL教程之SQL 中数据透视表的不同方法
随机推荐
开发者,你对云计算可能有些误解
Successful experience of postgraduate entrance examination in materials and Chemical Engineering (metal) of Beijing University of Aeronautics and Astronautics in 2023
Copilot免费时代结束!正式版67元/月,学生党和热门开源项目维护者可白嫖
2021-05-11 abstract class
给RepVGG填坑?其实是RepVGG2的RepOptimizer开源
JS教程之 什么是 JSX?为什么我们需要它?
Dr. Sun Jian was commemorated at the CVPR conference. The best student thesis was awarded to Tongji Ali. Lifeifei won the huangxutao Memorial Award
Golang 快速上手 (2)
文献综述怎么写 ,一直没头绪写不出来怎么办?
After the uncommitted transactions in the redo log buffer of MySQL InnoDB are persisted to the redo log, what happens if the transaction rollback occurs? How does the redo log handle this transaction
SQL教程之SQL 中数据透视表的不同方法
基于STM32设计的宠物投喂器
2021-05-11static关键字
实现领域驱动设计 - 使用ABP框架 - 通用准则
Install the typescript environment and enable vscode to automatically monitor the compiled TS file as a JS file
云原生数据库-Amazon RDS
2021-04-12 the first implementation of linked list!!!
2021-05-07封装 继承 super this
2021-04-16数组
Jump game of leetcode topic analysis