Android Memory Leaks OR Different Ways to Leak

本文详细介绍了作者在解决AnySoftKeyboard应用中出现的内存泄漏问题的过程,包括六种常见内存泄漏的原因及解决方案。

Android Memory Leaks OR Different Ways to Leak

转自:http://evendanan.net/2013/02/Android-Memory-Leaks-OR-Different-Ways-to-Leak/


 few weeks ago I released version 85 of AnySoftKeyboard and soon after, crash reports started to hit my email box. It usually happens - mostly because I forget to test a feature, or just because the specific device decided it is time to crash (it happens, really! The floor is crooked). But this time, all the crashes were of the same nature, and were many, about 5-10 a day.
The crashes were due to OutOfMemoryError (a.k.a, OOM), which usually means a memory leak in Android (why? Unlike a desktop, an Android device has very little heap to play with and leaking will fill that little space quickly), I started to look for them.

TL;DR: If I was able to recreate this crash on my devices, I would have find the cause immediately, and just fix the 6th cause in this list. But, I wasn't able to reproduce it (why? later on that), and it took me some time to understand that the reporting users want me to fix it, and are willing to help!

Cause 1: Context Leak

The most common leak in Android is the  Context  leak (here is a great  explanation ). Although all the examples are talking about Activity leaking, a Service (which AnySoftKeyboard is) has the same problems as Activity in that regard.

Solution

It's a no-brainer, but tedious: where ever I used a Context object, I made sure that instance is the Application's Context - getApplicationContext() .

Cause 2: The Handler Leak OR the Inner Class

The  inner-class  (a.k.a.  nested classes ) in Java has an implicit property where it keeps a reference to its outer class, this is why you can call outer-class's functions in the inner-class scope. As long as the Handler is alive, so is the outer-class, and in my case - the whole Input Method Service, which in turn holds the entire tree!
One thing to note, once there are no messages in the Handler's loop, it will go out of reference in the Main Looper, and if there are no other references to it, well, it will be collected, with its implicit outer-class.  So what is the problem?  you may ask, and you are right. The problem is that some of the messages are delayed (or just still in queue), and as long as there is a message in the queue, the Handler will not be collected, so even if the OS will decide to kill AnySoftKeyboard (say, orientation change) and create a new one, the old  one is still in memory , and we have two full trees at the same time. Maybe not for long, but enough time to cause OOM on some devices.

Solution

