Android 内存溢出解决方案(OOM) 整理总结 2012-03-13 01:24:07 标签: Android Android加载大 移动开发 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处、作者信息和本声明。否则将追究法律责任。 http://mzh3344258.blog.51cto.com/1823534/804237

在最近做的工程中发现加载的图片太多或图片过大时经常出现OOM问题,找网上资料也提供了很多方法,但自己感觉有点乱,特此,今天在不同型号的三款安卓手机上做了测试,因为有效果也有结果,今天小马就做个详细的总结,以供朋友们共同交流学习,也供自己以后在解决OOM问题上有所提高,提前讲下,片幅有点长,涉及的东西太多,大家耐心看,肯定有收获的,里面的很多东西小马也是学习参考网络资料使用的,先来简单讲下下:

一般我们大家在遇到内存问题的时候常用的方式网上也有相关资料,大体如下几种:

一:在内存引用上做些处理,常用的有软引用、强化引用、弱引用

二:在内存中加载图片时直接在内存中做处理,如:边界压缩

三:动态回收内存

四:优化Dalvik虚拟机的堆内存分配

五:自定义堆内存大小

可是真的有这么简单吗,就用以上方式就能解决OOM了?不是的,继续来看...

下面小马就照着上面的次序来整理下解决的几种方式,数字序号与上面对应:

1:软引用(SoftReference)、虚引用(PhantomRefrence)、弱引用(WeakReference),这三个类是对heap中java对象的应用,通过这个三个类可以和gc做简单的交互,除了这三个以外还有一个是最常用的强引用

1.1:强引用,例如下面代码:

            
  1. Objecto=newObject();
  2. Objecto1=o;

上面代码中第一句是在heap堆中创建新的Object对象通过o引用这个对象,第二句是通过o建立o1到new Object()这个heap堆中的对象的引用,这两个引用都是强引用.只要存在对heap中对象的引用,gc就不会收集该对象.如果通过如下代码:

            
  1. o=null;
  2. o1=null

heap中对象有强可及对象、软可及对象、弱可及对象、虚可及对象和不可到达对象。应用的强弱顺序是强、软、弱、和虚。对于对象是属于哪种可及的对象,由他的最强的引用决定。如下:

            
  1. Stringabc=newString("abc");//1
  2. SoftReference<String>abcSoftRef=newSoftReference<String>(abc);//2
  3. WeakReference<String>abcWeakRef=newWeakReference<String>(abc);//3
  4. abc=null;//4
  5. 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软引用和弱引用可以有可无,但是虚引用必须有,参见:

            
  1. Reference(TparamT,ReferenceQueue<?superT>paramReferenceQueue)

被 Soft Reference 指到的对象,即使没有任何 Direct Reference,也不会被清除。一直要到 JVM 内存不足且 没有 Direct Reference 时才会清除,SoftReference 是用来设计 object-cache 之用的。如此一来 SoftReference 不但可以把对象 cache 起来,也不会造成内存不足的错误 (OutOfMemoryError)。我觉得 Soft Reference 也适合拿来实作 pooling 的技巧。

            
  1. Aobj=newA();
  2. Refenrencesr=newSoftReference(obj);
  3. //引用时
  4. if(sr!=null){
  5. obj=sr.get();
  6. }else{
  7. obj=newA();
  8. sr=newSoftReference(obj);
  9. }

1.3:弱引用

当gc碰到弱可及对象,并释放abcWeakRef的引用,收集该对象。但是gc可能需要对此运用才能找到该弱可及对象。通过如下代码可以了明了的看出它的作用:

            
  1. Stringabc=newString("abc");
  2. WeakReference<String>abcWeakRef=newWeakReference<String>(abc);
  3. abc=null;
  4. System.out.println("beforegc:"+abcWeakRef.get());
  5. System.gc();
  6. System.out.println("aftergc:"+abcWeakRef.get());

运行结果:

before gc: abc

after gc: null

gc收集弱可及对象的执行过程和软可及一样,只是gc不会根据内存情况来决定是不是收集该对象。如果你希望能随时取得某对象的信息,但又不想影响此对象的垃圾收集,那么你应该用 Weak Reference 来记住此对象,而不是用一般的 reference。

            
  1. Aobj=newA();
  2. WeakReferencewr=newWeakReference(obj);
  3. obj=null;
  4. //等待一段时间,obj对象就会被垃圾回收
  5.   ...
  6.   if(wr.get()==null){
  7.   System.out.println("obj已经被清除了");
  8.   }else{
  9.   System.out.println("obj尚未被清除,其信息是"+obj.toString());
  10.   }
  11.   ...
  12. }

