当前位置:网站首页>jvm-对象生命周期
jvm-对象生命周期
2022-07-03 13:28:00 【名字忒不好取】
jvm中对象的生命周期

创建阶段
(1)为对象分配存储空间
(2)开始构造对象
(3)从超类到子类对static成员进行初始化
(4)超类成员变量按顺序初始化,递归调用超类的构造方法
(5)子类成员变量按顺序初始化,子类构造方法调用,并且一旦对象被创建,并被分派给某些变量赋
值,这个对象的状 态就切换到了应用阶段
应用阶段
(1)系统至少维护着对象的一个强引用(Strong Reference)
(2)所有对该对象的引用全部是强引用(除非我们显式地使用了:软引用(Soft Reference)、
弱引用(Weak Reference)或虚引用(Phantom Reference))
引用的定义:
1.我们的数据类型必须是引用类型
2.我们这个类型的数据所存储的数据必须是另外一块内存的起始地址
不可见阶段
不可见阶段的对象在虚拟机的对象根引用集合中再也找不到直接或者间接的强引用,最常见的就是线程或者函数中的临时变量。程序不再持有对象的强引用。(但是某些类的静态变量或者JNI是有可能持有的 )
不可达阶段
指对象不再被任何强引用持有,GC发现该对象已经不可达
与不可见阶段的区别就是:不可达是经过算法之后的
那么有哪些算法?
引用计数:
对象有一个引用就+1,去掉一个引用就-1,当引用数为0就认为是没有引用,可以回收
问题:解决不了循环依赖问题

可达性分析:通过GC Root的对象,开始向下寻找,看某个对象是否可达
什么能看作GC ROOT:加载器、Thread、虚拟机栈的本地变量表、static成员、常量引用、
本地方法栈的变量等本质上就是一组活跃的引用

收集阶段
看对象需不需要执行finalize方法:
如果不需要:虚拟机已经调用过finalize方法或者对象没有重写finalize方法,那么对象可以回收
需要:虚拟机将对象放入F-queue,创建一个低优先级的线程执行队列中对象的finalize方法
finalize方法中是否重新为该对象建立引用连接
是:虚拟机将对象移出F-queue队列,对象重新回到应用阶段
否:对象可以回收
finalize方法会影响JVM的对象以及分配回收速度finalize方法可能会让对象复活
终结阶段
对象的finalize()函数执行完成后,对象仍处于不可达状态,该对象进程终结阶段。
对象内存空间重新分配阶段
GC对该对象占用的内存空间进行回收或者再分配,该对象彻底消失
内存溢出
1、堆内存溢出:java.lang.OutOfMemoryError: GC overhead limit exceeded
-Xmx20M :程序运行时最大能扩容到的大小
-Xms20M:初始化堆大小
2、方法区内存溢出:java.lang.OutOfMemoryError: Metaspace
-XX:MetaspaceSize:初始化方法区大小
-XX:MaxMetaspaceSize:程序运行时最大能扩容到的大小
3、占内存溢出:java.lang.StackOverflowError
-Xss:栈的大小(默认1M)
边栏推荐
- Summary of common error reporting problems and positioning methods of thrift
- NFT new opportunity, multimedia NFT aggregation platform okaleido will be launched soon
- HALCON联合C#检测表面缺陷——HALCON例程autobahn
- SQL Injection (POST/Select)
- Qt学习24 布局管理器(三)
- Flutter动态化 | Fair 2.5.0 新版本特性
- IBEM mathematical formula detection data set
- Installation impression notes
- 交联环糊精金属有机骨架负载甲氨蝶呤缓释微粒|金属-有机多孔材料UiO-66负载黄酮苷类药物|齐岳
- logback日志的整理
猜你喜欢
![[how to solve FAT32 when the computer is inserted into the U disk or the memory card display cannot be formatted]](/img/95/09552d33d2a834af4d304129714775.png)
[how to solve FAT32 when the computer is inserted into the U disk or the memory card display cannot be formatted]

Qt学习18 登录对话框实例分析

Use and design of Muduo buffer class

Using registered classes to realize specific type matching function template

JVM family - overview, program counter day1-1

Flutter dynamic | fair 2.5.0 new version features

The latest BSC can pay dividends. Any B usdt Shib eth dividend destruction marketing can
![[redis] cache warm-up, cache avalanche and cache breakdown](/img/df/81f38087704de36946b470f68e8004.jpg)
[redis] cache warm-up, cache avalanche and cache breakdown

Qt学习25 布局管理器(四)

JVM系列——概述,程序计数器day1-1
随机推荐
Qt学习19 Qt 中的标准对话框(上)
可编程逻辑器件软件测试
HALCON联合C#检测表面缺陷——HALCON例程autobahn
Mastering the cypress command line options is the basis for truly mastering cypress
Go language web development series 28: solve cross domain access of CORS with gin contrib / CORS
pytorch 载入历史模型时更换gpu卡号,map_location设置
Ocean CMS vulnerability - search php
PHP maze game
Go language web development series 25: Gin framework: using MD5 to verify the signature for the interface station
[bw16 application] instructions for firmware burning of Anxin Ke bw16 module and development board update
3D vision - 2 Introduction to pose estimation - openpose includes installation, compilation and use (single frame, real-time video)
又一个行业被中国芯片打破空白,难怪美国模拟芯片龙头降价抛售了
[redis] cache warm-up, cache avalanche and cache breakdown
Which securities company has the lowest Commission for opening an account online? I want to open an account. Is it safe for the online account manager to open an account
“又土又穷”的草根高校,凭什么被称为“东北小清华”?
Golang — 命令行工具cobra
Qt学习22 布局管理器(一)
Go: send the get request and parse the return JSON (go1.16.4)
Heap structure and heap sort heapify
Windos creates Cordova prompt because running scripts is prohibited on this system