当前位置:网站首页>如何写出不容易内存溢出的代码
如何写出不容易内存溢出的代码
2022-06-10 11:15:00 【打不死的喜羊羊】
为什么会内存溢出?
原因是:不断new对象(强引用对象是不会被JVM回收的,就算内存溢出了也不会回收)
怎么写出不容易内存溢出的代码?
void functionA(){
//少用new对象创建强引用对象
for(){
//这里可以创建软引用对象,如果真的内存溢出,软引用对象也会被回收
functionB();
}
}
void functionB(){
//functionB里最好创建弱引用对象,这样,当作用域被JVM回收的时候,弱引用对象也会被回收
}
总结
1、for循环调用方法,方法的对象最好是弱引用对象(new weakreference)
2、主方法体的对象,如果可以,就是用软引用对象(new softreference)
边栏推荐
- 87. (leaflet house) leaflet military plotting - straight arrow modification
- Nucleic acid detection robot
- 解析:稳定币不是「稳定的币」 其本质是一种产品
- 核酸检测机器人
- Flutter socketio example
- 为你推荐一款高效的IO组件——okio
- What about the interface that needs to be logged in first when using the apipost test interface (based on cookies)?
- 【黄啊码】我用这个方式清理了谷歌浏览器的控制台
- SaaS management system for digital commerce cloud business service industry: to achieve efficient business collaboration and help enterprises improve their digital transformation
- Uniapp implements authorized login
猜你喜欢

上企评家,看各央企、上市公司企业成长性评价

数商云商业服务业SaaS管理系统:实现高效业务协作,助力企业完善数字化转型之路

软件测试质量与保证大题

Transfer of 30% equity of Zhuhai Gaoyuan Electric Energy Technology Co., Ltd., shared by tamigou

为你推荐一款高效的IO组件——okio

数据在内存中的存储方式

87.(leaflet之家)leaflet军事标绘-直线箭头修改

更耐用的游戏真无线耳机,电池超大续航持久,英雄G1上手

Mit6.824-lab2d-2022 (detailed explanation of log compression)

It's more convenient to keep warm water. It's a small artifact for daily milk brewing. It's a colorful jingle wireless portable milk mixer
随机推荐
Meetup回顾|DevOps&MLOps如何在企业中解决机器学习困境?
More durable game real wireless headset with large battery and long endurance. Hero G1 can use it
After an error occurs in the source observable, it will be directly entered into the catchrror operator instead of the sequence in the pipe before entering the catchrror
PV operation daily question - orange apple question (advanced version)
切换npm的淘宝镜像
redis 面经详解
基于昇腾AI异构计算架构CANN的通用目标检测与识别一站式方案初体验
剑指位运算
source observable 发生错误之后会直接进入到catchError operator 而不是按照pipe里面的顺序处理之后再进入catchError
What is the difference between NoClassDefFoundError and classnotfoundexception
【BUUCTF】[Zer0pts2020]Can you guess it?
string类及学习使用文档
在线文档协作工具,是提高工作效率的第一步
JS implements tree data operation through recursion
软件测试基础
PV operation daily question - orange apple question (advanced version)
保温热水更方便,日常泡奶的小神器,七彩叮当无线便携式调奶器上手
从 0 开始构建研发高效能全栈式团队
【黄啊码】PHP7为什么比PHP5快两倍?
Niuke Mianjing 02