Android 内存溢出解决方案(OOM) 整理总结
2015-04-22 15:55
344 查看
http://mzh3344258.blog.51cto.com/1823534/804237
在最近做的工程中发现加载的图片太多或图片过大时经常出现OOM问题,找网上资料也提供了很多方法,但自己感觉有点乱,特此,今天在不同型号的三款安卓手机上做了测试,因为有效果也有结果,今天小马就做个详细的总结,以供朋友们共同交流学习,也供自己以后在解决OOM问题上有所提高,提前讲下,片幅有点长,涉及的东西太多,大家耐心看,肯定有收获的,里面的很多东西小马也是学习参考网络资料使用的,先来简单讲下下:
一般我们大家在遇到内存问题的时候常用的方式网上也有相关资料,大体如下几种:
一:在内存引用上做些处理,常用的有软引用、强化引用、弱引用
二:在内存中加载图片时直接在内存中做处理,如:边界压缩
三:动态回收内存
四:优化Dalvik虚拟机的堆内存分配
五:自定义堆内存大小
可是真的有这么简单吗,就用以上方式就能解决OOM了?不是的,继续来看...
下面小马就照着上面的次序来整理下解决的几种方式,数字序号与上面对应:
1:软引用(SoftReference)、虚引用(PhantomRefrence)、弱引用(WeakReference),这三个类是对heap中java对象的应用,通过这个三个类可以和gc做简单的交互,除了这三个以外还有一个是最常用的强引用
1.1:强引用,例如下面代码:
上面代码中第一句是在heap堆中创建新的Object对象通过o引用这个对象,第二句是通过o建立o1到new Object()这个heap堆中的对象的引用,这两个引用都是强引用.只要存在对heap中对象的引用,gc就不会收集该对象.如果通过如下代码:
heap中对象有强可及对象、软可及对象、弱可及对象、虚可及对象和不可到达对象。应用的强弱顺序是强、软、弱、和虚。对于对象是属于哪种可及的对象,由他的最强的引用决定。如下:
上面的代码中:
第一行在heap对中创建内容为“abc”的对象,并建立abc到该对象的强引用,该对象是强可及的。第二行和第三行分别建立对heap中对象的软引用和弱引用,此时heap中的对象仍是强可及的。第四行之后heap中对象不再是强可及的,变成软可及的。同样第五行执行之后变成弱可及的。
1.2:软引用
软引用是主要用于内存敏感的高速缓存。在jvm报告内存不足之前会清除所有的软引用,这样以来gc就有可能收集软可及的对象,可能解决内存吃紧问题,避免内存溢出。什么时候会被收集取决于gc的算法和gc运行时可用内存的大小。当gc决定要收集软引用是执行以下过程,以上面的abcSoftRef为例:
1 首先将abcSoftRef的referent设置为null,不再引用heap中的new String("abc")对象。
2 将heap中的new String("abc")对象设置为可结束的(finalizable)。
3 当heap中的new String("abc")对象的finalize()方法被运行而且该对象占用的内存被释放, abcSoftRef被添加到它的ReferenceQueue中。
注:对ReferenceQueue软引用和弱引用可以有可无,但是虚引用必须有,参见:
被 Soft Reference 指到的对象,即使没有任何
Direct Reference,也不会被清除。一直要到 JVM 内存不足且 没有 Direct Reference 时才会清除,SoftReference 是用来设计 object-cache 之用的。如此一来 SoftReference 不但可以把对象 cache 起来,也不会造成内存不足的错误 (OutOfMemoryError)。我觉得 Soft Reference 也适合拿来实作 pooling 的技巧。
1.3:弱引用
当gc碰到弱可及对象,并释放abcWeakRef的引用,收集该对象。但是gc可能需要对此运用才能找到该弱可及对象。通过如下代码可以了明了的看出它的作用:
运行结果:
before gc: abc
after gc: null
gc收集弱可及对象的执行过程和软可及一样,只是gc不会根据内存情况来决定是不是收集该对象。如果你希望能随时取得某对象的信息,但又不想影响此对象的垃圾收集,那么你应该用 Weak Reference 来记住此对象,而不是用一般的 reference。
在此例中,透过 get() 可以取得此 Reference 的所指到的对象,如果返回值为 null 的话,代表此对象已经被清除。这类的技巧,在设计 Optimizer 或 Debugger 这类的程序时常会用到,因为这类程序需要取得某对象的信息,但是不可以 影响此对象的垃圾收集。
1.4:虚引用
就是没有的意思,建立虚引用之后通过get方法返回结果始终为null,通过源代码你会发现,虚引用通向会把引用的对象写进referent,只是get方法返回结果为null.先看一下和gc交互的过程在说一下他的作用.
1.4.1 不把referent设置为null, 直接把heap中的new String("abc")对象设置为可结束的(finalizable).
1.4.2 与软引用和弱引用不同, 先把PhantomRefrence对象添加到它的ReferenceQueue中.然后在释放虚可及的对象.
你会发现在收集heap中的new String("abc")对象之前,你就可以做一些其他的事情.通过以下代码可以了解他的作用.
结果为
class java.lang.String@96354
gc will collect:class java.lang.String@96354 好了,关于引用就讲到这,下面看2
2:在内存中压缩小马做了下测试,对于少量不太大的图片这种方式可行,但太多而又大的图片小马用个笨的方式就是,先在内存中压缩,再用软引用避免OOM,两种方式代码如下,大家可参考下:
方式一代码如下:
方式二代码如下:
上面两种方式第一种直接使用边界压缩,第二种在使用边界压缩的情况下间接的使用了软引用来避免OOM,但大家都知道,这些函数在完成decode后,最终都是通过java层的createBitmap来完成的,需要消耗更多内存,如果图片多且大,这种方式还是会引用OOM异常的,不着急,有的是办法解决,继续看,以下方式也大有妙用的:
3:大家可以选择在合适的地方使用以下代码动态并自行显式调用GC来回收内存:
4:这个就好玩了,优化Dalvik虚拟机的堆内存分配,听着很强大,来看下具体是怎么一回事
对于Android平台来说,其托管层使用的Dalvik JavaVM从目前的表现来看还有很多地方可以优化处理,比如我们在开发一些大型游戏或耗资源的应用中可能考虑手动干涉GC处理,使用
dalvik.system.VMRuntime类提供的setTargetHeapUtilization方法可以增强程序堆内存的处理效率。当然具体原理我们可以参考开源工程,这里我们仅说下使用方法: 代码如下:
5:自定义我们的应用需要多大的内存,这个好暴力哇,强行设置最小内存大小,代码如下:
好了,文章写完了,片幅有点长,因为涉及到的东西太多了,其它文章小马都会贴源码,这篇文章小马是直接在项目中用三款安卓真机测试的,有效果,项目原码就不在这贴了,不然泄密了都,吼吼,但这里讲下还是会因为手机的不同而不同,大家得根据自己需求选择合适的方式来避免OOM,大家加油呀,每天都有或多或少的收获,这也算是进步,加油加油!
本文出自 “酷_莫名简单、KNothing” 博客,请务必保留此出处http://mzh3344258.blog.51cto.com/1823534/804237
类别:安卓学习┆阅读(61364)┆评论(28)
┆ 返回博主首页┆返回博客首页
上一篇 Android模拟器小工具实现模拟传感器(详解) 下一篇 Android
动态壁纸引擎试刀学习(初识安卓引擎)
![](http://s3.51cto.com/wyfs02/M01/5A/18/wKioL1T2oLHzwq17AAEH1ynHJBs361.jpg)
1.4 Android应用App开发工具
Android的源代码结构
疯狂披萨店 v2.0.10Android手机游戏
( 1/2 )
[1楼]
![](http://img1.51cto.com/image/skin/reply.gif)
wws5201985 回复
[align=right]2012-03-15 22:41:19[/align]
文章不错,值得学习!
希望大家支持下!赞上一个,谢谢啦!
http://wws5201985.blog.51cto.com/216275/803871
本文收录至博客专题:《2012龙年薪途活动:秘籍大起底》
希望大家支持下!赞上一个,谢谢啦!
[2楼]
![](http://img1.51cto.com/image/skin/reply.gif)
name_110 回复
[align=right]2012-04-08 07:59:16[/align]
楼主真是好人,谢谢楼主
[3楼]楼主
![](http://img1.51cto.com/image/skin/reply.gif)
华华世界 回复
[align=right]2012-04-09 09:21:43[/align]
回复 name_110:[2楼]
吼吼,我把所有遇到的人都当做朋友,加油,我们一起努力,奋斗!
[4楼]
![](http://img1.51cto.com/image/skin/reply.gif)
qq_Android 回复
[align=right]2012-06-18 13:17:05[/align]
我也遇到了这样的问题 谢谢
[5楼]楼主
![](http://img1.51cto.com/image/skin/reply.gif)
华华世界 回复
[align=right]2012-06-19 17:48:44[/align]
回复 qq_Android:[4楼]
能帮到你是最好的,呵呵,一起学习!加油。。。^_^
[6楼]
![](http://img1.51cto.com/image/skin/reply.gif)
coolismonster 回复
[align=right]2012-09-28 15:53:37[/align]
说的好详细啊 不知道内存泄露的问题其他系统有没有这么严重 碰到过这种问题 那时候只是简单地缓存图片到本地 然后将图片进行压缩 但是感觉这个问题没有很好的解决办法 只是减小了发生的几率
[7楼]楼主
![](http://img1.51cto.com/image/skin/reply.gif)
华华世界 回复
[align=right]2012-10-01 21:33:33[/align]
回复 coolismonster:[6楼]
呵呵,现在3.0版本以后有个LruCache,一般情况下使用动态内存分配就可以解决,如果还不行的话,可以试下用只缓存屏幕上显示的图片就可以了,如果高频率使用BitmapFactory.decode。。。。的话可以查找下ContentProvider做缓存,提高效率..希望对朋友有帮助! ^_^ O_O
[8楼]
![](http://img1.51cto.com/image/skin/reply.gif)
[匿名]初入江湖 回复
[align=right]2012-12-06 10:03:18[/align]
楼主好强大啊,又那么认真的发贴,楼主好人,谢谢楼主
[9楼]楼主
![](http://img1.51cto.com/image/skin/reply.gif)
华华世界 回复
[align=right]2012-12-07 12:56:30[/align]
回复 初入江湖:[8楼]
吼吼,谢谢支持.....周末会发布更为经典的篇,欢迎到时支持哦,加油加油!生命在于折腾!!! 一起努力
[10楼]
![](http://img1.51cto.com/image/skin/reply.gif)
稚者杨力 回复
[align=right]2013-04-17 17:34:24[/align]
楼主有没有PhotoScanActivity的完整的代码?你这只粘一个类出来,看不太懂啊。
邮箱 : yangli_for_study@163.com
[11楼]楼主
![](http://img1.51cto.com/image/skin/reply.gif)
华华世界 回复
[align=right]2013-04-17 19:08:18[/align]
回复 稚者杨力:[10楼]
你好。我粘出来的就是完整的类哦....其中有一些是公司里面的代码,所以可能兄弟你看起来感觉有点缺少,其实我粘出来的代码够你使用了,如果还有部分代码不懂的,直接加我扣扣,你问我回答你 929789081
[12楼]
![](http://img1.51cto.com/image/skin/reply.gif)
稚者杨力 回复
[align=right]2013-04-22 22:11:34[/align]
回复 华华世界:[11楼]
其实主要是SoftReference不了解,看过之后还是理解不了它是怎么用的。
网上也搜过好多Java的四种引用方式,没能找到一个完整的,能让我明白的例子。
现在的项目中也问题遇到OOM的错误,着实头疼。
[13楼]楼主
![](http://img1.51cto.com/image/skin/reply.gif)
华华世界 回复
[align=right]2013-04-23 01:10:49[/align]
回复 稚者杨力:[12楼]
呵呵,先给你讲个思路:不管是SoftReference、Weak Reference当中的哪一个,在内存解决的问题上真的很有帮助的,二者的区别看这里: http://johncookie.iteye.com/blog/1354394 我先讲个大体的思路吧,OOM一般是在连续decode的时候就出现的如果是ListView、GridView等包含多个子控件的控件中使用时,肯定是要用到缓存的,不然肯定报错,典型的处理方法就是在你第一次获取(decode的时候)到Bitmap的同时讲这个Bitmap以Map<Key,Value>的形式存入到SoftReference中,这个地方我给朋友你贴上一个类,你看下大体的思路
package net.oschina.app.common;
import java.io.File;
import java.io.IOException;
import java.lang.ref.SoftReference;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.WeakHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import net.oschina.app.AppException;
import net.oschina.app.api.ApiClient;
import android.graphics.Bitmap;
import android.os.Handler;
import android.os.Message;
import android.widget.ImageView;
/**
* 异步线程加载图片工具类
* 使用说明:
* BitmapManager bmpManager;
* bmpManager = new BitmapManager(BitmapFactory.decodeResource(context.getResources(), R.drawable.loading));
* bmpManager.loadBitmap(imageURL, imageView);
* @author liux (http://my.oschina.net/liux)
* @version 1.0
* @created 2012-6-25
*/
public class BitmapManager {
private static HashMap<String, SoftReference<Bitmap>> cache;
private static ExecutorService pool;
private static Map<ImageView, String> imageViews;
private Bitmap defaultBmp;
static {
cache = new HashMap<String, SoftReference<Bitmap>>();
pool = Executors.newFixedThreadPool(5); //固定线程池
imageViews = Collections.synchronizedMap(new WeakHashMap<ImageView, String>());
}
public BitmapManager(){}
public BitmapManager(Bitmap def) {
this.defaultBmp = def;
}
/**
* 设置默认图片
* @param bmp
*/
public void setDefaultBmp(Bitmap bmp) {
defaultBmp = bmp;
}
/**
* 加载图片
* @param url
* @param imageView
*/
public void loadBitmap(String url, ImageView imageView) {
loadBitmap(url, imageView, this.defaultBmp, 0, 0);
}
/**
* 加载图片-可设置加载失败后显示的默认图片
* @param url
* @param imageView
* @param defaultBmp
*/
public void loadBitmap(String url, ImageView imageView, Bitmap defaultBmp) {
loadBitmap(url, imageView, defaultBmp, 0, 0);
}
/**
* 加载图片-可指定显示图片的高宽
* @param url
* @param imageView
* @param width
* @param height
*/
public void loadBitmap(String url, ImageView imageView, Bitmap defaultBmp, int width, int height) {
imageViews.put(imageView, url);
Bitmap bitmap = getBitmapFromCache(url);
if (bitmap != null) {
//显示缓存图片
imageView.setImageBitmap(bitmap);
} else {
//加载SD卡中的图片缓存
String filename = FileUtils.getFileName(url);
String filepath = imageView.getContext().getFilesDir() + File.separator + filename;
File file = new File(filepath);
if(file.exists()){
//显示SD卡中的图片缓存
Bitmap bmp = ImageUtils.getBitmap(imageView.getContext(), filename);
imageView.setImageBitmap(bmp);
}else{
//线程加载网络图片
imageView.setImageBitmap(defaultBmp);
queueJob(url, imageView, width, height);
}
}
}
/**
* 从缓存中获取图片
* @param url
*/
public Bitmap getBitmapFromCache(String url) {
Bitmap bitmap = null;
if (cache.containsKey(url)) {
bitmap = cache.get(url).get();
}
return bitmap;
}
/**
* 从网络中加载图片
* @param url
* @param imageView
* @param width
* @param height
*/
public void queueJob(final String url, final ImageView imageView, final int width, final int height) {
/* Create handler in UI thread. */
final Handler handler = new Handler() {
public void handleMessage(Message msg) {
String tag = imageViews.get(imageView);
if (tag != null && tag.equals(url)) {
if (msg.obj != null) {
imageView.setImageBitmap((Bitmap) msg.obj);
try {
//向SD卡中写入图片缓存
ImageUtils.saveImage(imageView.getContext(), FileUtils.getFileName(url), (Bitmap) msg.obj);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
};
pool.execute(new Runnable() {
public void run() {
Message message = Message.obtain();
message.obj = downloadBitmap(url, width, height);
handler.sendMessage(message);
}
});
}
/**
* 下载图片-可指定显示图片的高宽
* @param url
* @param width
* @param height
*/
private Bitmap downloadBitmap(String url, int width, int height) {
Bitmap bitmap = null;
try {
//http加载图片
bitmap = ApiClient.getNetBitmap(url);
if(width > 0 && height > 0) {
//指定显示图片的高宽
bitmap = Bitmap.createScaledBitmap(bitmap, width, height, true);
}
//放入缓存
cache.put(url, new SoftReference<Bitmap>(bitmap));
} catch (AppException e) {
e.printStackTrace();
}
return bitmap;
}
}
在最近做的工程中发现加载的图片太多或图片过大时经常出现OOM问题,找网上资料也提供了很多方法,但自己感觉有点乱,特此,今天在不同型号的三款安卓手机上做了测试,因为有效果也有结果,今天小马就做个详细的总结,以供朋友们共同交流学习,也供自己以后在解决OOM问题上有所提高,提前讲下,片幅有点长,涉及的东西太多,大家耐心看,肯定有收获的,里面的很多东西小马也是学习参考网络资料使用的,先来简单讲下下:
一般我们大家在遇到内存问题的时候常用的方式网上也有相关资料,大体如下几种:
一:在内存引用上做些处理,常用的有软引用、强化引用、弱引用
二:在内存中加载图片时直接在内存中做处理,如:边界压缩
三:动态回收内存
四:优化Dalvik虚拟机的堆内存分配
五:自定义堆内存大小
可是真的有这么简单吗,就用以上方式就能解决OOM了?不是的,继续来看...
下面小马就照着上面的次序来整理下解决的几种方式,数字序号与上面对应:
1:软引用(SoftReference)、虚引用(PhantomRefrence)、弱引用(WeakReference),这三个类是对heap中java对象的应用,通过这个三个类可以和gc做简单的交互,除了这三个以外还有一个是最常用的强引用
1.1:强引用,例如下面代码:
Object o=new Object(); Object o1=o;
上面代码中第一句是在heap堆中创建新的Object对象通过o引用这个对象,第二句是通过o建立o1到new Object()这个heap堆中的对象的引用,这两个引用都是强引用.只要存在对heap中对象的引用,gc就不会收集该对象.如果通过如下代码:
o=null; o1=null
heap中对象有强可及对象、软可及对象、弱可及对象、虚可及对象和不可到达对象。应用的强弱顺序是强、软、弱、和虚。对于对象是属于哪种可及的对象,由他的最强的引用决定。如下:
String abc=new String("abc"); //1 SoftReference<String> abcSoftRef=new SoftReference<String>(abc); //2 WeakReference<String> abcWeakRef = new WeakReference<String>(abc); //3 abc=null; //4 abcSoftRef.clear();//5
上面的代码中:
第一行在heap对中创建内容为“abc”的对象,并建立abc到该对象的强引用,该对象是强可及的。第二行和第三行分别建立对heap中对象的软引用和弱引用,此时heap中的对象仍是强可及的。第四行之后heap中对象不再是强可及的,变成软可及的。同样第五行执行之后变成弱可及的。
1.2:软引用
软引用是主要用于内存敏感的高速缓存。在jvm报告内存不足之前会清除所有的软引用,这样以来gc就有可能收集软可及的对象,可能解决内存吃紧问题,避免内存溢出。什么时候会被收集取决于gc的算法和gc运行时可用内存的大小。当gc决定要收集软引用是执行以下过程,以上面的abcSoftRef为例:
1 首先将abcSoftRef的referent设置为null,不再引用heap中的new String("abc")对象。
2 将heap中的new String("abc")对象设置为可结束的(finalizable)。
3 当heap中的new String("abc")对象的finalize()方法被运行而且该对象占用的内存被释放, abcSoftRef被添加到它的ReferenceQueue中。
注:对ReferenceQueue软引用和弱引用可以有可无,但是虚引用必须有,参见:
Reference(T paramT, ReferenceQueue<? super T>paramReferenceQueue)
被 Soft Reference 指到的对象,即使没有任何
Direct Reference,也不会被清除。一直要到 JVM 内存不足且 没有 Direct Reference 时才会清除,SoftReference 是用来设计 object-cache 之用的。如此一来 SoftReference 不但可以把对象 cache 起来,也不会造成内存不足的错误 (OutOfMemoryError)。我觉得 Soft Reference 也适合拿来实作 pooling 的技巧。
A obj = new A(); Refenrence sr = new SoftReference(obj); //引用时 if(sr!=null){ obj = sr.get(); }else{ obj = new A(); sr = new SoftReference(obj); }
1.3:弱引用
当gc碰到弱可及对象,并释放abcWeakRef的引用,收集该对象。但是gc可能需要对此运用才能找到该弱可及对象。通过如下代码可以了明了的看出它的作用:
String abc=new String("abc"); WeakReference<String> abcWeakRef = new WeakReference<String>(abc); abc=null; System.out.println("before gc: "+abcWeakRef.get()); System.gc(); System.out.println("after gc: "+abcWeakRef.get());
运行结果:
before gc: abc
after gc: null
gc收集弱可及对象的执行过程和软可及一样,只是gc不会根据内存情况来决定是不是收集该对象。如果你希望能随时取得某对象的信息,但又不想影响此对象的垃圾收集,那么你应该用 Weak Reference 来记住此对象,而不是用一般的 reference。
A obj = new A(); WeakReference wr = new WeakReference(obj); obj = null; //等待一段时间,obj对象就会被垃圾回收 ... if (wr.get()==null) { System.out.println("obj 已经被清除了 "); } else { System.out.println("obj 尚未被清除,其信息是 "+obj.toString()); } ... }
在此例中,透过 get() 可以取得此 Reference 的所指到的对象,如果返回值为 null 的话,代表此对象已经被清除。这类的技巧,在设计 Optimizer 或 Debugger 这类的程序时常会用到,因为这类程序需要取得某对象的信息,但是不可以 影响此对象的垃圾收集。
1.4:虚引用
就是没有的意思,建立虚引用之后通过get方法返回结果始终为null,通过源代码你会发现,虚引用通向会把引用的对象写进referent,只是get方法返回结果为null.先看一下和gc交互的过程在说一下他的作用.
1.4.1 不把referent设置为null, 直接把heap中的new String("abc")对象设置为可结束的(finalizable).
1.4.2 与软引用和弱引用不同, 先把PhantomRefrence对象添加到它的ReferenceQueue中.然后在释放虚可及的对象.
你会发现在收集heap中的new String("abc")对象之前,你就可以做一些其他的事情.通过以下代码可以了解他的作用.
import java.lang.ref.PhantomReference; import java.lang.ref.Reference; import java.lang.ref.ReferenceQueue; import java.lang.reflect.Field; public class Test { public static boolean isRun = true; public static void main(String[] args) throws Exception { String abc = new String("abc"); System.out.println(abc.getClass() + "@" + abc.hashCode()); final ReferenceQueue referenceQueue = new ReferenceQueue<String>(); new Thread() { public void run() { while (isRun) { Object o = referenceQueue.poll(); if (o != null) { try { Field rereferent = Reference.class .getDeclaredField("referent"); rereferent.setAccessible(true); Object result = rereferent.get(o); System.out.println("gc will collect:" + result.getClass() + "@" + result.hashCode()); } catch (Exception e) { e.printStackTrace(); } } } } }.start(); PhantomReference<String> abcWeakRef = new PhantomReference<String>(abc, referenceQueue); abc = null; Thread.currentThread().sleep(3000); System.gc(); Thread.currentThread().sleep(3000); isRun = false; } }
结果为
class java.lang.String@96354
gc will collect:class java.lang.String@96354 好了,关于引用就讲到这,下面看2
2:在内存中压缩小马做了下测试,对于少量不太大的图片这种方式可行,但太多而又大的图片小马用个笨的方式就是,先在内存中压缩,再用软引用避免OOM,两种方式代码如下,大家可参考下:
方式一代码如下:
@SuppressWarnings("unused") private Bitmap copressImage(String imgPath){ File picture = new File(imgPath); Options bitmapFactoryOptions = new BitmapFactory.Options(); //下面这个设置是将图片边界不可调节变为可调节 bitmapFactoryOptions.inJustDecodeBounds = true; bitmapFactoryOptions.inSampleSize = 2; int outWidth = bitmapFactoryOptions.outWidth; int outHeight = bitmapFactoryOptions.outHeight; bmap = BitmapFactory.decodeFile(picture.getAbsolutePath(), bitmapFactoryOptions); float imagew = 150; float imageh = 150; int yRatio = (int) Math.ceil(bitmapFactoryOptions.outHeight / imageh); int xRatio = (int) Math .ceil(bitmapFactoryOptions.outWidth / imagew); if (yRatio > 1 || xRatio > 1) { if (yRatio > xRatio) { bitmapFactoryOptions.inSampleSize = yRatio; } else { bitmapFactoryOptions.inSampleSize = xRatio; } } bitmapFactoryOptions.inJustDecodeBounds = false; bmap = BitmapFactory.decodeFile(picture.getAbsolutePath(), bitmapFactoryOptions); if(bmap != null){ //ivwCouponImage.setImageBitmap(bmap); return bmap; } return null; }
方式二代码如下:
package com.lvguo.scanstreet.activity; import java.io.File; import java.lang.ref.SoftReference; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import android.app.Activity; import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.content.res.TypedArray; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.BitmapFactory.Options; import android.os.Bundle; import android.view.View; import android.view.ViewGroup; import android.view.WindowManager; import android.widget.AdapterView; import android.widget.AdapterView.OnItemLongClickListener; import android.widget.BaseAdapter; import android.widget.Gallery; import android.widget.ImageView; import android.widget.Toast; import com.lvguo.scanstreet.R; import com.lvguo.scanstreet.data.ApplicationData; /** * @Title: PhotoScanActivity.java * @Description: 照片预览控制类 * @author XiaoMa */ public class PhotoScanActivity extends Activity { private Gallery gallery ; private List<String> ImageList; private List<String> it ; private ImageAdapter adapter ; private String path ; private String shopType; private HashMap<String, SoftReference<Bitmap>> imageCache = null; private Bitmap bitmap = null; private SoftReference<Bitmap> srf = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); setContentView(R.layout.photoscan); Intent intent = this.getIntent(); if(intent != null){ if(intent.getBundleExtra("bundle") != null){ Bundle bundle = intent.getBundleExtra("bundle"); path = bundle.getString("path"); shopType = bundle.getString("shopType"); } } init(); } private void init(){ imageCache = new HashMap<String, SoftReference<Bitmap>>(); gallery = (Gallery)findViewById(R.id.gallery); ImageList = getSD(); if(ImageList.size() == 0){ Toast.makeText(getApplicationContext(), "无照片,请返回拍照后再使用预览", Toast.LENGTH_SHORT).show(); return ; } adapter = new ImageAdapter(this, ImageList); gallery.setAdapter(adapter); gallery.setOnItemLongClickListener(longlistener); } /** * Gallery长按事件操作实现 */ private OnItemLongClickListener longlistener = new OnItemLongClickListener() { @Override public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) { //此处添加长按事件删除照片实现 AlertDialog.Builder dialog = new AlertDialog.Builder(PhotoScanActivity.this); dialog.setIcon(R.drawable.warn); dialog.setTitle("删除提示"); dialog.setMessage("你确定要删除这张照片吗?"); dialog.setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { File file = new File(it.get(position)); boolean isSuccess; if(file.exists()){ isSuccess = file.delete(); if(isSuccess){ ImageList.remove(position); adapter.notifyDataSetChanged(); //gallery.setAdapter(adapter); if(ImageList.size() == 0){ Toast.makeText(getApplicationContext(), getResources().getString(R.string.phoSizeZero), Toast.LENGTH_SHORT).show(); } Toast.makeText(getApplicationContext(), getResources().getString(R.string.phoDelSuccess), Toast.LENGTH_SHORT).show(); } } } }); dialog.setNegativeButton("取消",new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); dialog.create().show(); return false; } }; /** * 获取SD卡上的所有图片文件 * @return */ private List<String> getSD() { /* 设定目前所在路径 */ File fileK ; it = new ArrayList<String>(); if("newadd".equals(shopType)){ //如果是从查看本人新增列表项或商户列表项进来时 fileK = new File(ApplicationData.TEMP); }else{ //此时为纯粹新增 fileK = new File(path); } File[] files = fileK.listFiles(); if(files != null && files.length>0){ for(File f : files ){ if(getImageFile(f.getName())){ it.add(f.getPath()); Options bitmapFactoryOptions = new BitmapFactory.Options(); //下面这个设置是将图片边界不可调节变为可调节 bitmapFactoryOptions.inJustDecodeBounds = true; bitmapFactoryOptions.inSampleSize = 5; int outWidth = bitmapFactoryOptions.outWidth; int outHeight = bitmapFactoryOptions.outHeight; float imagew = 150; float imageh = 150; int yRatio = (int) Math.ceil(bitmapFactoryOptions.outHeight / imageh); int xRatio = (int) Math .ceil(bitmapFactoryOptions.outWidth / imagew); if (yRatio > 1 || xRatio > 1) { if (yRatio > xRatio) { bitmapFactoryOptions.inSampleSize = yRatio; } else { bitmapFactoryOptions.inSampleSize = xRatio; } } bitmapFactoryOptions.inJustDecodeBounds = false; bitmap = BitmapFactory.decodeFile(f.getPath(), bitmapFactoryOptions); //bitmap = BitmapFactory.decodeFile(f.getPath()); srf = new SoftReference<Bitmap>(bitmap); imageCache.put(f.getName(), srf); } } } return it; } /** * 获取图片文件方法的具体实现 * @param fName * @return */ private boolean getImageFile(String fName) { boolean re; /* 取得扩展名 */ String end = fName .substring(fName.lastIndexOf(".") + 1, fName.length()) .toLowerCase(); /* 按扩展名的类型决定MimeType */ if (end.equals("jpg") || end.equals("gif") || end.equals("png") || end.equals("jpeg") || end.equals("bmp")) { re = true; } else { re = false; } return re; } public class ImageAdapter extends BaseAdapter{ /* 声明变量 */ int mGalleryItemBackground; private Context mContext; private List<String> lis; /* ImageAdapter的构造符 */ public ImageAdapter(Context c, List<String> li) { mContext = c; lis = li; TypedArray a = obtainStyledAttributes(R.styleable.Gallery); mGalleryItemBackground = a.getResourceId(R.styleable.Gallery_android_galleryItemBackground, 0); a.recycle(); } /* 几定要重写的方法getCount,传回图片数目 */ public int getCount() { return lis.size(); } /* 一定要重写的方法getItem,传回position */ public Object getItem(int position) { return lis.get(position); } /* 一定要重写的方法getItemId,传并position */ public long getItemId(int position) { return position; } /* 几定要重写的方法getView,传并几View对象 */ public View getView(int position, View convertView, ViewGroup parent) { System.out.println("lis:"+lis); File file = new File(it.get(position)); SoftReference<Bitmap> srf = imageCache.get(file.getName()); Bitmap bit = srf.get(); ImageView i = new ImageView(mContext); i.setImageBitmap(bit); i.setScaleType(ImageView.ScaleType.FIT_XY); i.setLayoutParams( new Gallery.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT)); return i; } } }
上面两种方式第一种直接使用边界压缩,第二种在使用边界压缩的情况下间接的使用了软引用来避免OOM,但大家都知道,这些函数在完成decode后,最终都是通过java层的createBitmap来完成的,需要消耗更多内存,如果图片多且大,这种方式还是会引用OOM异常的,不着急,有的是办法解决,继续看,以下方式也大有妙用的:
1. InputStream is = this.getResources().openRawResource(R.drawable.pic1); BitmapFactory.Options options=new BitmapFactory.Options(); options.inJustDecodeBounds = false; options.inSampleSize = 10; //width,hight设为原来的十分一 Bitmap btp =BitmapFactory.decodeStream(is,null,options); 2. if(!bmp.isRecycle() ){ bmp.recycle() //回收图片所占的内存 system.gc() //提醒系统及时回收 } 上面代码与下面代码大家可分开使用,也可有效缓解内存问题哦...吼吼... /** 这个地方大家别搞混了,为了方便小马把两个贴一起了,使用的时候记得分开使用 * 以最省内存的方式读取本地资源的图片 */ public static Bitmap readBitMap(Context context, int resId){ BitmapFactory.Options opt = new BitmapFactory.Options(); opt.inPreferredConfig = Bitmap.Config.RGB_565; opt.inPurgeable = true; opt.inInputShareable = true; //获取资源图片 InputStream is = context.getResources().openRawResource(resId); return BitmapFactory.decodeStream(is,null,opt); }
3:大家可以选择在合适的地方使用以下代码动态并自行显式调用GC来回收内存:
if(bitmapObject.isRecycled()==false) //如果没有回收 bitmapObject.recycle();
4:这个就好玩了,优化Dalvik虚拟机的堆内存分配,听着很强大,来看下具体是怎么一回事
对于Android平台来说,其托管层使用的Dalvik JavaVM从目前的表现来看还有很多地方可以优化处理,比如我们在开发一些大型游戏或耗资源的应用中可能考虑手动干涉GC处理,使用
dalvik.system.VMRuntime类提供的setTargetHeapUtilization方法可以增强程序堆内存的处理效率。当然具体原理我们可以参考开源工程,这里我们仅说下使用方法: 代码如下:
private final static floatTARGET_HEAP_UTILIZATION = 0.75f; 在程序onCreate时就可以调用 VMRuntime.getRuntime().setTargetHeapUtilization(TARGET_HEAP_UTILIZATION); 即可
5:自定义我们的应用需要多大的内存,这个好暴力哇,强行设置最小内存大小,代码如下:
private final static int CWJ_HEAP_SIZE = 6* 1024* 1024 ; //设置最小heap内存为6MB大小 VMRuntime.getRuntime().setMinimumHeapSize(CWJ_HEAP_SIZE);
好了,文章写完了,片幅有点长,因为涉及到的东西太多了,其它文章小马都会贴源码,这篇文章小马是直接在项目中用三款安卓真机测试的,有效果,项目原码就不在这贴了,不然泄密了都,吼吼,但这里讲下还是会因为手机的不同而不同,大家得根据自己需求选择合适的方式来避免OOM,大家加油呀,每天都有或多或少的收获,这也算是进步,加油加油!
本文出自 “酷_莫名简单、KNothing” 博客,请务必保留此出处http://mzh3344258.blog.51cto.com/1823534/804237
![]() | ![]() |
![]() 蒙奇小路飞、wangchenze123、lzl_bs | 12人 | 了这篇文章 |
┆ 返回博主首页┆返回博客首页
上一篇 Android模拟器小工具实现模拟传感器(详解) 下一篇 Android
动态壁纸引擎试刀学习(初识安卓引擎)
![](http://s3.51cto.com/wyfs02/M01/5A/18/wKioL1T2oLHzwq17AAEH1ynHJBs361.jpg)
相关文章
Android高手应该精通哪些内容?1.4 Android应用App开发工具
Android的源代码结构
疯狂披萨店 v2.0.10Android手机游戏
文章评论
<< 1 2 >> 页数( 1/2 )
[1楼]
![](http://img1.51cto.com/image/skin/reply.gif)
wws5201985 回复
[align=right]2012-03-15 22:41:19[/align]
文章不错,值得学习!
希望大家支持下!赞上一个,谢谢啦!
http://wws5201985.blog.51cto.com/216275/803871
本文收录至博客专题:《2012龙年薪途活动:秘籍大起底》
希望大家支持下!赞上一个,谢谢啦!
[2楼]
![](http://img1.51cto.com/image/skin/reply.gif)
name_110 回复
[align=right]2012-04-08 07:59:16[/align]
楼主真是好人,谢谢楼主
[3楼]楼主
![](http://img1.51cto.com/image/skin/reply.gif)
华华世界 回复
[align=right]2012-04-09 09:21:43[/align]
回复 name_110:[2楼]
吼吼,我把所有遇到的人都当做朋友,加油,我们一起努力,奋斗!
[4楼]
![](http://img1.51cto.com/image/skin/reply.gif)
qq_Android 回复
[align=right]2012-06-18 13:17:05[/align]
我也遇到了这样的问题 谢谢
[5楼]楼主
![](http://img1.51cto.com/image/skin/reply.gif)
华华世界 回复
[align=right]2012-06-19 17:48:44[/align]
回复 qq_Android:[4楼]
能帮到你是最好的,呵呵,一起学习!加油。。。^_^
[6楼]
![](http://img1.51cto.com/image/skin/reply.gif)
coolismonster 回复
[align=right]2012-09-28 15:53:37[/align]
说的好详细啊 不知道内存泄露的问题其他系统有没有这么严重 碰到过这种问题 那时候只是简单地缓存图片到本地 然后将图片进行压缩 但是感觉这个问题没有很好的解决办法 只是减小了发生的几率
[7楼]楼主
![](http://img1.51cto.com/image/skin/reply.gif)
华华世界 回复
[align=right]2012-10-01 21:33:33[/align]
回复 coolismonster:[6楼]
呵呵,现在3.0版本以后有个LruCache,一般情况下使用动态内存分配就可以解决,如果还不行的话,可以试下用只缓存屏幕上显示的图片就可以了,如果高频率使用BitmapFactory.decode。。。。的话可以查找下ContentProvider做缓存,提高效率..希望对朋友有帮助! ^_^ O_O
[8楼]
![](http://img1.51cto.com/image/skin/reply.gif)
[匿名]初入江湖 回复
[align=right]2012-12-06 10:03:18[/align]
楼主好强大啊,又那么认真的发贴,楼主好人,谢谢楼主
[9楼]楼主
![](http://img1.51cto.com/image/skin/reply.gif)
华华世界 回复
[align=right]2012-12-07 12:56:30[/align]
回复 初入江湖:[8楼]
吼吼,谢谢支持.....周末会发布更为经典的篇,欢迎到时支持哦,加油加油!生命在于折腾!!! 一起努力
[10楼]
![](http://img1.51cto.com/image/skin/reply.gif)
稚者杨力 回复
[align=right]2013-04-17 17:34:24[/align]
楼主有没有PhotoScanActivity的完整的代码?你这只粘一个类出来,看不太懂啊。
邮箱 : yangli_for_study@163.com
[11楼]楼主
![](http://img1.51cto.com/image/skin/reply.gif)
华华世界 回复
[align=right]2013-04-17 19:08:18[/align]
回复 稚者杨力:[10楼]
你好。我粘出来的就是完整的类哦....其中有一些是公司里面的代码,所以可能兄弟你看起来感觉有点缺少,其实我粘出来的代码够你使用了,如果还有部分代码不懂的,直接加我扣扣,你问我回答你 929789081
[12楼]
![](http://img1.51cto.com/image/skin/reply.gif)
稚者杨力 回复
[align=right]2013-04-22 22:11:34[/align]
回复 华华世界:[11楼]
其实主要是SoftReference不了解,看过之后还是理解不了它是怎么用的。
网上也搜过好多Java的四种引用方式,没能找到一个完整的,能让我明白的例子。
现在的项目中也问题遇到OOM的错误,着实头疼。
[13楼]楼主
![](http://img1.51cto.com/image/skin/reply.gif)
华华世界 回复
[align=right]2013-04-23 01:10:49[/align]
回复 稚者杨力:[12楼]
呵呵,先给你讲个思路:不管是SoftReference、Weak Reference当中的哪一个,在内存解决的问题上真的很有帮助的,二者的区别看这里: http://johncookie.iteye.com/blog/1354394 我先讲个大体的思路吧,OOM一般是在连续decode的时候就出现的如果是ListView、GridView等包含多个子控件的控件中使用时,肯定是要用到缓存的,不然肯定报错,典型的处理方法就是在你第一次获取(decode的时候)到Bitmap的同时讲这个Bitmap以Map<Key,Value>的形式存入到SoftReference中,这个地方我给朋友你贴上一个类,你看下大体的思路
package net.oschina.app.common;
import java.io.File;
import java.io.IOException;
import java.lang.ref.SoftReference;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.WeakHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import net.oschina.app.AppException;
import net.oschina.app.api.ApiClient;
import android.graphics.Bitmap;
import android.os.Handler;
import android.os.Message;
import android.widget.ImageView;
/**
* 异步线程加载图片工具类
* 使用说明:
* BitmapManager bmpManager;
* bmpManager = new BitmapManager(BitmapFactory.decodeResource(context.getResources(), R.drawable.loading));
* bmpManager.loadBitmap(imageURL, imageView);
* @author liux (http://my.oschina.net/liux)
* @version 1.0
* @created 2012-6-25
*/
public class BitmapManager {
private static HashMap<String, SoftReference<Bitmap>> cache;
private static ExecutorService pool;
private static Map<ImageView, String> imageViews;
private Bitmap defaultBmp;
static {
cache = new HashMap<String, SoftReference<Bitmap>>();
pool = Executors.newFixedThreadPool(5); //固定线程池
imageViews = Collections.synchronizedMap(new WeakHashMap<ImageView, String>());
}
public BitmapManager(){}
public BitmapManager(Bitmap def) {
this.defaultBmp = def;
}
/**
* 设置默认图片
* @param bmp
*/
public void setDefaultBmp(Bitmap bmp) {
defaultBmp = bmp;
}
/**
* 加载图片
* @param url
* @param imageView
*/
public void loadBitmap(String url, ImageView imageView) {
loadBitmap(url, imageView, this.defaultBmp, 0, 0);
}
/**
* 加载图片-可设置加载失败后显示的默认图片
* @param url
* @param imageView
* @param defaultBmp
*/
public void loadBitmap(String url, ImageView imageView, Bitmap defaultBmp) {
loadBitmap(url, imageView, defaultBmp, 0, 0);
}
/**
* 加载图片-可指定显示图片的高宽
* @param url
* @param imageView
* @param width
* @param height
*/
public void loadBitmap(String url, ImageView imageView, Bitmap defaultBmp, int width, int height) {
imageViews.put(imageView, url);
Bitmap bitmap = getBitmapFromCache(url);
if (bitmap != null) {
//显示缓存图片
imageView.setImageBitmap(bitmap);
} else {
//加载SD卡中的图片缓存
String filename = FileUtils.getFileName(url);
String filepath = imageView.getContext().getFilesDir() + File.separator + filename;
File file = new File(filepath);
if(file.exists()){
//显示SD卡中的图片缓存
Bitmap bmp = ImageUtils.getBitmap(imageView.getContext(), filename);
imageView.setImageBitmap(bmp);
}else{
//线程加载网络图片
imageView.setImageBitmap(defaultBmp);
queueJob(url, imageView, width, height);
}
}
}
/**
* 从缓存中获取图片
* @param url
*/
public Bitmap getBitmapFromCache(String url) {
Bitmap bitmap = null;
if (cache.containsKey(url)) {
bitmap = cache.get(url).get();
}
return bitmap;
}
/**
* 从网络中加载图片
* @param url
* @param imageView
* @param width
* @param height
*/
public void queueJob(final String url, final ImageView imageView, final int width, final int height) {
/* Create handler in UI thread. */
final Handler handler = new Handler() {
public void handleMessage(Message msg) {
String tag = imageViews.get(imageView);
if (tag != null && tag.equals(url)) {
if (msg.obj != null) {
imageView.setImageBitmap((Bitmap) msg.obj);
try {
//向SD卡中写入图片缓存
ImageUtils.saveImage(imageView.getContext(), FileUtils.getFileName(url), (Bitmap) msg.obj);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
};
pool.execute(new Runnable() {
public void run() {
Message message = Message.obtain();
message.obj = downloadBitmap(url, width, height);
handler.sendMessage(message);
}
});
}
/**
* 下载图片-可指定显示图片的高宽
* @param url
* @param width
* @param height
*/
private Bitmap downloadBitmap(String url, int width, int height) {
Bitmap bitmap = null;
try {
//http加载图片
bitmap = ApiClient.getNetBitmap(url);
if(width > 0 && height > 0) {
//指定显示图片的高宽
bitmap = Bitmap.createScaledBitmap(bitmap, width, height, true);
}
//放入缓存
cache.put(url, new SoftReference<Bitmap>(bitmap));
} catch (AppException e) {
e.printStackTrace();
}
return bitmap;
}
}
相关文章推荐
- Android 内存溢出解决方案(OOM) 整理总结
- Android 内存溢出解决方案(OOM) 整理总结
- Android 内存溢出解决方案(OOM) 整理总结
- Android 内存溢出解决方案(OOM) 整理总结
- Android 内存溢出解决方案(OOM) 整理总结
- Android 内存溢出解决方案(OOM) 整理总结
- Android 内存溢出解决方案(OOM) 整理总结
- Android 内存溢出解决方案(OOM) 整理总结
- Android 内存溢出解决方案(OOM) 整理总结
- Android 内存溢出解决方案(OOM) 整理总结
- Android 内存溢出解决方案(OOM) 整理总结
- Android 内存溢出解决方案(OOM) 整理总结
- Android 内存溢出解决方案(OOM) 整理总结
- Android 内存溢出解决方案(OOM) 整理总结
- Android 内存溢出解决方案(OOM) 整理总结
- Android 内存溢出解决方案(OOM) 整理总结
- Android 内存溢出解决方案(OOM) 整理总结
- Android 内存溢出解决方案(OOM) 整理总结
- Android 内存溢出解决方案(OOM) 整理总结
- Android 内存溢出解决方案(OOM) 整理总结