關(guān)于是否可以try catch OutOfMemoryError的討論
目錄
[TOC]
問題由來
這是一家公司的面試題目,感覺有點意思,所以面試回來準(zhǔn)備測試下什么情況
問題論點
對于這個問題,主要討論兩種OutOfMemory可能性,一種是突然使用了大量內(nèi)存,比如加載了特別巨大的圖片,第二是內(nèi)存泄漏.
然后還有個問題是,一旦發(fā)生OOM,引發(fā)OOM的操作是否會成功,如果會成功賦值是否會成功呢?理論上操作和賦值都不會成功的,但是我覺得有必要嘗試一下.
構(gòu)建測試代碼
那么針對問題構(gòu)建測試代碼如下
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<Button
android:id="@+id/out"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:padding="10dp"
android:text="變量在try外面"
android:textSize="15sp" />
<Button
android:id="@+id/in"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:padding="10dp"
android:text="變量在try里面"
android:textSize="15sp" />
<Button
android:id="@+id/add"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:padding="10dp"
android:text="添加數(shù)組"
android:textSize="15sp" />
<Button
android:id="@+id/action"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:padding="10dp"
android:text="其他正常操作"
android:textSize="15sp" />
<Button
android:id="@+id/gc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:padding="10dp"
android:text="垃圾回收"
android:textSize="15sp" />
<TextView
android:id="@+id/left"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:padding="10dp"
android:textSize="15sp" />
</LinearLayout>
MainActivity.java
package com.yxf.trytocatchoutofmemory;
import android.app.ActivityManager;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private static final String TAG = MainActivity.class.getSimpleName();
private ArrayList<byte[]> mArrayList = new ArrayList<byte[]>();
private Button mOutButton, mInButton, mAddButton, mActionButton, mGcButton;
private TextView mLeftMemoryView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mOutButton = findViewById(R.id.out);
mInButton = findViewById(R.id.in);
mAddButton = findViewById(R.id.add);
mActionButton = findViewById(R.id.action);
mGcButton = findViewById(R.id.gc);
mLeftMemoryView = findViewById(R.id.left);
mOutButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
byte[] values = null;
try {
values = new byte[1024 * 1024 * 1024];
} catch (OutOfMemoryError error) {
error.printStackTrace();
}
if (values == null) {
Log.d(TAG, "onClick: values is null");
} else {
Log.d(TAG, "onClick: values not null");
}
updateLeftMemoryView();
}
});
mInButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
byte[] values = new byte[1024 * 1024 * 1024];
} catch (OutOfMemoryError error) {
error.printStackTrace();
}
updateLeftMemoryView();
}
});
mAddButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
mArrayList.add(new byte[1024 * 1024 * 10]);
} catch (OutOfMemoryError error) {
error.printStackTrace();
}
updateLeftMemoryView();
}
});
mActionButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
byte[] bytes = new byte[1024 * 1024 * 10];
updateLeftMemoryView();
}
});
mGcButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
System.gc();
updateLeftMemoryView();
}
});
updateLeftMemoryView();
}
private void updateLeftMemoryView() {
String s = String.format("內(nèi)存總計 : %#.3f , 剩余 : %#.3f", (Runtime.getRuntime().totalMemory() * 1.0 / 1024 / 1024), (Runtime.getRuntime().freeMemory() * 1.0 / 1024 / 1024));
mLeftMemoryView.setText(s);
}
}
測試界面
測試情況
變量在try外面
log如下
D: JIT code cache reset in 0 ms (0 bytes 1/0)
GC_FOR_ALLOC freed 254K, 10% free 3058K/3376K, paused 10ms, total 12ms
I: Forcing collection of SoftReferences for 1073741836-byte allocation
D: GC_BEFORE_OOM freed 2K, 10% free 3056K/3376K, paused 9ms, total 12ms
E: Out of memory on a 1073741836-byte allocation.
I: "main" prio=5 tid=1 RUNNABLE
| group="main" sCount=0 dsCount=0 obj=0x94c64bd8 self=0xb90c6500
| sysTid=1571 nice=0 sched=0/0 cgrp=[fopen-error:2] handle=-1216952576
| state=R schedstat=( 0 0 0 ) utm=45 stm=23 core=0
at com.yxf.trytocatchoutofmemory.MainActivity$1.onClick(MainActivity.java:~37)
at android.view.View.performClick(View.java:4438)
at android.view.View$PerformClick.run(View.java:18422)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5019)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
W: java.lang.OutOfMemoryError
at com.yxf.trytocatchoutofmemory.MainActivity$1.onClick(MainActivity.java:37)
at android.view.View.performClick(View.java:4438)
at android.view.View$PerformClick.run(View.java:18422)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5019)
at java.lang.reflect.Method.invokeNative(Native Method)
W: at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
D: onClick: values is null
實際上這個OutOfMemoryError成功被catch到了,而且程序并沒有崩潰.
程序中的剩余內(nèi)存等也沒有發(fā)生太大變化,這說明OutOfMemoryError觸發(fā),其實并不會真正分配內(nèi)存.
而且從上面的日志信息上也可以了解到一些信息
比如:
- 在分配可能會導(dǎo)致OutOfMemoryError的內(nèi)存前,會先做一次垃圾回收
- 在做這次垃圾回收時會把SoftReference也回收掉
- 變量并沒有被賦值成功
變量在Try里面
這個測試是為了測試是否會成功創(chuàng)建并且賦值的,由于上面結(jié)論已經(jīng)確定不行了,所以這個測試其實失去了意義.
添加數(shù)組測試
這個測試是模擬內(nèi)存泄漏的,類比反復(fù)的啟動一個被其他類引用的activity.
這里是一直給一個ArrayList添加數(shù)據(jù),直到接近OutOfMemory.
結(jié)果如圖
從圖中可知這個也可以catch到OutOfMemoryError
而且還可以了解到其他信息:
- 應(yīng)用分配的總內(nèi)存并不是固定的,而是會根據(jù)使用情況增長的,而且這個總內(nèi)存是會根據(jù)手機ram改變的,模擬器2g內(nèi)存最大180mb,我真機6g內(nèi)存,最大380mb.
說個題外話,那么我們手機內(nèi)存明明都那么大了,卻依然還是卡的原因出來了!!!無良App就是,老子最重要,老子性能,體驗都要最好,然后無良App們就通過什么LruCache各種使用強引用緩存,占著內(nèi)存不放,然后它真的實現(xiàn)了,內(nèi)存都給它霸占著,系統(tǒng)是卡,其他后面開的App也卡,但是它就是不卡,23333333.
其他正常操作測試
這個測試是用來證明catch內(nèi)存泄漏導(dǎo)致的oom是否有意義的
測試結(jié)果如下
這個操作只是一個正常而又簡單的分配一個byte數(shù)組的操作,但是程序崩了.
由于之前的內(nèi)存泄漏已經(jīng)導(dǎo)致內(nèi)存幾乎使用完了,catch OutOfMemoryError雖然成功,但是并沒有意義,因為任何一個接下來的操作都可能依然會導(dǎo)致OutOfMemoryError的出現(xiàn).
總結(jié)
OutOfMemoryError不應(yīng)該去catch,出現(xiàn)OutOfMemoryError不管是因為一次巨大的內(nèi)存分配還是內(nèi)存泄漏導(dǎo)致,都是程序設(shè)計的問題,如果是大內(nèi)存操作,應(yīng)該想辦法一點點加載,或者壓縮資源來加載,如果是巨大數(shù)量的排序問題,則可以選擇外排序的方式進行,如果是內(nèi)存泄漏,則需要尋找程序自身的設(shè)計問題.