在此例中,透过 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")对象之前,你就可以做一些其他的事情.通过以下代码可以了解他的作用.

            
  1. importjava.lang.ref.PhantomReference;
  2. importjava.lang.ref.Reference;
  3. importjava.lang.ref.ReferenceQueue;
  4. importjava.lang.reflect.Field;
  5. publicclassTest{
  6. publicstaticbooleanisRun=true;
  7. publicstaticvoidmain(String[]args)throwsException{
  8. Stringabc=newString("abc");
  9. System.out.println(abc.getClass()+"@"+abc.hashCode());
  10. finalReferenceQueuereferenceQueue=newReferenceQueue<String>();
  11. newThread(){
  12. publicvoidrun(){
  13. while(isRun){
  14. Objecto=referenceQueue.poll();
  15. if(o!=null){
  16. try{
  17. Fieldrereferent=Reference.class
  18. .getDeclaredField("referent");
  19. rereferent.setAccessible(true);
  20. Objectresult=rereferent.get(o);
  21. System.out.println("gcwillcollect:"
  22. +result.getClass()+"@"
  23. +result.hashCode());
  24. }catch(Exceptione){
  25. e.printStackTrace();
  26. }
  27. }
  28. }
  29. }
  30. }.start();
  31. PhantomReference<String>abcWeakRef=newPhantomReference<String>(abc,
  32. referenceQueue);
  33. abc=null;
  34. Thread.currentThread().sleep(3000);
  35. System.gc();
  36. Thread.currentThread().sleep(3000);
  37. isRun=false;
  38. }
  39. }

结果为

class java.lang.String@96354

gc will collect:class java.lang.String@96354 好了,关于引用就讲到这,下面看2

2:在内存中压缩小马做了下测试,对于少量不太大的图片这种方式可行,但太多而又大的图片小马用个笨的方式就是,先在内存中压缩,再用软引用避免OOM,两种方式代码如下,大家可参考下:

方式一代码如下:

            
  1. @SuppressWarnings("unused")
  2. privateBitmapcopressImage(StringimgPath){
  3. Filepicture=newFile(imgPath);
  4. OptionsbitmapFactoryOptions=newBitmapFactory.Options();
  5. //下面这个设置是将图片边界不可调节变为可调节
  6. bitmapFactoryOptions.inJustDecodeBounds=true;
  7. bitmapFactoryOptions.inSampleSize=2;
  8. intoutWidth=bitmapFactoryOptions.outWidth;
  9. intoutHeight=bitmapFactoryOptions.outHeight;
  10. bmap=BitmapFactory.decodeFile(picture.getAbsolutePath(),
  11. bitmapFactoryOptions);
  12. floatimagew=150;
  13. floatimageh=150;
  14. intyRatio=(int)Math.ceil(bitmapFactoryOptions.outHeight
  15. /imageh);
  16. intxRatio=(int)Math
  17. .ceil(bitmapFactoryOptions.outWidth/imagew);
  18. if(yRatio>1||xRatio>1){
  19. if(yRatio>xRatio){
  20. bitmapFactoryOptions.inSampleSize=yRatio;
  21. }else{
  22. bitmapFactoryOptions.inSampleSize=xRatio;
  23. }
  24. }
  25. bitmapFactoryOptions.inJustDecodeBounds=false;
  26. bmap=BitmapFactory.decodeFile(picture.getAbsolutePath(),
  27. bitmapFactoryOptions);
  28. if(bmap!=null){
  29. //ivwCouponImage.setImageBitmap(bmap);
  30. returnbmap;
  31. }
  32. returnnull;
  33. }

