2013-03-05 89 views
1

我已經制作了一個全屏的點擊型應用程序,它有6個屏幕。該應用程序從屏幕上點擊一下。每個屏幕都有自己的活動(步驟1,步驟2,步驟3等到步驟6)。沒有什麼複雜的,佈局非常簡單,只有一個背景圖片和一個按鈕。當問題開始時,我開始關注屏幕3,突然顯示下一個屏幕,但是應用程序在內存不足錯誤中崩潰。我試過移動到屏幕4,5,6,沒有運氣,但如果我移回來從屏幕3說屏幕2,那麼它的工作原理!有東西泄漏,但我不知道是什麼。背景圖像很小。安卓內存不足setContentView

我活動之間進行切換使用此代碼:

Intent myIntent = new Intent(Step3.this, Step6.class); 
Step3.this.startActivity(myIntent); 
System.gc(); 

這是日誌:

03-05 12:38:44.090: D/dalvikvm(22184): WAIT_FOR_CONCURRENT_GC blocked 0ms 
03-05 12:38:44.115: D/dalvikvm(22184): GC_EXPLICIT freed 3622K, 8% free 55598K/59911K, paused 1ms+3ms, total 22ms 
03-05 12:39:33.965: D/dalvikvm(22184): WAIT_FOR_CONCURRENT_GC blocked 0ms 
03-05 12:39:34.040: D/dalvikvm(22184): GC_EXPLICIT freed 31K, 8% free 55633K/59911K, paused 11ms+5ms, total 74ms 
03-05 12:39:34.040: I/Choreographer(22184): Skipped 2910 frames! The application may be doing too much work on its main thread. 
03-05 12:39:43.850: D/dalvikvm(22184): GC_FOR_ALLOC freed 34K, 8% free 55613K/59911K, paused 23ms, total 24ms 
03-05 12:39:43.860: I/dalvikvm-heap(22184): Grow heap (frag case) to 58.364MB for 3686416-byte allocation 
03-05 12:39:43.900: D/dalvikvm(22184): GC_CONCURRENT freed <1K, 7% free 59213K/63559K, paused 14ms+5ms, total 39ms 
03-05 12:39:43.935: D/dalvikvm(22184): GC_FOR_ALLOC freed 0K, 7% free 59213K/63559K, paused 18ms, total 18ms 
03-05 12:39:43.935: I/dalvikvm-heap(22184): Forcing collection of SoftReferences for 14745616-byte allocation 
03-05 12:39:43.955: D/dalvikvm(22184): GC_BEFORE_OOM freed <1K, 7% free 59213K/63559K, paused 18ms, total 19ms 
03-05 12:39:43.955: E/dalvikvm-heap(22184): Out of memory on a 14745616-byte allocation. 
03-05 12:39:43.955: I/dalvikvm(22184): "main" prio=5 tid=1 RUNNABLE 
03-05 12:39:43.955: I/dalvikvm(22184): | group="main" sCount=0 dsCount=0 obj=0x413f0508 self=0x413e0468 
03-05 12:39:43.955: I/dalvikvm(22184): | sysTid=22184 nice=0 sched=0/0 cgrp=apps handle=1074609968 
03-05 12:39:43.955: I/dalvikvm(22184): | schedstat=(1501533493 98715963 900) utm=116 stm=33 core=3 
03-05 12:39:43.955: I/dalvikvm(22184): at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method) 
03-05 12:39:43.955: I/dalvikvm(22184): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:623) 
03-05 12:39:43.955: I/dalvikvm(22184): at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:476) 
03-05 12:39:43.955: I/dalvikvm(22184): at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:781) 
03-05 12:39:43.955: I/dalvikvm(22184): at android.content.res.Resources.loadDrawable(Resources.java:1963) 
03-05 12:39:43.955: I/dalvikvm(22184): at android.content.res.TypedArray.getDrawable(TypedArray.java:601) 
03-05 12:39:43.955: I/dalvikvm(22184): at android.widget.ImageView.<init>(ImageView.java:120) 
03-05 12:39:43.960: I/dalvikvm(22184): at android.widget.ImageView.<init>(ImageView.java:110) 
03-05 12:39:43.960: I/dalvikvm(22184): at java.lang.reflect.Constructor.constructNative(Native Method) 
03-05 12:39:43.960: I/dalvikvm(22184): at java.lang.reflect.Constructor.newInstance(Constructor.java:417) 
03-05 12:39:43.960: I/dalvikvm(22184): at android.view.LayoutInflater.createView(LayoutInflater.java:587) 
03-05 12:39:43.960: I/dalvikvm(22184): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56) 
03-05 12:39:43.960: I/dalvikvm(22184): at android.view.LayoutInflater.onCreateView(LayoutInflater.java:660) 
03-05 12:39:43.960: I/dalvikvm(22184): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685) 
03-05 12:39:43.960: I/dalvikvm(22184): at android.view.LayoutInflater.rInflate(LayoutInflater.java:746) 
03-05 12:39:43.960: I/dalvikvm(22184): at android.view.LayoutInflater.inflate(LayoutInflater.java:489) 
03-05 12:39:43.960: I/dalvikvm(22184): at android.view.LayoutInflater.inflate(LayoutInflater.java:396) 
03-05 12:39:43.960: I/dalvikvm(22184): at android.view.LayoutInflater.inflate(LayoutInflater.java:352) 
03-05 12:39:43.960: I/dalvikvm(22184): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:308) 
03-05 12:39:43.960: I/dalvikvm(22184): at android.app.Activity.setContentView(Activity.java:1924) 
03-05 12:39:43.960: I/dalvikvm(22184): at com.frogr4g.networktrial.Step6.onCreate(Step6.java:11) 
03-05 12:39:43.960: I/dalvikvm(22184): at android.app.Activity.performCreate(Activity.java:5206) 
03-05 12:39:43.960: I/dalvikvm(22184): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1083) 
03-05 12:39:43.960: I/dalvikvm(22184): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064) 
03-05 12:39:43.960: I/dalvikvm(22184): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125) 
03-05 12:39:43.960: I/dalvikvm(22184): at android.app.ActivityThread.access$600(ActivityThread.java:140) 
03-05 12:39:43.960: I/dalvikvm(22184): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227) 
03-05 12:39:43.960: I/dalvikvm(22184): at android.os.Handler.dispatchMessage(Handler.java:99) 
03-05 12:39:43.960: I/dalvikvm(22184): at android.os.Looper.loop(Looper.java:137) 
03-05 12:39:43.960: I/dalvikvm(22184): at android.app.ActivityThread.main(ActivityThread.java:4898) 
03-05 12:39:43.960: I/dalvikvm(22184): at java.lang.reflect.Method.invokeNative(Native Method) 
03-05 12:39:43.960: I/dalvikvm(22184): at java.lang.reflect.Method.invoke(Method.java:511) 
03-05 12:39:43.960: I/dalvikvm(22184): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006) 
03-05 12:39:43.960: I/dalvikvm(22184): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773) 
03-05 12:39:43.960: I/dalvikvm(22184): at dalvik.system.NativeStart.main(Native Method) 
03-05 12:39:43.960: A/libc(22184): Fatal signal 11 (SIGSEGV) at 0x00000000 (code=1), thread 22184 (4g.networktrial) 
03-05 12:39:57.150: D/dalvikvm(22766): GC_FOR_ALLOC freed 64K, 6% free 12115K/12867K, paused 34ms, total 35ms 
03-05 12:39:57.155: I/dalvikvm-heap(22766): Grow heap (frag case) to 15.883MB for 3686416-byte allocation 
03-05 12:39:57.170: D/dalvikvm(22766): GC_CONCURRENT freed <1K, 5% free 15714K/16519K, paused 3ms+1ms, total 14ms 

