如何在Android中释放内存以避免内存泄漏 [英] How to release memory in android to avoid memory leak

查看:184
本文介绍了如何在Android中释放内存以避免内存泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在浏览android开发人员网站时,我发现了这个.

While going through the android developer site i found this .

它说要避免内存泄漏,我们应该在onStop()中释放资源,但要这样做.

it says to avoid memory leak we should release resources in onStop()but how to do so.

推荐答案

基本上任何适当为空的对象都被视为已释放,并且操作系统可以回收其内存.您的问题过于笼统,很难提供详尽的方法列表,但通常应注意以下几点:

Basically any objects that are properly nulled are considered released and their memory can be reclaimed by the OS. Your question is too general and it's hard to offer a exhaustive list of methods, but you should generally be aware of these:

  1. 停止/关闭不再需要的任何服务/文件/连接
  2. 请勿在任何静态对象中存储任何Drawable,而Drawable保留对其所有者View的引用,而该View保留对其所有者Activity的引用,因此,如果您保留任何Drawable,您将持有很多对象/内存不必要
  3. 对于实用程序应用程序,您可能不必担心内存.但是对于使用大量位图的应用,您应该对位图有深刻的了解管理以及如何在应用中使用位图以有效地对其进行管理
  1. Stop/close any services/files/connections that you no longer need
  2. Do NOT store any Drawable in any static Object, Drawables hold references to their owner View's which hold references to their owner Activity's, so if you hold on to any Drawable you will hold onto a lot of objects/memory unnecessarily
  3. For an utility app, you probably needn't worry about memory; but for apps that use lots of Bitmaps, you should have a deep understanding of Bitmap management and how Bitmaps are used in your app in order to manage them effectively

这篇关于如何在Android中释放内存以避免内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