Avoiding memory leaks

74LS161/48 交通灯控制系统:30-5-20-5 时序逻辑电路 Multisim 仿真验证 本文详细解析了基于74LS161/48芯片的交通灯控制系统设计,通过Multisim仿真验证30-5-20-5时序逻辑电路。从系统架构搭建、状态机实现到波形分析,提供全流程操作指南与典型问题排查方法,助力数字电路课程设计高效完成。 阅读详情

Avoiding memory leaks

Android applications are, at least on the T-Mobile G1, limited to 16 MB of heap. It's both a lot of memory for a phone and yet very little for what some developers want to achieve. Even if you do not plan on using all of this memory, you should use as little as possible to let other applications run without getting them killed. The more applications Android can keep in memory, the faster it will be for the user to switch between his apps. As part of my job, I ran into memory leaks issues in Android applications and they are most of the time due to the same mistake: keeping a long-lived reference to a Context .

On Android, a Context is used for many operations but mostly to load and access resources. This is why all the widgets receive a Context parameter in their constructor. In a regular Android application, you usually have two kinds of Context , Activity and Application . It's usually the first one that the developer passes to classes and methods that need a Context :

This means that views have a reference to the entire activity and therefore to anything your activity is holding onto; usually the entire View hierarchy and all its resources. Therefore, if you leak the Context ("leak" meaning you keep a reference to it thus preventing the GC from collecting it), you leak a lot of memory. Leaking an entire activity can be really easy if you're not careful.

When the screen orientation changes the system will, by default, destroy the current activity and create a new one while preserving its state. In doing so, Android will reload the application's UI from the resources. Now imagine you wrote an application with a large bitmap that you don't want to load on every rotation. The easiest way to keep it around and not having to reload it on every rotation is to keep in a static field:

This code is very fast and also very wrong; it leaks the first activity created upon the first screen orientation change. When a Drawable is attached to a view, the view is set as a callback on the drawable. In the code snippet above, this means the drawable has a reference to the TextView which itself has a reference to the activity (the Context ) which in turns has references to pretty much anything (depending on your code.)

This example is one of the simplest cases of leaking the Context and you can see how we worked around it in the Home screen's source code (look for the unbindDrawables() method) by setting the stored drawables' callbacks to null when the activity is destroyed. Interestingly enough, there are cases where you can create a chain of leaked contexts, and they are bad. They make you run out of memory rather quickly.

There are two easy ways to avoid context-related memory leaks. The most obvious one is to avoid escaping the context outside of its own scope. The example above showed the case of a static reference but inner classes and their implicit reference to the outer class can be equally dangerous. The second solution is to use the Application context. This context will live as long as your application is alive and does not depend on the activities life cycle. If you plan on keeping long-lived objects that need a context, remember the application object. You can obtain it easily by calling Context.getApplicationContext() or Activity.getApplication() .

In summary, to avoid context-related memory leaks, remember the following:

  • Do not keep long-lived references to a context-activity (a reference to an activity should have the same life cycle as the activity itself)
  • Try using the context-application instead of a context-activity
  • Avoid non-static inner classes in an activity if you don't control their life cycle, use a static inner class and make a weak reference to the activity inside. The solution to this issue is to use a static inner class with a WeakReference to the outer class, as done in ViewRoot and its W inner class for instance
  • A garbage collector is not an insurance against memory leaks
EDSR-PyTorch实战:从模型下载到超分辨率图像生成 本文详细介绍了如何使用PyTorch实现EDSR模型进行超分辨率图像生成,包括环境配置、模型下载、参数调优及常见问题解决方案。EDSR作为超分辨率重建的利器,通过深度残差网络显著提升图像质量,特别适合老照片修复等场景。文章还提供了视频处理和自定义训练的进阶技巧,帮助开发者快速掌握这一技术。 阅读详情

相关推荐

Libero SoC v2021.1离线安装与IP核配置全攻略

本文详细介绍了Libero SoC v2021.1在离线环境下的完整安装与配置流程。核心内容包括如何选择正确的离线安装包、获取并配置许可证、安装与挂载MegaVault IP库,以及将ModelSim仿真器切换至免费SE模式的关键步骤。文章旨在帮助内网开发者避开常见陷阱,顺利完成FPGA开发环境的搭建。

weixin_29279101的博客 420

(翻译)avoiding memory leaks

原文来自Android SDK文档中的 docs/resources/articles/avoiding-memory-leaks.html   android应用,堆内存大小限制为16MB(至少在T-Mobile G1上是这样)。 对手机而言, 这已经是相当大一部分内存,但对某些开发者而言16MB太少了。 就算不准备使用全部的16MB内存, 至少也应用使用尽可能少的内存,从而避免其他应用因...

chen_410063005的专栏 202

使用fairseq从头开始训练一个中英神经机器翻译模型