編輯:我創建的六個屏幕類似的應用程序,只需用的TextView和一個按鈕和導航鏈(1 - > 2 - > 3 - > 4 - > 5 - > 6 - > 1),沒有任何圖像,它的作品像黃油。 (如預期)。所以這絕對是與圖像佔用大量內存有關。

+0

如果您不必返回,您可以始終在開始新活動之前完成當前活動。 – Niko 2013-03-05 11:55:37

+0

使用Memory Analyzer工具查找內存泄漏的位置。如果沒有看到代碼,很難判斷問題出在哪裏。 – tundundun 2013-03-05 11:53:10

+0

tundundun,將不得不看看,謝謝你的提示。看起來有點嚇人。 – Pompair 2013-03-05 13:46:36

回答

1

recycle()你的位圖,只要你離開一個活動去其他和重新排列代碼:

bitmap.recycle(); 
System.gc(); 
Intent myIntent = new Intent(Step3.this, Step6.class); 
Step3.this.startActivity(myIntent); 
+1

謝謝,但我沒有使用位圖,而是使用圖像瀏覽。 – Pompair 2013-03-05 13:03:40

2

有幾件事情來看待。

  • 你提供了幾種密度的背景圖嗎?縮放有時會消耗大量內存。
  • 您是否使用startActivitystartActivityForResult()?在後一種情況下,舊的Activity對象不能被銷燬。
  • 您是否收集Application課程中的信息?
+0

不,我沒有提供幾種密度。我正在使用startActivity。我沒有使用Application類來存儲任何變量,如果這就是你的意思。 – Pompair 2013-03-05 13:46:03

+0

@Pompair你的圖像大小是多少,格式是什麼,你是否暗示要求它們縮放?當Android不得不縮放它時,即使是相對較小的JPG也會消耗相當多的內存。提供不同密度的圖像可能有助於避免這種情況。 – 2013-03-05 14:19:54

+0

Class Stacker,圖像是720x1280 32位PNG,每個大約20KB。我不知道縮放的隱含請求的含義是什麼意思......我使用默認的imageview設置來適應活動的大小。如果我想在三星S3上運行,我應該放置哪個文件夾? png是應該去的drawable-hdpi文件夾...? – Pompair 2013-03-05 15:02:37