20、内存溢出(Out of Memory)

 内存引用(释放强引用)

Object obj=new Object();

obj = null; 

 内存引用(使用软引用

        软引用是主要用于内存敏感的高速缓存。在jvm报告内存不足之前会清 除所有的软引用,这样以来gc就有可能收集软可及的对象,可能解决内存 吃紧问题,避免内存溢出。什么时候会被收集取决于gc的算法和gc运行时 可用内存的大小。

        软引用即使没有被引用,也不会释放,直到虚拟机报告内存不够才回 收,所以适合做Cache。 

String abc = “aaa”;
SoftReference<String> abcSoft=new SoftReference<String>(abc); 

 内存引用(使用弱引用)

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

String abc = “aaa”;
WeakReference<String> abcWea = new WeakReference<String>(abc); 

 图像处理(在内存中压缩图像)

Options bitmapFactoryOptions = new BitmapFactory.Options();

bitmapFactoryOptions.inSampleSize = 2;
bmap = BitmapFactory.decodeFile(picture.getAbsolutePath(),

bitmapFactoryOptions); 

 图像处理(回收图片所占的内存)

if(bitmapObject.isRecycled()==false) //如果没有回收

{
        bitmapObject.recycle();

       system.gc() //提醒系统及时回收

 VMRuntime 

private final static floatTARGET_HEAP_UTILIZATION = 0.75f;

VMRuntime.getRuntime().setTargetHeapUtilization(TARGET_HEAP_UTIL IZATION);

private final static int CWJ_HEAP_SIZE = 6* 1024* 1024 ;

//设置最小heap内存为6MB大小 VMRuntime.getRuntime().setMinimumHeapSize(CWJ_HEAP_SIZE);

从Android2.3以后,VMRuntime不再提供,不过Google表示以后可能会提 供VMRuntime。 

1 public class Memory {
2     public static long used() {
3         long total = Runtime.getRuntime().totalMemory();
4         long free = Runtime.getRuntime().freeMemory();
5         return (total - free);
6     }
7 }
 1 import java.util.ArrayList;
 2 import java.util.List;
 3 import android.app.Activity;
 4 import android.os.Bundle;
 5 import android.view.View;
 6 import android.widget.Toast;
 7 
 8 public class Main extends Activity {
 9 
10     private List<Integer> list1 = new ArrayList<Integer>();
11 
12     @Override
13     public void onCreate(Bundle savedInstanceState) {
14         super.onCreate(savedInstanceState);
15         setContentView(R.layout.main);
16     }
17 
18     public void test1() {
19         for (int i = 0; i < 10000; i++) {
20             list1.add(i);
21         }
22     }
23 
24     public void test2() {
25         for (int i = 0; i < 10000; i++)
26             list1.get(i);
27     }
28 
29     public void onClick_Test(View view) {
30         try {
31             // 获取调用test1方法之前的内存
32             long start1 = Memory.used();
33             // 调用test1方法
34             test1();
35             // 获取调用test1方法之后的内存
36             long end1 = Memory.used();
37             // 获取调用test2方法之前的内存
38             long start2 = Memory.used();
39             // 调用test2方法
40             test2();
41             // 获取调用test2方法之后的内存
42             long end2 = Memory.used();
43             // 显示内存测试结果
44             Toast.makeText(
45                     this,
46                     "test1方法占用的内存:" + (end1 - start1) + "字节
test2方法的占用的内存:"
47                             + (end2 - start2) + "字节", Toast.LENGTH_LONG).show();
48         } catch (Exception e) {
49         }
50 
51     }
52 }
原文地址:https://www.cnblogs.com/androidsj/p/3929982.html