前言 由于毕设是做神经机器翻译相关,所以先尝试一下神经机器翻译的整个流程是非常有必要的。故将在news-commentary-v15语料上训练有监督中英NMT模型的整个流程,包括工具和数据的准备、数据的预处理、训练及解码等过程,以及过程中遇到的问题和解决方案记录在此,以供后期回顾,也希望能够给予别人一些帮助。附本人博客 1 相关工具及目录结构 1.1 相关工具 除jieba是使用pip install安装外,其他几个工具都是建议直接克隆库到自己的用户目录中,方便使用其脚本(moses/subword-nmt

qq_42734797的博客 2万+

Avoiding Memory Leaks

为什么80%的码农都做不了架构师?>>> ...

weixin_34205076的博客 144

无人机避障——卡内基梅隆大学(CMU)CERLAB 无人机自主框架复现

CMUCERLAB无人机自主框架是一个模块化的无人机自主导航系统,包含感知、测绘、规划和控制等组件。安装过程中遇到的主要问题包括:(1)cv_bridge路径未正确识别,需手动指定路径;(2)OpenCV版本冲突(4.2与4.6),导致动态导航节点崩溃。解决方案包括:完全卸载OpenCV4.2,重新编译安装OpenCV4.6,并修改相关CMakeLists文件指定版本。最终成功运行仿真环境,实现无人机在Gazebo和RViz中的自主避障导航功能。

weixin_45390670的博客 1650

Android音频学习(一)——音频基础

在系统的学习Android音频之前,先了解音频的基础知识。

miemie2014的博客 1487

Android articles 翻译之一:Avoiding Memory Leaks

Avoiding Memory LeaksAndroid applications are, at least on the T-Mobile G1, limited to 16 MB of heap. It's both a lot of memory for a phone and yet very little for what some developers want to achieve. Even if you do not plan on using all of this memory

康康兔的牛棚 959

Microduck机器鸭开源AI项目:本地部署与模型微调实战指南

开源AI模型的本地化部署与定制化训练正成为开发者关注的技术趋势。与依赖云端API的在线服务不同,本地部署允许用户将模型权重下载至自有GPU环境,通过命令行或WebUI完成推理、批量任务处理及API集成,在数据隐私、离线使用和接口稳定性方面具备天然优势。模型微调则基于预训练权重,利用领域数据训练特定任务,从而提升生成质量与业务适配度。这一技术路径广泛应用于内容生成、智能对话、自动标注等场景,尤其适合消费级显卡开发者、数据敏感型团队及二次开发研究者。Microduck(机器鸭)作为近期预售火爆的开源项目,围绕“

weixin_29082143的博客 431

Avoiding memory leaks ---- Context Leak 篇

原文地址:http://android-developers.blogspot.tw/2009/01/avoiding-memory-leaks.html Android applications are, at least on the T-Mobile G1, limited to 16 MB of heap. It's both a lot of memory for a

AnnaLoad的专栏 1234

ADS8681数据手册没讲透的细节:SPI时序、量程切换与电压换算的避坑指南

本文深入解析ADS8681数据手册未提及的SPI时序优化、量程切换陷阱与电压校准技巧。针对树莓派5与ADS8681的SPI通信问题,提供CPOL/CPHA配置建议;详解量程切换的位操作玄机与寄存器配置流程;并提出三步校准法应对非理想供电环境,帮助开发者提升ADC测量精度。

weixin_29178069的博客 532

PHP证书图片生成

$realname = "姓名"; $schoolname = "学校"; $idcard = "身份证号"; $image = imagecreatefrompng('certification.png'); // 证书模版图片文件的路径 $red = imagecolorallocate($image,00,00,00); // 字体颜色

4041

Avoiding memory leaks in POSIX thread programming

Avoiding memory leaks in POSIX thread programming

wyhang0的专栏 425

【面试系列】数据工程师高频面试题及详细解答

本文针对数据工程师岗位,提供了详尽的面试题库和解答,分为初级、中级和高级三个层次。初级面试题主要涉及基本的数据处理和ETL工具的使用,如SQL查询、数据清洗和基本的数据建模。中级面试题重点考察数据管道设计、数据库优化和大数据平台的应用。高级面试题则深入探讨高级数据处理技术、分布式系统架构和数据湖的实现。通过这些问题和解答,候选人可以全面了解数据工程师岗位所需的技能,帮助其更好地准备面试。常考知识点总结部分则系统归纳了数据工程师岗位的关键技能领域。

在路上的专栏 2377

Visual Basic: Avoiding Memory Leaks in Objects

Avoiding Memory Leaks in Objects http://www.vbi.org/Items/article.asp?id=106 In this article, I will discuss the potential memory leak that can occur if object references aren't handled in a proper ...

诸葛建站 132

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

android avoiding-memory-leaks

为什么80%的码农都做不了架构师?>>> ...

weixin_34321977的博客 121

怀旧版电脑GBC模拟器

再现原版GBC掌机的界面,电脑运行连游戏界面都一样大小,很怀旧,很真实,(英文版)强烈推荐,很小的文件,很难才找到的。

Avoiding memory leaks in POSIX thread programming, 多线程避免内存泄漏

Avoiding memory leaks in POSIX thread programming, 多线程避免内存泄漏 默认创建的线程为joinable的,必须调用pthread_join()才可以释放所占的内存 创建分离线程detach, attr 线程函数运行结束,调用pthread_exit 其它线程调用pthread_cancle(...

weixin_33928467的博客 118

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

防止内存泄漏_了解和防止内存泄漏

防止内存泄漏 Delphi's support for object-oriented programming is rich and powerful. Classes and objects allow for modular code programming. Along with more modular and more complex components come more soph...

cumao2792的博客 602

An Autofac Lifetime Primer

Or, “Avoiding Memory Leaks in Managed Composition” Understanding lifetime can be pretty tough when you’re new to IoC. Even long-time users express vague fears and misgivings when it comes to t...

dijiaomeng0136的博客 309

【翻译】Android避免内存泄露(Activity的context 与Context.getApplicationContext)

原谅地址:http://android-developers.blogspot.com/2009/01/avoiding-memory-leaks.html,英文原文在翻译之后 Android 应用至少,在T-Mobile G1这个型号,就有16MB的堆内存。这个容量对于手机来说是很大了,但是对于有些开发者来说是少了些。为了全其他应用可以运行而不被系统杀掉,即使你没有打算使用完所有分配的容量,...

weixin_30340617的博客 126
上一篇: SVN常用命令学习笔记
下一篇: AT Command for QOS
CodeJoker
博客等级 码龄18年 50粉丝 37原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值
⌂ 换网址