简体   繁体   English

如何在Android中释放内存?

[英]How to release memory in Android?

我想知道如何以编程方式在Android中释放内存,因为我的布局包含很多视图,并且当我转到任何“活动”时,应用程序都会停止工作,并给我显示内存不足错误。

So what is the way to release Memory  ?

Android Already define How to use that Click here. Android已经定义了如何使用它

Possible Reasons: 可能的原因:

There are number of reasons why we get a Out of memory errors. 出现内存不足错误的原因有很多。 Some of those are: 其中一些是:

  1. You are doing some operation that continuously demands a lot of memory and at some point it goes beyond the max heap memory limit of a process. 您正在执行一些连续需要大量内存的操作,并且在某些时候它超出了进程的最大堆内存限制。

  2. You are leaking some memory ie you didn't make the previous objects you allocated eligible for Garbage Collection (GC). 您正在泄漏一些内存,即您没有使分配的先前对象符合垃圾收集(GC)的条件。 This is called Memory leak. 这称为内存泄漏。

  3. You are dealing with large bitmaps and loading all of them at run time. 您正在处理大型位图,并在运行时加载所有位图。 You have to deal very carefully with large bitmaps by loading the size that you need not the whole bitmap at once and then do scaling. 您必须非常小心地处理大型位图,方法是一次加载不需要整个位图的大小,然后进行缩放。

Tools to Diagnose OOM : 诊断OOM的工具:

In Android there are some tools that can be used to find the memory leaks and one of them is MAT (Memory Analyzer Tool). 在Android中,有一些工具可用于查找内存泄漏,其中之一是MAT(内存分析器工具)。 This can be used as a plugin to Eclipse or as a stand alone tool. 它可以用作Eclipse的插件或独立的工具。

Lets start with installing MAT and using it with Eclipse with a step by step guide: 让我们开始安装MAT并将其与Eclipse一起使用,并提供逐步指南:

  1. To start up with this tool you have to download this tool from link : 要启动此工具,您必须从链接下载此工具:
  2. Start Eclipse and run your application. 启动Eclipse并运行您的应用程序。
  3. Go to DDMS and select your application and click the Dump HPROF file button and it will ask you to save the hprof file. 转到DDMS并选择您的应用程序,然后单击“转储HPROF文件”按钮,它将要求您保存hprof文件。 Save it on your hard disk. 将其保存在硬盘上。
  4. Run the MemoryAnalyzer.exe file situated in the MAT package downloaded. 运行位于下载的MAT包中的MemoryAnalyzer.exe文件。
  5. his MAT tool will not understand the hprof file generated by Eclipse so you have to convert it using the hprof-conv tool in the tools folder of android-sdk using the following command: 他的MAT工具将无法理解Eclipse生成的hprof文件,因此您必须使用以下命令使用android-sdk的tools文件夹中的hprof-conv工具对其进行转换:

hprof-conv com.example.android.hcgallery.hprof mat.hprof hprof-conv com.example.android.hcgallery.hprof mat.hprof

  • Now this file is ready to use with MAT. 现在该文件已准备好与MAT一起使用。

在此处输入图片说明

And one more things i found that Memory Analysis for Android Applications 我发现Android应用程序的内存分析还有另外一件事

You cant just release memory it is the garbage collectors jobs to find the reference of this object if the object is not used then garbage collector kicks in. 您不能只是释放内存,如果不使用该对象,则垃圾收集器作业将找到该对象的引用,然后垃圾收集器将启动。

The problem is that you have a lots of nested View which consumes a lot of memory, you can either minimized your views to use less memory space in the heap or dynamically add the view when you need it and remove other views when you dont need it to save memory space. 问题是您有lots of nested View ,这些lots of nested View消耗大量内存,您可以最小化视图以使用较少的堆内存空间,也可以在需要时动态添加视图,而在不需要时删除其他视图节省内存空间。

I use this onBackPressed event, and it totally release my app process: 我使用此onBackPressed事件,它完全释放了我的应用程序进程:

android.os.Process.killProcess(android.os.Process.myPid());

Hope it help 希望对你有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM