当前位置:网站首页>Memory leak
Memory leak
2022-06-28 18:42:00 【Novice Xiaowang】
What is memory leak
The reason for memory leakage is essentially that objects with a long life cycle refer to objects with a short life cycle , Resulting in a short life cycle The object cannot be GC Recycle in time , As a result, the occupied memory cannot be released , If the program runs for a long time, it may eventually lead to OOM out of memory .
Under what circumstances are memory leaks prone to occur ?
There are many reasons for memory leaks. Here are a few common situations .
One 、Handler Cause memory leaks .
We know ,Handler、Message、MessageQueue It's interconnected ,Handler By sending a message Message Interact with the main thread , If Handler Message sent Message Not yet dealt with , The Message And send it Handler The object will be MessageQueue Has been held , This could lead to Handler Cannot be recycled , This leads to memory leaks .
for instance :
AActivity There's a delay in the code 1 Second execution message Message, When the interface starts from AActivity Jump to BActivity when ,AActivity Automatic backstage , At this point, if the system resources are tight ( Or open the “ Don't keep the activity ” Options ),AActivity Will be finish. But here's the problem , hypothesis AActivity Of Handler object mHandler Is a non static anonymous inner class object , It automatically holds the outer class AActivity References to , Which leads to SecondActivity Cannot be recycled , Memory leak .
terms of settlement :
take Handler Declared as a static inner class , You don't hold external classes SecondActivity References to , Its lifecycle has nothing to do with external classes , If Handler It needs to context Words , External classes can be referenced by weak references , Or directly at Activity Of onDestroy Lieutenant general Handler Destruction of objects .
Two 、Bitmap There is no call recycle()
Bitmap When the object is confirmed not to be used , We should call recycle() Free memory , Then set to null, Otherwise, memory leaks can result .
terms of settlement :
stay Acrivity or Fragment In the call recycle().
3、 ... and 、 Memory leak caused by objects in the collection not being cleaned up
We often add references to some objects to the collection , But when we don't need to change the object , It doesn't clean up its references from the collection , In this way, the collection will become larger and larger . If this set is static, This will cause the resources in the collection to occupy memory all the time and cannot be cleaned up .
terms of settlement :
Set the collection resource empty when it is useless .
Four 、 Memory leak caused by resource object not being closed
Resource objects such as (Cursor,File Documents, etc. ) Often with some buffer , When we don't use it , They should be shut down in time , So that their buffers reclaim memory in time . Instead of waiting GC To deal with it .
5、 ... and 、 There is no memory leak caused by registration cancellation
such Android Memory leak ratio Java The memory leak is even more serious , Because some of the others Android The program may refer to the Android The object of the program ( E.g. registration mechanism ). Even if Android The program is over , But other applications still have access to Android A reference to an object of a program , It can also cause memory leaks .
resolvent :
1. Use ApplicationContext Instead of ActivityContext;
2. stay Activity perform onDestory when , Call de registration ;
Ways to detect memory leaks
LeakCanary Tools
The official address is as follows :https://github.com/square/leakcanary/LeakCanary Need to integrate... In project code . When a memory leak occurs ,LeakCanary A window will pop up to prompt and generate the corresponding heap storage information record .
Android Profiler analyzer
open Android Studio, Compile code , Run on a simulator or a real machine App, Then click on the icon below , Enter the following interface

This method can intuitively observe the memory fluctuation during the running of the program, and can clearly list all the objects causing memory consumption and memory allocation , It takes some experience to use ,
边栏推荐
- 亿信华辰:地产企业数字化转型想要把握时代机遇
- CANN媒体数据处理V2,JPEGD接口介绍
- 从理论到实践增强STEAM和工程教育
- Analysis of response parsing process of SAP ui5 batch request
- Unity about oculus quest2 developing 002-ui interaction based on XR interaction Toolkit
- 原生实现.NET 5.0+ 自定义日志
- render函数解析
- Record an emotet Trojan horse handling case
- 运筹学note
- 1 invalid import format(s) Postman Collection Format v1 is no longer supported and can not be import
猜你喜欢

进阶高级-业务事务设计 开发入门

Steam education to break the barriers between disciplines

Enhancing steam and engineering education from theory to practice

OneFlow源码解析:算子签名的自动推断

Lumiprobe ProteOrange 蛋白质凝胶染料说明书

Lumiprobe 蛋白质标记研究方案

tensorboard 使用总结

Advanced technology management - how managers communicate performance and control risks

Unity about oculus quest2 basic development based on XR interaction toolkit 003- capture function - making a VR bowling game

About Significance Tests
随机推荐
Analysis of response parsing process of SAP ui5 batch request
leetcode 1423. Maximum Points You Can Obtain from Cards(从牌中能得到的最大点数和)
Introduction to apifox
微软独家付费功能,也被完美解锁了
被315点名的流氓下载器,又回来了…
Go 降序排序 取 Top N
浦发银行软件测试面试真题(小编面试亲测)
Shell 未知汇总1
C语言指针的一些易错点
推荐两款超高质量的壁纸软件
IDM certification process log embedding point description
模块化操作
idea其他分支合并到dev分支
使用.NetCore自带的后台作业,出入队简单模拟生产者消费者处理请求响应的数据
618活动季——百数低代码平台特享折扣来临
新工作第一天
C语言文件操作
Record an emotet Trojan horse handling case
声网 VQA:将实时互动中未知的视频画质用户主观体验变可知
Common DOS commands