Very simple: gone over all the inner-classes I had (including the Handlers) and made sure they are static classes, and if they required a reference to the the outer-class, I used  WeakReference :
  public class AnySoftKeyboard extends InputMethodService {
   
  private static final class KeyboardUIStateHanlder extends Handler {
  //This will NOT be counted as a reference!
  private final WeakReference<AnySoftKeyboard> mKeyboard;
   
  public KeyboardUIStateHanlder(AnySoftKeyboard keyboard) {
  mKeyboard = new WeakReference<AnySoftKeyboard>(keyboard);
  }
   
  @Override
  public void handleMessage(Message msg) {
  AnySoftKeyboard ask = mKeyboard.get();
  if (ask == null)//delayed posts and such may result in the reference gone
  return;
   
  switch (msg.what) {
  case MSG_UPDATE_SUGGESTIONS:
  ask.performUpdateSuggestions();
  break;
view raw InnerClassSnippet.java hosted with ❤ by  GitHub

In the code snippet above, I used a WeakReference field to keep a pointer to the main AnySoftKeyboard instance. By using this pattern I was able to call methods of AnySoftKeyboard, but also made sure I was not keeping AnySoftKeyboard from being garbage-collected if needed.
Do remember that if using this pattern it is required to check that the weak reference is still pointing to something (e.g., not null).

Cause 3: The Drawable Callback

Most of the Drawables I use are not attached to Views, but some are, and since I keep reference to the drawables objects, this was still a possible leak. Why? The drawable requires that its client (usually a View) will implement theDrawable.Callback interface, which is used by the Drawable to perform animation related tasks. So, if the View is removed from the window/activity, but Drawable is still referenced, then the View is still referenced too, and will not be garbage-collected.

Solution

I explicitly unbind the drawables when the keyboard's View is no longer needed:
  private static void unbindDrawable(Drawable d) {
  if (d != null)
  d.setCallback(null);
  }
view raw AnyKeyboardBaseView.java hosted with ❤ by  GitHub
This was, actually, not required in my case since the only reference to the Drawable was the View which is the Callback for the Drawable.
Commit  (look for changes in  AnyKeyboardBaseView.java ).

Cause 4: The External Context

This is special for AnySoftKeyboard, most apps do not reference to an external package's Context, but since AnySoftKeyboard supports external packages (i.e., language packs, themes, etc.), I was keeping a reference to that external Context always: let's say you use three languages (not very rare or the users of AnySoftKeyboard), and want to use the  Ice Cream Sandwich theme , you'll end up with  four  external Context objects sitting around in AnySoftKeyboard heap. This is quite a waste, since most of the time I use the external Context only once or twice!

Solution

I moved the external from an explicit reference, to a WeakReference, and added the package's name to class's fields so I'll be able to create the context if needed.
Commit .

Cause 5: The Too Large Objects

Key background, Shift, Control, Enter, Space, Tab, Cancel, Globe, Microphone, Settings, Arrows  and more. All these are Drawables which were loaded into the keyboard View, and were kept in memory. About 15 of them, and some with various states (like the Enter key, which has a Normal, Search, Done, Go, etc states, each a full drawable). That's not a leak, but it is still a lot of memory for some devices - HTC Desire will not allow more than 32MB of heap, for example.

Solution

I created a  DrawableBuilder  class which have all the information required to get the drawable. This way, if the layout does not use a specific icon, it will not be loaded into memory.
Commit .

Cause 6: The Database Connection

After I fixed all the issues above, the app was still crashing on some devices. I had no idea where to look anymore, I was sure there was a leak, and it very elusive, maybe even vendor specific (although the crashes came from many vendors, and various OS versions), so I added to the  UncaughtExceptionHandler  a code that check of the thrown Exception type, and if it is OutOfMemoryError, I asked the framework do create a  memory dump :
  File extFolder = Environment.getExternalStorageDirectory();
  File target = new File(extFolder, "ask_mem_dump.hprof");
  Debug.dumpHprofData(target.getAbsolutePath());
view raw AndroidDumpHprof.java hosted with ❤ by  GitHub
I found the leak easily using  Eclipse Memory Analyzer (a.k.a, MAT) : the leak was a Database Connection Transport, and a huge one, each such transport used 0.5MB (for that user - since he had a large user-dictionary), and it leaked every time the user switched language! It was leaking due to a race-condition.

The Race Condition

When a dictionary is created, it loads its words list using an  AsyncTask  (so it wont hold up the UI thread), but when the list is long and the device is slow, the dictionary's  close  method (which is called when the language changes) may be called before the loading is done, hence not closing anything!

So, if it happens always why haven't I recreated it on my devices? Some flavors (Samsung devices and CyanogenMod) could handle that release automatically (there is a finalizer in Java, you know), and they did. Some vendors did not. I had a Samsung, a Motorola and a device running CyanogenMod. Moreover, my devices are fast just enough, and used a small words list. Bummer, ah? Ya..

Solution

Just closed the database connection: by ensuring the related functions use monitors.
Commit  and  commit .

The End


内容概要:本文系统研究了电力系统短期负荷预测问题,提出并实现了基于极限学习机(ELM)及其智能优化改进模型的预测方法。研究涵盖标准ELM、白鲸优化算法(BWO)优化ELM和鹭鹰优化算法(IBOA)优化ELM三种模型,重点通过智能优化算法对ELM的输入权重与偏置参数进行全局寻优,有效克服了传统ELM因参数随机初始化导致的不稳定性和泛化能力不足的问题。文章完整呈现了从数据预处理、特征选择、模型构建、参数优化到预测结果对比分析的全流程,利用Matlab编程实现各模型的仿真验证,显著提升了预测精度与模型鲁棒性,为电力系统调度决策提供了可靠的技术支撑。; 适合人群:具备电力系统基础知识、时间序列预测理论及Matlab编程能力的高校研究生、科研机构研究人员以及电力公司从事负荷预测、电网调度与规划工作的技术人员。; 使用场景及目标:①应用于实际电力系统短期负荷预测业务中,提升电网运行调度的精细化与智能化水平;②作为智能优化算法与神经网络融合的经典案例,服务于学术论文撰写、科研项目申报及算法性能对比研究;③应对新能源大规模接入背景下负荷波动加剧的挑战,为构建高精度、强鲁棒性的现代负荷预测体系提供解决方案。; 阅读建议:建议读者结合所提供的Matlab代码进行动手实践,深入理解ELM网络结构与优化算法的集成机制,重点对比分析不同优化策略在收敛速度、预测误差(如MAE、RMSE、MAPE)等方面的性能差异,进而掌握智能优化技术在提升预测模型性能方面的关键作用。
内容概要:本文研究了基于Benders分解与输电网运营商(TSO)和配电网运营商(DSO)协调机制的不确定环境下输配电网双层优化模型,旨在提升高比例可再生能源接入背景下电网系统的协调性与鲁棒性。模型上层以系统整体经济性为目标进行优化调度,下层采用Benders分解实现TSO与DSO之间的信息交互与协同决策,通过引入割平面迭代机制保障求解的收敛性与全局最优性。研究充分考虑新能源出力与负荷需求的不确定性,构建了具有强适应性的双层优化框架,并基于Matlab完成了模型的编程实现与仿真验证,有效解决了多主体、多层级、多不确定性因素耦合下的电力系统优化调度难题。; 适合人群:具备电力系统分析、运筹学与优化理论基础,熟悉Matlab编程环境,从事智能电网、能源互联网、分布式能源集成、电力市场等方向的研究生、科研人员及工程技术人员。; 使用场景及目标:①研究高渗透率可再生能源条件下输配电网协同优化调度策略;②掌握Benders分解在电力系统双层优化建模中的应用方法与实现技巧;③构建TSO-DSO多主体协调机制,实现跨层级电网资源的高效互动与决策解耦;④提升对不确定性建模、分解算法设计及大规模优化问题求解能力。; 阅读建议:建议读者结合Matlab代码逐模块剖析模型构建流程,重点理解Benders割的生成逻辑、主从问题的信息传递机制及收敛判据设定,推荐在标准IEEE测试系统上复现实验以深入掌握模型特性与算法性能。
内容概要:本文提出了一种基于断线解环思想的配电网辐射状拓扑约束建模方法,旨在通过Matlab代码实现确保配电网在重构或运行过程中始终保持辐射状结构,防止环路形成,从而提升系统的安全性与稳定性。该方法通过系统性地识别网络中的潜在环路,并依据拓扑规则自动切断特定支路,有效处理配电网在优化调度、故障恢复及网络重构中的拓扑约束问题。文中详细阐述了算法的核心逻辑、数学模型构建过程、实现步骤及关键判据,并结合标准测试系统进行了仿真验证,充分证明了该方法在复杂配电网络中的有效性与实用性,尤其适用于含分布式电源接入的智能配电网场景。; 适合人群:具备一定电力系统分析基础和Matlab编程能力的高校研究生、科研人员,以及从事配电网自动化、智能电网优化、电力系统运行与控制等相关领域的工程技术人员。; 使用场景及目标:①解决配电网重构过程中的辐射状拓扑可行性验证与约束建模问题;②支撑含高比例分布式电源的配电网在故障恢复、动态重构中的安全运行分析;③为相关高水平EI期刊论文的模型复现、算法验证及科研项目申报提供可靠的代码实现与技术参考。; 阅读建议:建议读者结合Matlab代码与电力网络拓扑理论进行同步学习,重点理解断线解环的图论基础、环路搜索算法及支路断开逻辑的实现机制,并尝试在不同规模的测试系统(如IEEE 33节点系统)上进行仿真调试,以深入掌握该方法的应用技巧与优化潜力。
内容概要:本文围绕基于元模型优化算法的主从博弈多虚拟电厂动态定价与能量管理展开研究,提出了一种结合主从博弈理论与元模型优化方法的协同决策框架,通过Matlab代码实现,旨在解决高比例可再生能源接入背景下多虚拟电厂在复杂电力市场环境中的协调优化难题。研究构建了上层领导者(如主网或运营商)与下层跟随者(各虚拟电厂)之间的非对称互动模型,实现了动态电价制定与多主体能量调度的联合优化,有效提升了系统整体运行效率、经济收益与市场公平性。文中详细阐述了模型构建过程、算法设计思路及仿真验证方案,重点突出了元模型在降低计算复杂度、处理不确定性因素以及加速求解收敛方面的优势,具有较强的工程复现价值与理论参考意义。; 适合人群:具备一定电力系统运行、博弈论基础、优化建模能力及Matlab编程技能的研究生、科研人员,以及从事虚拟电厂运营、能源互联网规划、智能电网调度等相关领域的技术人员。; 使用场景及目标:①用于多主体能源系统中市场机制设计与竞价策略分析;②支撑含分布式能源的主动配电网协同优化调度研究;③为虚拟电厂参与电力市场的动态定价、需求响应与能量管理提供仿真验证平台与解决方案参考。; 阅读建议:建议读者结合Matlab代码逐模块理解算法实现流程,重点关注主从博弈架构的数学建模方式与元模型近似优化技巧的应用细节,同时可通过调整市场参数、负荷场景或可再生能源出力数据进行拓展性实验,以深化对模型鲁棒性与泛化能力的理解。
内容概要:本文围绕列车-轨道-桥梁耦合系统开展动力学交互仿真研究,基于Matlab平台构建多体动力学数值模型,综合考虑列车移动荷载、轨道结构特性与桥梁动态响应之间的耦合作用,实现对列车通过桥梁过程中振动传递规律、结构受力特性和动力响应行为的精确模拟。研究涵盖系统建模、运动方程求解、关键参数设定及仿真结果分析全过程,提供完整的Matlab代码实现方案,有助于深入理解轨道交通基础设施在运营条件下的动力性能,为桥梁结构安全性评估、轨道平顺性优化及减振设计提供理论支持和技术手段。; 适合人群:具备一定结构动力学、振动力学基础知识及Matlab编程能力的研究生、高校教师、科研机构研究人员以及从事铁路与桥梁工程设计、运维的工程技术人才。; 使用场景及目标:①用于高速铁路桥梁在列车荷载作用下的动力响应仿真与安全评估;②支撑轨道-桥梁系统减振降噪设计与结构优化;③作为高等教学与科研中的典型案例,辅助讲授多体系统动力学建模与数值仿真方法; 阅读建议:建议读者结合结构动力学相关理论教材,逐步运行并调试所提供的Matlab代码,重点关注质量-刚度-阻尼矩阵的构建、轮轨接触关系处理、时间积分算法实现等核心模块,深入理解仿真结果的物理含义及其工程应用价值。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值