当前位置:网站首页>jvm three heap and stack
jvm three heap and stack
2022-08-05 05:04:00 【Xi small 2】
一、堆
1.1、什么是堆
Java 堆是虚拟机所管理的内存中最⼤的⼀块,Java 堆是所有线程共享的⼀块内存区域,在虚拟机启动时创建.此内存区域的唯⼀⽬的就是存放对象实例,⼏乎所有的对象实例以及数组都在这⾥分配内存
1.2、堆中存放的数据
1.通过new关键 ,创建的对象存放在堆中;
2.所有线程会共享到同一个堆内存;
3.在堆内存中是有垃圾回收机制的;
1.3、A memory leak occurs
堆内存泄漏问题
内存泄漏(Memory Leak)是指程序中已动态分配的堆内存由于某种原因程序未释放或无法释放,造成系统内存的浪费,导致程序运行速度减慢甚至系统崩溃等严重后果.
内存泄漏: Many objects are created and stored in heap memory,GCAfter being recycled many times, the garbage object cannot be cleaned up.
内存泄漏示例1
- 设置 jvm 测试-Xmx8m When we loop over the data new 出来放到list 中, UserEntity 对象一直被list 引用,无法存放gc 回收,从而内存泄漏 导致 内存溢出
/** * 演示堆内存溢出 -Xmx8m * * @param args */
public static void main(String[] args) {
List list = new ArrayList<>();
while (true) {
list.add(new UserEntity());
}
}
public class UserEntity {
// 申请 1mb 空间
private byte[] mayiktByte = new byte[1024 * 1024];
}

内存泄漏示例2
如下示例:
bytes 如果不设置为null , will always take up space,Even active System.gc(),Can't clean up either
public static void main(String[] args) throws InterruptedException {
System.out.println("1"); // 堆内存 假设 5mb
Thread.sleep(1000 * 30); // 阻塞30s
byte[] bytes = new byte[1024 * 1024 * 10];// 申请10mb内存
System.out.println("2"); // 堆内存 假设 15mb
Thread.sleep(1000 * 30); // 阻塞30s
bytes = null; // null GCRoot 引用链
System.gc(); // 主动调用GC方法 不会立即清理 Heap memory garbage notification
System.out.println("3"); // gc回收 15-10mb 5mb左右
Thread.sleep(1000 * 10000);
}
其他:
- 1、错误使用了 Threadlocal
- 2、HashMap 自定义key 可以导致
1.4、内存溢出:
内存溢出 一般都是 problems caused by memory leaks, gcGarbage objects cannot be recycled,Re-applying for heap memory,The new memory storage object has no space to use
出现情况:
- 1、数据库表中 存放几千万条数据---- limit 0,100000 (分页查询 limit 0,20)
- 2、循环代码 一直new 新的对象存放在我们的集合中, 使用完后未清空,使得JVM不能回收(All complete empty collections,Or avoid large amounts of data)
- 3、使用的第三方软件中的BUG
- 4、启动参数内存值设定的过小,如最大堆内存10mb (Increase heap memory space)
- 5、代码中存在死循环或循环产生过多重复的对象实体;
内存溢出(Out Of Memory)应用系统中存在无法回收的内存或使用的内存过多,最终使得程序运行要用到的内存大于能提供的内存.
1.5、Check whether the heap usage is too large
Win操作系统打开任务管理系统 查看到那个进程占用cpu比较高即可
Linux 可使用arthas(阿尔萨斯) 排查cpu飙高的问题
# 1.下载阿尔萨斯
curl -O https://arthas.aliyun.com/arthas-boot.jar
2. java -jar arthas-boot.jar
3. Select the underlying process to run 输入1
4. thread -n 3
1.5、How to check the size of objects occupied by the heap
1、命令
1.Jps 查看当前系统中有哪些Java进程
需安装 jdk
jps # 查看当前系统中有哪些Java进程
jmap -heap 进程id # 查看堆内存占用情况
2、使用图形化界面 Jvisualvm 或者是 jconsole.exe
jdk 安装目录下
使用 Jvisualvm
View the maximum occupied memory
边栏推荐
- Analyses the mainstream across technology solutions
- Flutter TapGestureRecognizer 如何工作
- How does the Flutter TapGestureRecognizer work
- flex布局青蛙游戏通关攻略
- Flutter真机运行及模拟器运行
- After controlling the export file in MySQL, it becomes \N. Is there any solution?
- Homework 8.4 Interprocess Communication Pipes and Signals
- [BSidesCF 2019] Kookie
- 【学习笔记之菜Dog学C】动态内存管理之经典笔试题
- Judgment statement _switch and case
猜你喜欢
![[BJDCTF2020] EasySearch](/img/60/464de3bcdda876171b9f61ad31bff1.png)
[BJDCTF2020] EasySearch

Excel画图
![[Surveying] Quick Summary - Excerpt from Gaoshu Gang](/img/35/e5c5349b8d4ccf9203c432a9aaee7b.png)
[Surveying] Quick Summary - Excerpt from Gaoshu Gang

小程序_动态设置tabBar主题皮肤

The difference between span tag and p

雷克萨斯lm的安全性到底体现在哪里?一起来看看吧

请写出SparkSQL语句

How can Flutter parent and child components receive click events

【学生毕业设计】基于web学生信息管理系统网站的设计与实现(13个页面)

u-boot debugging and positioning means
随机推荐
Talk about 20 common problems in data governance
write the story about us
Mini Program_Dynamic setting of tabBar theme skin
University Physics---Particle Kinematics
for..in和for..of的区别
【转】什么是etcd
基于Web的商城后台管理系统的设计与实现
dedecms后台生成提示读取频道信息失败的解决方法
dedecms dream weaving tag tag does not support capital letters fix
Use IDEA to connect to TDengine server
The difference between span tag and p
使用二维码解决固定资产管理的难题
The first performance test practice, there are "100 million" a little nervous
Develop your own node package
【软考 系统架构设计师】软件架构设计③ 特定领域软件架构(DSSA)
8.04 Day35-----MVC三层架构
Flutter学习4-基本UI组件
phone call function
Day019 Method overriding and introduction of related classes
LAB 信号量实现细节