Android 内存优化测试(转)
今天做了個內存優化的小測試,包括兩點
?
1,??比較Drawable與Bitmap占用內存大小
2,??比較BitmapFactory類的decodeResource方法與decodeStream方法的效率
?
?
?
?
好吧,先來看第1個測試!
以下這個是測試加載1000個Drawable對象的代碼,很簡單的,我就不解釋了!
?
public?class?Main?extends?Activity
{
????int?number?= 1000;
????Drawable[]?array;
?
????@Override
????public?void?onCreate(Bundle savedInstanceState)
????{
???????super.onCreate(savedInstanceState);
???????setContentView(R.layout.main);
?
????????array?=?new?BitmapDrawable[number];
?????????????????????
????????for(int?i = 0; i <?number; i++)
????????{
????????????Log.e("",?"測試第"?+ (i+1) +?"張圖片");
????????????array[i] = getResources().getDrawable(R.drawable.img);
????????}
????}
}
?
?
輸出結果:
04-07 21:49:25.248: D/szipinf(7828): Initializing inflate state
04-07 21:49:25.398: E/(7828):?測試第1張圖片
04-07 21:49:25.658: D/dalvikvm(7828): GC_EXTERNAL_ALLOC freed 48K, 50% free 2692K/5379K, external 0K/0K, paused 24ms
04-07 21:49:25.748: E/(7828):?測試第2張圖片
04-07 21:49:25.748: E/(7828):?測試第3張圖片
………………
………………
04-07 21:49:26.089: E/(7828):?測試第998張圖片
04-07 21:49:26.089: E/(7828):?測試第999張圖片
04-07 21:49:26.089: E/(7828):?測試第1000張圖片
?
程序沒有報錯,正常運行,加載1000個Drawable對象沒問題。
?
下面再來看一下加載1000個Bitmap對象的代碼,同樣的,代碼很簡單的,我就不解釋了!
public?class?Main?extends?Activity
{
????int?number?= 1000;
????Bitmap bitmap[];
?
????@Override
????public?void?onCreate(Bundle savedInstanceState)
????{
???????super.onCreate(savedInstanceState);
???????setContentView(R.layout.main);
?
???????bitmap?=?new?Bitmap[number];
?
???????for?(int?i = 0; i <?number; i++)
???????{
???????????Log.e("",?"測試第"?+ (i+1) +?"張圖片");
???????????bitmap[i] = BitmapFactory.decodeResource(getResources(), R.drawable.img);
???????}
????}
}
?
輸出結果:
04-07 22:06:05.344: D/szipinf(7937): Initializing inflate state
04-07 22:06:05.374: E/(7937):?測試第1張圖片
04-07 22:06:05.544: D/dalvikvm(7937): GC_EXTERNAL_ALLOC freed 51K, 50% free 2692K/5379K, external 0K/0K, paused 40ms
04-07 22:06:05.664: E/(7937):?測試第2張圖片
04-07 22:06:05.774: D/dalvikvm(7937): GC_EXTERNAL_ALLOC freed 1K, 50% free 2691K/5379K, external 6026K/7525K, paused 31ms
04-07 22:06:05.834: E/(7937):?測試第3張圖片
04-07 22:06:05.934: D/dalvikvm(7937): GC_EXTERNAL_ALLOC freed <1K, 50% free 2691K/5379K, external 12052K/14100K, paused 24ms
04-07 22:06:06.004: E/(7937):?測試第4張圖片
04-07 22:06:06.124: D/dalvikvm(7937): GC_EXTERNAL_ALLOC freed <1K, 50% free 2691K/5379K, external 18078K/20126K, paused 27ms
04-07 22:06:06.204: E/(7937):?測試第5張圖片
04-07 22:06:06.315: D/dalvikvm(7937): GC_EXTERNAL_ALLOC freed <1K, 50% free 2691K/5379K, external 24104K/26152K, paused 26ms
04-07 22:06:06.395: E/(7937):?測試第6張圖片
04-07 22:06:06.495: D/dalvikvm(7937): GC_EXTERNAL_ALLOC freed <1K, 50% free 2691K/5379K, external 30130K/32178K, paused 22ms
04-07 22:06:06.565: E/(7937):?測試第7張圖片
04-07 22:06:06.665: D/dalvikvm(7937): GC_EXTERNAL_ALLOC freed <1K, 50% free 2691K/5379K, external 36156K/38204K, paused 22ms
04-07 22:06:06.745: E/(7937):?測試第8張圖片
04-07 22:06:06.845: D/dalvikvm(7937): GC_EXTERNAL_ALLOC freed 2K, 51% free 2689K/5379K, external 42182K/44230K, paused 23ms
04-07 22:06:06.845: E/dalvikvm-heap(7937): 6170724-byte external allocation too large for this process.
04-07 22:06:06.885: I/dalvikvm-heap(7937): Clamp target GC heap from 48.239MB to 48.000MB
04-07 22:06:06.885: E/GraphicsJNI(7937): VM won't let us allocate 6170724 bytes
04-07 22:06:06.885: D/dalvikvm(7937): GC_FOR_MALLOC freed <1K, 51% free 2689K/5379K, external 42182K/44230K, paused 25ms
04-07 22:06:06.885: D/AndroidRuntime(7937): Shutting down VM
04-07 22:06:06.885: W/dalvikvm(7937): threadid=1: thread exiting with uncaught exception (group=0x40015560)
04-07 22:06:06.885: E/AndroidRuntime(7937): FATAL EXCEPTION: main
04-07 22:06:06.885: E/AndroidRuntime(7937): java.lang.OutOfMemoryError: bitmap size exceeds VM budget
04-07 22:06:06.885: E/AndroidRuntime(7937):???at android.graphics.Bitmap.nativeCreate(Native Method)
04-07 22:06:06.885: E/AndroidRuntime(7937):???at android.graphics.Bitmap.createBitmap(Bitmap.java:477)
04-07 22:06:06.885: E/AndroidRuntime(7937):???at android.graphics.Bitmap.createBitmap(Bitmap.java:444)
04-07 22:06:06.885: E/AndroidRuntime(7937):???at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:349)
04-07 22:06:06.885: E/AndroidRuntime(7937):???at android.graphics.BitmapFactory.finishDecode(BitmapFactory.java:498)
04-07 22:06:06.885: E/AndroidRuntime(7937):???at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:473)
04-07 22:06:06.885: E/AndroidRuntime(7937):???at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:336)
04-07 22:06:06.885: E/AndroidRuntime(7937):???at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:359)
04-07 22:06:06.885: E/AndroidRuntime(7937):???at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:385)
04-07 22:06:06.885: E/AndroidRuntime(7937):???at bassy.test.drawable.Main.onCreate(Main.java:37)
04-07 22:06:06.885: E/AndroidRuntime(7937):???at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-07 22:06:06.885: E/AndroidRuntime(7937):???at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1722)
04-07 22:06:06.885: E/AndroidRuntime(7937):???at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1784)
04-07 22:06:06.885: E/AndroidRuntime(7937):???at android.app.ActivityThread.access$1500(ActivityThread.java:123)
04-07 22:06:06.885: E/AndroidRuntime(7937):???at android.app.ActivityThread$H.handleMessage(ActivityThread.java:939)
04-07 22:06:06.885: E/AndroidRuntime(7937):???at android.os.Handler.dispatchMessage(Handler.java:99)
04-07 22:06:06.885: E/AndroidRuntime(7937):???at android.os.Looper.loop(Looper.java:130)
04-07 22:06:06.885: E/AndroidRuntime(7937):???at android.app.ActivityThread.main(ActivityThread.java:3835)
04-07 22:06:06.885: E/AndroidRuntime(7937):???at java.lang.reflect.Method.invokeNative(Native Method)
04-07 22:06:06.885: E/AndroidRuntime(7937):???at java.lang.reflect.Method.invoke(Method.java:507)
04-07 22:06:06.885: E/AndroidRuntime(7937):???at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847)
04-07 22:06:06.885: E/AndroidRuntime(7937):???at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
04-07 22:06:06.885: E/AndroidRuntime(7937):???at dalvik.system.NativeStart.main(Native Method)
?
?
看看上面的輸出,才加載到第8張圖片,程序就報錯了“java.lang.OutOfMemoryError: bitmap size exceeds VM budget”。
?
通過上面的例子,可以看清楚地看出來,使用Drawable保存圖片對象,占用更小的內存空間。
而使用Biamtp對象,則會占用很大內存空間,很容易就出現OOM了!
?
下面我們再來看一個例子,這個也是加載Bitmap對象。
只不過,之次不是使用BitmapFactory的decodeResource方法,
而是使用decodeStream方法,看代碼。
public?class?Main?extends?Activity
{
????int?number?= 1000;
????Bitmap bitmap[];
?
????@Override
????public?void?onCreate(Bundle savedInstanceState)
????{
???????super.onCreate(savedInstanceState);
???????setContentView(R.layout.main);
?
???????bitmap?=?new?Bitmap[number];
?
???????for?(int?i = 0; i <?number; i++)
???????{
???????????Log.e("",?"測試第"?+ (i+1) +?"張圖片");
???????????bitmap[i] =BitmapFactory.decodeStream(getResources().openRawResource(R.drawable.img));//這里換了方法
???????}
????}
}
?
?
輸出結果:
04-07 22:16:12.676: E/(8091):?測試第561張圖片
04-07 22:16:12.756: E/(8091):?測試第562張圖片
04-07 22:16:12.826: E/(8091):?測試第563張圖片
04-07 22:16:12.906: E/(8091):?測試第564張圖片
04-07 22:16:12.906: D/skia(8091): ---------- mmap failed for imageref_ashmem size=2744320 err=12
04-07 22:16:12.906: E/(8091):?測試第565張圖片
04-07 22:16:12.906: D/skia(8091): ---------- mmap failed for imageref_ashmem size=2744320 err=12
04-07 22:16:12.906: E/(8091):?測試第566張圖片
04-07 22:16:12.916: E/filemap(8091): mmap(0,416798) failed: Out of memory
04-07 22:16:12.916: D/filemap(8091): munmap(0x0, 0) failed
04-07 22:16:12.916: W/asset(8091): create map from entry failed
04-07 22:16:12.916: D/AndroidRuntime(8091): Shutting down VM
04-07 22:16:12.916: W/dalvikvm(8091): threadid=1: thread exiting with uncaught exception (group=0x40015560)
04-07 22:16:12.936: E/AndroidRuntime(8091): FATAL EXCEPTION: main
04-07 22:16:12.936: E/AndroidRuntime(8091): java.lang.RuntimeException: Unable to start activity ComponentInfo{bassy.test.drawable/bassy.test.drawable.Main}: android.content.res.Resources$NotFoundException: File res/drawable-mdpi/img.png from drawable resource ID #0x7f020001
04-07 22:16:12.936: E/AndroidRuntime(8091):???at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1768)
04-07 22:16:12.936: E/AndroidRuntime(8091):???at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1784)
04-07 22:16:12.936: E/AndroidRuntime(8091):???at android.app.ActivityThread.access$1500(ActivityThread.java:123)
04-07 22:16:12.936: E/AndroidRuntime(8091):???at android.app.ActivityThread$H.handleMessage(ActivityThread.java:939)
04-07 22:16:12.936: E/AndroidRuntime(8091):???at android.os.Handler.dispatchMessage(Handler.java:99)
04-07 22:16:12.936: E/AndroidRuntime(8091):???at android.os.Looper.loop(Looper.java:130)
04-07 22:16:12.936: E/AndroidRuntime(8091):???at android.app.ActivityThread.main(ActivityThread.java:3835)
04-07 22:16:12.936: E/AndroidRuntime(8091):???at java.lang.reflect.Method.invokeNative(Native Method)
04-07 22:16:12.936: E/AndroidRuntime(8091):???at java.lang.reflect.Method.invoke(Method.java:507)
04-07 22:16:12.936: E/AndroidRuntime(8091):???at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847)
04-07 22:16:12.936: E/AndroidRuntime(8091):???at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
04-07 22:16:12.936: E/AndroidRuntime(8091):???at dalvik.system.NativeStart.main(Native Method)
04-07 22:16:12.936: E/AndroidRuntime(8091): Caused by: android.content.res.Resources$NotFoundException: File res/drawable-mdpi/img.png from drawable resource ID #0x7f020001
04-07 22:16:12.936: E/AndroidRuntime(8091):???at android.content.res.Resources.openRawResource(Resources.java:860)
04-07 22:16:12.936: E/AndroidRuntime(8091):???at android.content.res.Resources.openRawResource(Resources.java:836)
04-07 22:16:12.936: E/AndroidRuntime(8091):???at bassy.test.drawable.Main.onCreate(Main.java:43)
04-07 22:16:12.936: E/AndroidRuntime(8091):???at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-07 22:16:12.936: E/AndroidRuntime(8091):???at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1722)
04-07 22:16:12.936: E/AndroidRuntime(8091):???... 11 more
04-07 22:16:12.936: E/AndroidRuntime(8091): Caused by: java.io.FileNotFoundException: res/drawable-mdpi/img.png
04-07 22:16:12.936: E/AndroidRuntime(8091):???at android.content.res.AssetManager.openNonAssetNative(Native Method)
04-07 22:16:12.936: E/AndroidRuntime(8091):???at android.content.res.AssetManager.openNonAsset(AssetManager.java:429)
04-07 22:16:12.936: E/AndroidRuntime(8091):???at android.content.res.Resources.openRawResource(Resources.java:857)
04-07 22:16:12.936: E/AndroidRuntime(8091):???... 15 more
?
從上面可以看出,程序在加載到第566張的時候,就出現了OOM錯誤。
不過,跟第2個例子比起來,你會發現,程序可以加載更多的圖片。
這說明了使用BitmapFactory的decodeResource方法會占據大量內存,
而使用使用decodeStream方法,則占據更小的內存。
?
從時間上來說,看看日志輸出,大概估算了一下加載一張圖片所需要的時間,發現,
decodeResource加載圖片需要約0.17秒的時間,
而使用decodeStream方法,只需要約0.08秒的時間!
這說明了,decodeStream無論是時間上還是空間上,都比decodeResource方法更優秀!!
?
?
從上面三個例子,可以看出,用第一種方法(即用Drawable加載圖片)可以加載更加的圖片,加載32張圖片的時間約為0.01秒!
我試著把Drawable的數量調至1000000,程序在運行時,停在了153761張圖片里,手機提示,“應用程序無響應…”
個人猜測,Drawable應該不屬于常駐內存的對象,不然的話,不可能不會出現OOM的~~
?
網上關于Drawable與Bitmap的資料太少,不能深入學習,真是遺憾~
?
?
剛才又做了個測試,把第一個例子中的
array[i] = getResources().getDrawable(R.drawable.img);
方法換成了
array[i] = Drawable.createFromStream(getResources().openRawResource(R.drawable.img), null);
結果和第三個例子一樣,在第566張圖片中,出現了OOM錯誤!
而且,加載的時間都是一樣~~
這樣一來,我就更加迷惑了~~
?
轉載于:https://www.cnblogs.com/siyiganshou/archive/2012/08/10/2631755.html
總結
以上是生活随笔為你收集整理的Android 内存优化测试(转)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mcf5271如何使用数学库函数
- 下一篇: 在Linux下开发多语言软件: Hell