当前位置:网站首页>内存抖动
内存抖动
2022-06-28 18:10:00 【菜鸟xiaowang】
一.内存抖动
1.内存抖动:指在短时间内有大量的对象被创建或者被回收的现象。
2.内存抖动产生原因:主要是频繁(很重要)在循环里创建对象(导致大量对象在短时间内被创建,由于新对象是要占用内存空间的而且是频繁,如果一次或者两次在循环里创建对象对内存影响不大,不会造成严重内存抖动这样可以接受也不可避免,频繁的话就很内存抖动很严重),内存抖动的影响是如果抖动很频繁,会导致垃圾回收机制频繁运行(短时间内产生大量对象,需要大量内存,而且还是频繁抖动,就可能会需要回收内存以用于产生对象,垃圾回收机制就自然会频繁运行了)。
3.内存抖动影响:频繁内存抖动会导致垃圾回收频繁运行,造成系统卡顿。
解决方案
那么常见会出现内存抖动的地方在哪里?
1.Android的onDraw, onMeasure方法。
这里, 官方不建议在这创建对象,为什么?知道了吧,这里频繁会被底层调用,(可以去了解下安卓刷新机制),如果大量创建对象,频繁回收,就很容易内存抖动。如果是bitmap,他的内存是在native分配的,处理需要经过内核,使得等待时间更加长久。
2.处理io的地方
读写文件,内存流时候创建大量的byte[]对象会出现。
3.生产消费者模型的组件,这些频繁会产生大量的对象,比如日志,埋点组件等
4.复杂类型的json解析。
比如Gson。默认情况下,是通过反射生成新的实例。通过适配器使用对象池可以解决
推荐解决方案:
通过复用对象使用解决内存抖动,
比如享元设计模式,对象池方案。但对象池不能减少内存,需要对内存做限制,而且每种对象场景不一样,可能需要筛选。需要lru算法的数据结构等。可参考handler,glide对象池原理。
对象池原理很简单,就是创建对象使用完毕后存到内存里面,比如存到链表里面,下次使用对象的时候,优先在链表里面查,如果有就拿出来时候,如果没有就创建,这样就能避免大量对象创建
边栏推荐
- Is it reliable to open an account for the shares of Oriental Wealth software? Where is safe to open an account
- Go 降序排序 取 Top N
- IDM certification process log embedding point description
- Openfire用户以及群组关系移植
- halcon知识:矩阵专题【01】
- Seata implementation of sharing JDBC distributed transaction
- [translation] list of Clickhouse 22.4 and 22.5 core features
- Chapter 2 processing files, cameras and GUI Cameo applications
- MySQL十种锁,一篇文章带你全解析
- select/poll/epoll
猜你喜欢

Small program graduation project based on wechat subscription water supply mall small program graduation project opening report function reference

China gaobang brand story: standing guard for safety, gaobang pays attention to

Go, begin, end, for, after, instead of

Applet graduation design based on wechat conference room reservation applet graduation design opening report function reference

Small program graduation project based on wechat mobile mall small program graduation project opening report function reference

Kubernetes visual interface dashboard

Dnslog injection

杂记:数据库go,begin,end,for,after,instead of

Le test de reconnaissance faciale comporte - t - il des préoccupations en matière de protection de la vie privée? Une entreprise étrangère a été arrêtée en cas d'urgence

堆的概念和代码实现
随机推荐
国内怎么就做不出 IDEA 那样的产品?
About the solution of "modulenotfounderror: no module named 'flask.\u compat'"
Applet graduation project is based on wechat property maintenance application property applet graduation project opening report function reference
Finally quit, full-time UE
Small program graduation project based on wechat milk tea takeout mall small program graduation project opening report function reference
EasyCVR新建用户后,视频调阅页面不能点击的问题修复
Introduction to kubernetes resource object and common commands
golang json 序列化、反序列化 字符串反序列化成 map[string]interface{}
电子商务盛行,怎么提高商店转换率?
Mycat+分库分表
CORBA 架构体系指南(通用对象请求代理体系架构)
MySQL十种锁,一篇文章带你全解析
BioVendor游离轻链(κ和λ)Elisa 试剂盒检测步骤
CANN媒体数据处理V2,JPEGD接口介绍
Graphic system - 2 View drawing
Le test de reconnaissance faciale comporte - t - il des préoccupations en matière de protection de la vie privée? Une entreprise étrangère a été arrêtée en cas d'urgence
Lumiprobe ProteOrange 蛋白质凝胶染料说明书
Shanghai Pudong Development Bank Software Test interview real question
基于固态激光雷达辅助的十六线机械雷达和单目相机的外参标定方法
HTNL简介