方式二代码如下:

            
  1. packagecom.lvguo.scanstreet.activity;
  2. importjava.io.File;
  3. importjava.lang.ref.SoftReference;
  4. importjava.util.ArrayList;
  5. importjava.util.HashMap;
  6. importjava.util.List;
  7. importandroid.app.Activity;
  8. importandroid.app.AlertDialog;
  9. importandroid.content.Context;
  10. importandroid.content.DialogInterface;
  11. importandroid.content.Intent;
  12. importandroid.content.res.TypedArray;
  13. importandroid.graphics.Bitmap;
  14. importandroid.graphics.BitmapFactory;
  15. importandroid.graphics.BitmapFactory.Options;
  16. importandroid.os.Bundle;
  17. importandroid.view.View;
  18. importandroid.view.ViewGroup;
  19. importandroid.view.WindowManager;
  20. importandroid.widget.AdapterView;
  21. importandroid.widget.AdapterView.OnItemLongClickListener;
  22. importandroid.widget.BaseAdapter;
  23. importandroid.widget.Gallery;
  24. importandroid.widget.ImageView;
  25. importandroid.widget.Toast;
  26. importcom.lvguo.scanstreet.R;
  27. importcom.lvguo.scanstreet.data.ApplicationData;
  28. /**
  29. *@Title:PhotoScanActivity.java
  30. *@Description:照片预览控制类
  31. *@authorXiaoMa
  32. */
  33. publicclassPhotoScanActivityextendsActivity{
  34. privateGallerygallery;
  35. privateList<String>ImageList;
  36. privateList<String>it;
  37. privateImageAdapteradapter;
  38. privateStringpath;
  39. privateStringshopType;
  40. privateHashMap<String,SoftReference<Bitmap>>imageCache=null;
  41. privateBitmapbitmap=null;
  42. privateSoftReference<Bitmap>srf=null;
  43. @Override
  44. publicvoidonCreate(BundlesavedInstanceState){
  45. super.onCreate(savedInstanceState);
  46. getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
  47. WindowManager.LayoutParams.FLAG_FULLSCREEN);
  48. setContentView(R.layout.photoscan);
  49. Intentintent=this.getIntent();
  50. if(intent!=null){
  51. if(intent.getBundleExtra("bundle")!=null){
  52. Bundlebundle=intent.getBundleExtra("bundle");
  53. path=bundle.getString("path");
  54. shopType=bundle.getString("shopType");
  55. }
  56. }
  57. init();
  58. }
  59. privatevoidinit(){
  60. imageCache=newHashMap<String,SoftReference<Bitmap>>();
  61. gallery=(Gallery)findViewById(R.id.gallery);
  62. ImageList=getSD();
  63. if(ImageList.size()==0){
  64. Toast.makeText(getApplicationContext(),"无照片,请返回拍照后再使用预览",Toast.LENGTH_SHORT).show();
  65. return;
  66. }
  67. adapter=newImageAdapter(this,ImageList);
  68. gallery.setAdapter(adapter);
  69. gallery.setOnItemLongClickListener(longlistener);
  70. }
  71. /**
  72. *Gallery长按事件操作实现
  73. */
  74. privateOnItemLongClickListenerlonglistener=newOnItemLongClickListener(){
  75. @Override
  76. publicbooleanonItemLongClick(AdapterView<?>parent,Viewview,
  77. finalintposition,longid){
  78. //此处添加长按事件删除照片实现
  79. AlertDialog.Builderdialog=newAlertDialog.Builder(PhotoScanActivity.this);
  80. dialog.setIcon(R.drawable.warn);
  81. dialog.setTitle("删除提示");
  82. dialog.setMessage("你确定要删除这张照片吗?");
  83. dialog.setPositiveButton("确定",newDialogInterface.OnClickListener(){
  84. @Override
  85. publicvoidonClick(DialogInterfacedialog,intwhich){
  86. Filefile=newFile(it.get(position));
  87. booleanisSuccess;
  88. if(file.exists()){
  89. isSuccess=file.delete();
  90. if(isSuccess){
  91. ImageList.remove(position);
  92. adapter.notifyDataSetChanged();
  93. //gallery.setAdapter(adapter);
  94. if(ImageList.size()==0){
  95. Toast.makeText(getApplicationContext(),getResources().getString(R.string.phoSizeZero),Toast.LENGTH_SHORT).show();
  96. }
  97. Toast.makeText(getApplicationContext(),getResources().getString(R.string.phoDelSuccess),Toast.LENGTH_SHORT).show();
  98. }
  99. }
  100. }
  101. });
  102. dialog.setNegativeButton("取消",newDialogInterface.OnClickListener(){
  103. @Override
  104. publicvoidonClick(DialogInterfacedialog,intwhich){
  105. dialog.dismiss();
  106. }
  107. });
  108. dialog.create().show();
  109. returnfalse;
  110. }
  111. };
  112. /**
  113. *获取SD卡上的所有图片文件
  114. *@return
  115. */
  116. privateList<String>getSD(){
  117. /*设定目前所在路径*/
  118. FilefileK;
  119. it=newArrayList<String>();
  120. if("newadd".equals(shopType)){
  121. //如果是从查看本人新增列表项或商户列表项进来时
  122. fileK=newFile(ApplicationData.TEMP);
  123. }else{
  124. //此时为纯粹新增
  125. fileK=newFile(path);
  126. }
  127. File[]files=fileK.listFiles();
  128. if(files!=null&&files.length>0){
  129. for(Filef:files){
  130. if(getImageFile(f.getName())){
  131. it.add(f.getPath());
  132. OptionsbitmapFactoryOptions=newBitmapFactory.Options();
  133. //下面这个设置是将图片边界不可调节变为可调节
  134. bitmapFactoryOptions.inJustDecodeBounds=true;
  135. bitmapFactoryOptions.inSampleSize=5;
  136. intoutWidth=bitmapFactoryOptions.outWidth;
  137. intoutHeight=bitmapFactoryOptions.outHeight;
  138. floatimagew=150;
  139. floatimageh=150;
  140. intyRatio=(int)Math.ceil(bitmapFactoryOptions.outHeight
  141. /imageh);
  142. intxRatio=(int)Math
  143. .ceil(bitmapFactoryOptions.outWidth/imagew);
  144. if(yRatio>1||xRatio>1){
  145. if(yRatio>xRatio){
  146. bitmapFactoryOptions.inSampleSize=yRatio;
  147. }else{
  148. bitmapFactoryOptions.inSampleSize=xRatio;
  149. }
  150. }
  151. bitmapFactoryOptions.inJustDecodeBounds=false;
  152. bitmap=BitmapFactory.decodeFile(f.getPath(),
  153. bitmapFactoryOptions);
  154. //bitmap=BitmapFactory.decodeFile(f.getPath());
  155. srf=newSoftReference<Bitmap>(bitmap);
  156. imageCache.put(f.getName(),srf);
  157. }
  158. }
  159. }
  160. returnit;
  161. }
  162. /**
  163. *获取图片文件方法的具体实现
  164. *@paramfName
  165. *@return
  166. */
  167. privatebooleangetImageFile(StringfName){
  168. booleanre;
  169. /*取得扩展名*/
  170. Stringend=fName
  171. .substring(fName.lastIndexOf(".")+1,fName.length())
  172. .toLowerCase();
  173. /*按扩展名的类型决定MimeType*/
  174. if(end.equals("jpg")||end.equals("gif")||end.equals("png")
  175. ||end.equals("jpeg")||end.equals("bmp")){
  176. re=true;
  177. }else{
  178. re=false;
  179. }
  180. returnre;
  181. }
  182. publicclassImageAdapterextendsBaseAdapter{
  183. /*声明变量*/
  184. intmGalleryItemBackground;
  185. privateContextmContext;
  186. privateList<String>lis;
  187. /*ImageAdapter的构造符*/
  188. publicImageAdapter(Contextc,List<String>li){
  189. mContext=c;
  190. lis=li;
  191. TypedArraya=obtainStyledAttributes(R.styleable.Gallery);
  192. mGalleryItemBackground=a.getResourceId(R.styleable.Gallery_android_galleryItemBackground,0);
  193. a.recycle();
  194. }
  195. /*几定要重写的方法getCount,传回图片数目*/
  196. publicintgetCount(){
  197. returnlis.size();
  198. }
  199. /*一定要重写的方法getItem,传回position*/
  200. publicObjectgetItem(intposition){
  201. returnlis.get(position);
  202. }
  203. /*一定要重写的方法getItemId,传并position*/
  204. publiclonggetItemId(intposition){
  205. returnposition;
  206. }
  207. /*几定要重写的方法getView,传并几View对象*/
  208. publicViewgetView(intposition,ViewconvertView,ViewGroupparent){
  209. System.out.println("lis:"+lis);
  210. Filefile=newFile(it.get(position));
  211. SoftReference<Bitmap>srf=imageCache.get(file.getName());
  212. Bitmapbit=srf.get();
  213. ImageViewi=newImageView(mContext);
  214. i.setImageBitmap(bit);
  215. i.setScaleType(ImageView.ScaleType.FIT_XY);
  216. i.setLayoutParams(newGallery.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT,
  217. WindowManager.LayoutParams.WRAP_CONTENT));
  218. returni;
  219. }
  220. }
  221. }

