当前位置:网站首页>2.6.2 memory leakage
2.6.2 memory leakage
2022-07-19 05:18:00 【Cuddle】
java Memory leak Basics
1.Java Memory allocation strategy
(1) Static storage area ( Method area ): Store static data 、 Global variables, etc , Allocated at compile time , Exist during operation .
(2) The stack area : Method execution , Local variables are created here , Automatically release after the method ends . High efficiency, limited capacity .
(3) Heap area : Dynamic memory allocation . Deposit new Out of the objects and arrays , Recycle by garbage collector .
2.java How to manage memory
We allocate memory ,gc Reclaiming memory .
Reachable with vertex 、 Unreachable .jpg
3.java Memory leak in
Useless object ( No longer used ) The memory of continuous comrades in arms or useless objects cannot be released in time , The resulting waste of memory space becomes a memory leak .
Android Memory leak
1. Single case
The singleton static feature makes its life cycle and app As long as , If an object is no longer used , The singleton holds its reference .
e.g. Do not pass in a singleton Activity Of Context, Instead, use context.getApplicationContext
2. Anonymous inner class
Instances created by non static inner classes are not recycled , Hold the reference of this class to the external class .
3.Handler
Change to static inner class , And let it hold weak references to external classes .
4. Avoid using static Member variables
Consider using life cycle management
5. Memory leak due to resource not closed
Activity Close or unregister resources on destruction
6.AsyncTask Memory leak
FAQ
Memory leak reference chain
The main thread / Sub thread -> ThreadLocal -> Looper -> MessageQueue -> Message -> Handler
LeakCanary principle
monitor Activity Life cycle of ,( adopt Application.registerActivityLifecycleCallbacks() )
stay onDestroy() When , Wait a few seconds , Then determine whether the object is recycled ( adopt ReferenceQueue+WeakReference). If not recycled , register IdleHandler, At leisure GC, And then judge whether the object is recycled .
After determining that there is a memory leak , Generate .hprof file ( call heapDumper.dumpHeap()).
stay IntentService Dealing with subsequent logic 、 Sending notice .
q: Why not use virtual references + Reference queue
a: Objects stored by virtual references are almost directly recycled , As a result, a certain amount of time cannot be stored ,LeakCanary For monitoring and recording , The required data and records should be stored for a certain period of time at least , Because in the actual use scenario, it is generally used if the reference is enough
边栏推荐
- Cesium bind mouse events and remove mouse events
- STL容器——set集合的应用
- 小程序editor富文本编辑使用及rich-text解析富文本
- Using JS to realize the second level menu of anjuke and the full version (demonstration of precautions and problem points)
- 学习C语言第三天
- STL container - basic operation of vector
- 【Es6】快速实现用户信息打印至页面中
- 微信小程序云开发使用方法-1
- Markdown notes and related shortcut keys of typora
- 学习C语言第8天
猜你喜欢
随机推荐
无重复字符的最长字串
es6新增-let和const (var的缺点&let及const)
Wechat applet status bar
获取URL参数的两种方法及location对象的各项获取方式
小程序editor富文本编辑使用及rich-text解析富文本
实习项目1-个性化主页配置
【C语言_复习_学习第二课】什么是进制?进制之间应该如何转换
热更新及其原理
【C语言—零基础第十三课】字符串的奥秘
<script>标签内容详解
网络命令:网卡信息,netstat,arp
BUUCTF 杂项——二维码
接上期内容:轮播图剩余两种方法
【C语言—零基础第十四课】变量的作用域与存储类
PAT乙级1002:写出这个数
Wechat applet obtains the week, morning, noon and evening of month, year and day
微信小程序云开发使用方法-1
获取数组中对象内部的数值最大与最小值多功能版及点名系统完整版并展示效果
基于PaddleOCR解决文本检测训练模型与inference模型预测效果不一致的问题
Use (offset, page) in JS to achieve login effect









