当前位置:网站首页>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
边栏推荐
- PAT乙级1017: A除以B
- Two JS methods of rolling wheel loading and modal box dragging
- Email (including attachments, Netease, QQ)
- Class object automatic injection attribute operation tool
- Es6 真实案例解构(多维数组对象)全新案例:
- es6新增-Symbol数据类型
- 学习C语言第三天
- Applet cloud development upload pictures to cloud storage
- 学习C语言的第五天
- ArcMap 创建常量栅格并镶嵌至新栅格
猜你喜欢

【AI】利用简单神经网络做动作识别——基于coco关键点

Addition and removal of cesium geojson data

Ucharts chart, pie chart, bar chart and line chart are used in uniapp

Internship project 2 - Homepage configuration - my data module

ArcMap 创建常量栅格并镶嵌至新栅格

uniapp中使用ucharts图表,饼状图,柱状图,折线图

IText modify PDF Text

BUUCTF 杂项——二维码

小程序云开发表单提交并在页面中获取数据

Bi design: distributed high concurrency epidemic prevention health management system based on vue+socket+redis
随机推荐
PAT乙级1017: A除以B
循环赛制日程表问题
运维安全要了解的二三事
es6新增-数组/对象的解构赋值
微信小程序 学习笔记
STL容器——vector的基本操作
MySQL optimization
学习C语言的第6天
【Es6】快速实现用户信息打印至页面中
Interface parameters return encapsulated class result
Submit the uniapp form (input, radio, picker) to get the parameter value
Addition and removal of cesium geojson data
es6新增-let和const (var的缺点&let及const)
遍历的方法总结
Ucharts chart, pie chart, bar chart and line chart are used in uniapp
学习C语言的第四天
轮播图的两种方法及自动轮播
Applet editor rich text editing and rich text parsing
Pat class B 1017: a divided by B
C语言练习2