[英]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:
其中一些是:
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. 您正在执行一些连续需要大量内存的操作,并且在某些时候它超出了进程的最大堆内存限制。
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.
这称为内存泄漏。
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一起使用,并提供逐步指南:
hprof-conv com.example.android.hcgallery.hprof mat.hprof
hprof-conv com.example.android.hcgallery.hprof mat.hprof
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.