上面两种方式第一种直接使用边界压缩,第二种在使用边界压缩的情况下间接的使用了软引用来避免OOM,但大家都知道,这些函数在完成decode后,最终都是通过java层的createBitmap来完成的,需要消耗更多内存,如果图片多且大,这种方式还是会引用OOM异常的,不着急,有的是办法解决,继续看,以下方式也大有妙用的:

            
  1. 1.InputStreamis=this.getResources().openRawResource(R.drawable.pic1);
  2. BitmapFactory.Optionsoptions=newBitmapFactory.Options();
  3. options.inJustDecodeBounds=false;
  4. options.inSampleSize=10;//width,hight设为原来的十分一
  5. Bitmapbtp=BitmapFactory.decodeStream(is,null,options);
  6. 2.if(!bmp.isRecycle()){
  7. bmp.recycle()//回收图片所占的内存
  8. system.gc()//提醒系统及时回收
  9. }

上面代码与下面代码大家可分开使用,也可有效缓解内存问题哦...吼吼...

  1. /**这个地方大家别搞混了,为了方便小马把两个贴一起了,使用的时候记得分开使用
  2. *以最省内存的方式读取本地资源的图片
  3. */
  4. publicstaticBitmapreadBitMap(Contextcontext,intresId){
  5. BitmapFactory.Optionsopt=newBitmapFactory.Options();
  6. opt.inPreferredConfig=Bitmap.Config.RGB_565;
  7. opt.inPurgeable=true;
  8. opt.inInputShareable=true;
  9. //获取资源图片
  10. InputStreamis=context.getResources().openRawResource(resId);
  11. returnBitmapFactory.decodeStream(is,null,opt);
  12. }

