当前位置:网站首页>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 ,
边栏推荐
- Go 降序排序 取 Top N
- 基于固态激光雷达辅助的十六线机械雷达和单目相机的外参标定方法
- About Covariance and Correlation(协方差和相关)
- 数字化转型的1个目标,3大领域,6大因素和9个环节
- select/poll/epoll
- 向上转型和向下转型
- About the solution of "modulenotfounderror: no module named 'flask.\u compat'"
- 如何使用 SAP CDS view 中的 currency conversion 功能
- 堆的概念和代码实现
- leetcode 1423. Maximum Points You Can Obtain from Cards(从牌中能得到的最大点数和)
猜你喜欢

数据资产为王,如何解析企业数字化转型与数据资产管理的关系?

About Significance Tests

HackTheBox-baby CachedView

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

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

Steam education to break the barriers between disciplines

业务层修改--根据现有框架的反推修改

技术管理进阶——管理者如何做绩效沟通及把控风险

Tensorboard Usage Summary

MindSpore系列一加载图像分类数据集
随机推荐
运筹学note
闭包的理解
Chapter 2 processing files, cameras and GUI Cameo applications
Object tracking using tracker in opencv
⼤⼚⾯试真题集合
解析机器人主持教学的实践发展
FFmpeg学习总结
Go 降序排序 取 Top N
Shell 未知汇总1
NFT流动性协议的安全困局—NFT借贷协议XCarnival被黑事件分析
几行代码就能实现复杂的 Excel 导入导出,这个工具类真心强大!
Shanghai Pudong Development Bank Software Test interview real question
POI Excel转换工具
图形系统——2.view绘制
postgresql数据库docker
Cann media data processing V2, jpegd interface introduction
亿信华辰:地产企业数字化转型想要把握时代机遇
An in-depth analysis of the election mechanism in kubernetes
实时Transformer:美团在单图像深度估计上的研究
声网 VQA:将实时互动中未知的视频画质用户主观体验变可知