3:大家可以选择在合适的地方使用以下代码动态并自行显式调用GC来回收内存:

            
  1. if(bitmapObject.isRecycled()==false)//如果没有回收
  2. bitmapObject.recycle();

4:这个就好玩了,优化Dalvik虚拟机的堆内存分配,听着很强大,来看下具体是怎么一回事

对于Android平台来说,其托管层使用的Dalvik JavaVM从目前的表现来看还有很多地方可以优化处理,比如我们在开发一些大型游戏或耗资源的应用中可能考虑手动干涉GC处理,使用 dalvik.system.VMRuntime类提供的setTargetHeapUtilization方法可以增强程序堆内存的处理效率。当然具体原理我们可以参考开源工程,这里我们仅说下使用方法:代码如下:

            
  1. privatefinalstaticfloatTARGET_HEAP_UTILIZATION=0.75f;
  2. 在程序onCreate时就可以调用
  3. VMRuntime.getRuntime().setTargetHeapUtilization(TARGET_HEAP_UTILIZATION);
  4. 即可

5:自定义我们的应用需要多大的内存,这个好暴力哇,强行设置最小内存大小,代码如下:

            
  1. privatefinalstaticintCWJ_HEAP_SIZE=6*1024*1024;
  2. //设置最小heap内存为6MB大小
  3. VMRuntime.getRuntime().setMinimumHeapSize(CWJ_HEAP_SIZE);

好了,文章写完了,片幅有点长,因为涉及到的东西太多了,其它文章小马都会贴源码,这篇文章小马是直接在项目中用三款安卓真机测试的,有效果,项目原码就不在这贴了,不然泄密了都,吼吼,但这里讲下还是会因为手机的不同而不同,大家得根据自己需求选择合适的方式来避免OOM,大家加油呀,每天都有或多或少的收获,这也算是进步,加油加油!

本文出自 “酷_莫名简单” 博客,请务必保留此出处http://mzh3344258.blog.51cto.com/1823534/804237

更多相关文章

  1. 【Android】注解框架(四)-- 一行代码注入微信支付
  2. android 上 webkit js 扩展之全局本地对象实现步骤
  3. android通过代码来开启和关闭移动网络
  4. [android][利用JNI技术在Android中调用、调试C++代码]
  5. android内存管理
  6. 【Android 开发】: Android 消息处理机制之三: Handler 中 sendM

随机推荐

  1. android视频截屏&手机录屏实现
  2. android ResolveInfo运用
  3. Fragment Arguments
  4. 【android】错误集锦及解决办法
  5. android 系统级别开发环境搭建
  6. 关于开机启动遇到的问题
  7. Android(安卓)四种阴影实现方式对比
  8. Android(安卓)拍照裁剪图片不清晰解决办
  9. Android(安卓)- 数据存储(一)之 SharedPref
  10. Android(安卓)studio2.2 启动模拟器出现