2012-04-25 61 views
26

在我的應用程序中,當我嘗試啓動它強制關閉和錯誤指向行「setContentView(R.layout.Menu);」的佈局。在XML文件中,它顯示了我的佈局中的「OutOfMemoryError」圖像視圖。我真的很困惑。請引導我進一步行動。引起:java.lang.OutOfMemoryError:位圖大小超過VM預算

編輯:

我的應用程序使用的數據庫,並在第一次就解析某些XML數據並將其插入SQLite數據庫。我的Outofmemory問題僅在第一時間發生。第二次,它工作正常。我試過System.gc()。那有沒有什麼可能。

這是我的日誌:

E/dalvikvm-heap(2712): 105376-byte external allocation too large for this process. 
VM won't let us allocate 105376 bytes 

    FATAL EXCEPTION: main 
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.Test/com.Test.Menu}: android.view.InflateException: Binary XML file line #13: Error inflating class <unknown> 
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647) 
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663) 
    at android.app.ActivityThread.access$1500(ActivityThread.java:117) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931) 
    at android.os.Handler.dispatchMessage(Handler.java:99) 
    at android.os.Looper.loop(Looper.java:130) 
    at android.app.ActivityThread.main(ActivityThread.java:3683) 
    at java.lang.reflect.Method.invokeNative(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:507) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 
    at dalvik.system.NativeStart.main(Native Method) 
Caused by: android.view.InflateException: Binary XML file line #13: Error inflating class <unknown> 
    at android.view.LayoutInflater.createView(LayoutInflater.java:518) 
    at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56) 
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:568) 
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:623) 
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:626) 
    at android.view.LayoutInflater.inflate(LayoutInflater.java:408) 
    at android.view.LayoutInflater.inflate(LayoutInflater.java:320) 
    at android.view.LayoutInflater.inflate(LayoutInflater.java:276) 
    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:207) 
    at android.app.Activity.setContentView(Activity.java:1657) 
    at com.Test.Menu.onCreate(Menu.java:32) 
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611) 
    ... 11 more 
Caused by: java.lang.reflect.InvocationTargetException 
    at java.lang.reflect.Constructor.constructNative(Native Method) 
    at java.lang.reflect.Constructor.newInstance(Constructor.java:415) 
    at android.view.LayoutInflater.createView(LayoutInflater.java:505) 
    ... 23 more 
Caused by: java.lang.OutOfMemoryError: bitmap size exceeds VM budget 
    at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method) 
    at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:460) 
    at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:336) 
    at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:697) 
    at android.content.res.Resources.loadDrawable(Resources.java:1709) 
    at android.content.res.TypedArray.getDrawable(TypedArray.java:601) 
    at android.widget.ImageView.<init>(ImageView.java:118) 
    at android.widget.ImageView.<init>(ImageView.java:108) 

這是我的XML代碼:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical"> 

<RelativeLayout 
    android:id="@+id/RL_Title" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_weight="8" 
    android:onClick="onTitleClick" > 

    <ImageView 
     android:id="@+id/Img_Title_bg" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:scaleType="center" 
     android:src="@drawable/title_bg" /> 

    <Button 
     android:id="@+id/Btn_Title" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_centerVertical="true" 
     android:layout_marginRight="5dp" 
     android:background="@drawable/title_al" 
     android:drawableRight="@drawable/pro" 
     android:gravity="center" 
     android:onClick="onTitleClick" /> 
</RelativeLayout> 

<RelativeLayout 
    android:id="@+id/RL_MainMenu" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_weight="1" android:onClick="onDoNothing"> 

    <ImageView 
     android:id="@+id/ImageView01" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="4" 
     android:src="@drawable/main_bg" android:scaleType="centerCrop"/> 

    <ImageView 
     android:id="@+id/Img_logo" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="4" 
     android:scaleType="center" 
     android:src="@drawable/logo_al" /> 

    <LinearLayout 
     android:id="@+id/LI_Menu" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_above="@+id/RL_ExtraOption" 
     android:layout_alignTop="@+id/Img_logo" 
     android:layout_margin="2dp" 
     android:orientation="vertical" > 

     <ImageButton 
      android:id="@+id/Img_Buyer" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_margin="1dp" 
      android:layout_weight="1" 
      android:background="@drawable/bt_blink" 
      android:onClick="Nextclick" 
      android:scaleType="fitCenter" 
      android:soundEffectsEnabled="true" 
      android:src="@drawable/buyer_icon" /> 

     <ImageButton 
      android:id="@+id/Img_Seller" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_margin="1dp" 
      android:layout_weight="1" 
      android:background="@drawable/bt_blink" 
      android:onClick="Nextclick" 
      android:scaleType="fitCenter" 
      android:src="@drawable/seller_icon" /> 

     <ImageButton 
      android:id="@+id/Img_Lender" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_margin="1dp" 
      android:layout_weight="1" 
      android:background="@drawable/bt_blink" 
      android:onClick="Nextclick" 
      android:scaleType="fitCenter" 
      android:src="@drawable/lender_icon" /> 

     <ImageButton 
      android:id="@+id/Img_myTitleRep" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_margin="1dp" 
      android:layout_weight="1" 
      android:background="@drawable/bt_blink" 
      android:onClick="Nextclick" 
      android:scaleType="fitCenter" 
      android:src="@drawable/my_title_rep_icon_al" /> 

     <ImageButton 
      android:id="@+id/Img_Setup" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_margin="1dp" 
      android:layout_weight="1" 
      android:background="@drawable/bt_blink" 
      android:onClick="Nextclick" 
      android:scaleType="fitCenter" 
      android:src="@drawable/setup_icon" /> 
    </LinearLayout> 

    <RelativeLayout 
     android:id="@+id/RL_ExtraOption" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:background="@drawable/main_bottom_bg" > 

     <TextView 
      android:id="@+id/txt_RepName" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerVertical="true" 
      android:layout_marginLeft="10dp" 
      android:textColor="@color/white" 
      android:textSize="@dimen/font_size" /> 

     <TableRow 
      android:id="@+id/TR_ContactRep" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentRight="true" 
      android:layout_centerVertical="true" 
      android:gravity="center" > 

      <Button 
       android:id="@+id/Btn_ContactRep" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_marginRight="3dp" 
       android:background="@drawable/contact_rep_blink" 
       android:onClick="ContactRep_Click" /> 

      <Button 
       android:id="@+id/Btn_MoreOption" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_marginRight="5dp" 
       android:background="@drawable/main_more_blink" 
       android:onClick="onMoreClick" /> 
     </TableRow> 
    </RelativeLayout> 

    <LinearLayout 
     android:id="@+id/ln_Mainmore" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="@drawable/dialog_bg" android:layout_alignParentBottom="true" android:visibility="gone"> 

     <LinearLayout 
      android:id="@+id/LinearLayout02" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="3" 
      android:orientation="vertical" > 
     </LinearLayout> 

     <TableLayout 
      android:id="@+id/TableLayout01" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:gravity="center" > 

      <TableRow 
       android:id="@+id/TableRow04" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:layout_margin="5dp" 
       android:layout_marginTop="20dp" 
       android:gravity="center" > 

       <Button 
        android:id="@+id/Btn_Rate" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:layout_weight="1" 
        android:background="@drawable/property_blue_blink" 
        android:onClick="onRate" 
        android:singleLine="true" 
        android:text="Rate/Testimonial" 
        android:textColor="@color/white" 
        android:textSize="@dimen/font_size" 
        android:textStyle="bold" /> 
      </TableRow> 

      <TableRow 
       android:id="@+id/TableRow01" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:layout_margin="5dp" 
       android:gravity="center" > 

       <Button 
        android:id="@+id/btn_SubFeature" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:layout_weight="1" 
        android:background="@drawable/property_blue_blink" 
        android:onClick="onSubFeature" 
        android:singleLine="true" 
        android:text="Submit A Feature" 
        android:textColor="@color/white" 
        android:textSize="@dimen/font_size" 
        android:textStyle="bold" /> 
      </TableRow> 

      <TableRow 
       android:id="@+id/TableRow03" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:layout_margin="5dp" 
       android:gravity="center" > 

       <Button 
        android:id="@+id/Btn_ReferFrnd" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:layout_weight="1" 
        android:background="@drawable/property_blue_blink" 
        android:onClick="onReferAFrnd" 
        android:text="Refer A Friend" 
        android:textColor="@color/white" 
        android:textSize="@dimen/font_size" 
        android:textStyle="bold" /> 
      </TableRow> 

      <TableRow 
       android:id="@+id/TableRow02" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:layout_margin="5dp" 
       android:gravity="center" > 

       <Button 
        android:id="@+id/Btn_cancel" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:layout_weight="1" 
        android:background="@drawable/property_cancel_blink" 
        android:onClick="onClose" 
        android:text="Cancel" 
        android:textColor="@color/black" 
        android:textSize="@dimen/font_size" 
        android:textStyle="bold" /> 
      </TableRow> 
     </TableLayout> 

     <LinearLayout 
      android:id="@+id/linearLayout2" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="3" 
      android:orientation="vertical" > 
     </LinearLayout> 
    </LinearLayout> 

</RelativeLayout> 

+1

我猜你有一個位圖加載爲背景或imageview的來源。位圖大小是多少? – Simon 2012-04-25 11:22:09

+0

在「相關」下看看右邊的欄目 - 還有很多其他相同或相似主題的問題。 – claesv 2012-04-25 11:27:01

+0

現在只有15.9 KB。起初它的接近240 KB,我試圖將尺寸減小到15.9 KB,但是問題存在。 – Sniper 2012-04-25 11:27:21

回答

60

我猜這個問題不在你的佈局中;該問題在您的代碼中的其他位置。並且您可能正在泄漏上下文

其他可能的原因是,您必須在解析XML時創建龐大的多個對象(正如您在解析XML時第一次提到的那樣)。雖然Java有自動垃圾收集方法,但仍然不能完全依賴它。 取消您的收藏實例或在不再需要時清除對象內容是一種很好的做法。

但是我仍然準備了一些重要的事項,您應該在處理Android上的位圖時記住這些事項。

1)您可以在每個位圖上調用回收並將其設置爲空。 (bitmap.recycle()將釋放此位圖使用的所有內存,但不會使位圖對象無效)。

2)當活動被破壞時,您還可以解除與佈局關聯的drawables。嘗試下面給出的代碼,也可以看看這個鏈接link

private void unbindDrawables(View view) { 
     if (view.getBackground() != null) { 
      view.getBackground().setCallback(null); 
     } 
     if (view instanceof ViewGroup) { 
      for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) { 
       unbindDrawables(((ViewGroup) view).getChildAt(i)); 
      } 
      ((ViewGroup) view).removeAllViews(); 
     } 
    } 

// Call this method from onDestroy() 

    void onDestroy() { 
     super.onDestroy(); 
     unbindDrawables(findViewById(R.id.RootView)); 
     System.gc(); 
    } 

3)你可以把你hashmapsWeakHashmaps,從而使系統運行內存不足時,其存儲將得到釋放。

4)您可以縮放/調整所有位圖的大小。要縮放位圖,可以嘗試如下所示:

BitmapFactory.Options options = new BitmapFactory.Options(); 
    options.inSampleSize = 8; 
    Bitmap preview_bitmap=BitmapFactory.decodeStream(is, null, options); 

此inSampleSize選項可減少內存消耗。

這是一個完整的方法。首先它讀取圖像大小而不解碼內容本身。然後它找到最好的inSampleSize值;它應該是2的冪。最後,圖像被解碼。

// Decodes image and scales it to reduce memory consumption 
private Bitmap decodeFile(File f){ 
    try { 
     // Decode image size 
     BitmapFactory.Options o = new BitmapFactory.Options(); 
     o.inJustDecodeBounds = true; 
     BitmapFactory.decodeStream(new FileInputStream(f),null,o); 

     // The new size we want to scale to 
     final int REQUIRED_SIZE=70; 

     // Find the correct scale value. It should be the power of 2. 
     int scale=1; 
     while(o.outWidth/scale/2 >= REQUIRED_SIZE && o.outHeight/scale/2 >= REQUIRED_SIZE) 
      scale*=2; 

     // Decode with inSampleSize 
     BitmapFactory.Options o2 = new BitmapFactory.Options(); 
     o2.inSampleSize=scale; 
     return BitmapFactory.decodeStream(new FileInputStream(f), null, o2); 
    } 
    catch (FileNotFoundException e) { 
    } 
    return null; 
} 

看看這個link.

5)您可以在整個系統內存不足時調用onLowMemory()方法來獲取調用。你可以在那裏發佈一些資源。

6)您可以使您的對象SoftReferenceWeakreference,以便它們在低內存條件下釋放。

我觀察到的一個非常常見的內存泄漏是由於內部類的實現和在Activity中實現HandlerThis links talks about the same in more detail

我希望這有助於消除您的問題。

除了什麼人在這裏寫
+0

你如何決定REQUIRED_SIZE? – NinjaCoder 2013-06-03 19:08:36

+2

做得好N-Joy,感謝您的幫助 – 2015-04-27 07:44:41

+1

這對我有幫助很好的解決方案 – Dilip 2016-04-29 12:41:24

4

很多設備都非常有限,限制內存可用於每個裝置。許多較舊的設備爲您的應用程序分配16MB的內存,而一些更新的設備可能分配24MB甚至32MB(我見過的設備只有14MB)。

無論何時您將如此多的圖像加載到內存中,此空間填滿非常快,導致OutOfMemoryError被拋出。爲了克服這個問題,你應該(如之前的答案所述)將圖像縮小爲縮略圖,並且只將縮略圖保留在設備內存中(減少內存佔用)並將圖像保存到SDCARD。

您可以從Eclipse中的DDMS佈局中看到您的程序使用了多少內存。我相信你也可以覆蓋Application類來監聽「Low Memory」警告函數。

當您通過BitmapFactory讀取位圖時,將BitmapFactory.Option inScaled參數設置爲2或4(或適用於您的任何內容)以創建更小,內存更少的圖像。

通常,只有一種解決方案:使用較小的圖像。如果沒有代碼,很難說什麼是錯的。您可以嘗試降低圖像大小。

+0

我試圖縮小圖像大小,問題依然存在。日誌說它甚至不能分配103 Kb。 04-30 16:58:21.833:E/dalvikvm-heap(3654):105376字節的外部分配對於這個過程來說太大了。 04-30 16:58:21.841:E /(3654):VM不會讓我們分配105376字節 – Sniper 2012-04-30 11:40:25

+0

您可以發送佈局中使用的圖像,我會在我的末端嘗試這個 – 2012-04-30 11:47:49

+0

我的應用程序使用數據庫,並且在第一次解析一些XML數據並插入到Sqlite數據庫中。我的內存不足問題只發生在第一次。第二次,它工作正常。我試過System.gc()。那有沒有什麼可能。或者我可以以其他方式釋放內存。 – Sniper 2012-04-30 12:01:29

6

這是在舊版設備上使用Android ImageView與大圖像時(即使文件大小僅爲幾Kbs),也是一個典型問題。原始圖形庫名爲skia用於解碼圖像根據圖像的尺寸分配本機內存,因此文件大小不會直接影響此。

即使您省略了XML佈局定義中的src屬性以自行將圖像加載到您的onCreate()方法中,也會觸發相同的錯誤。解決此問題的唯一方法是使用的BitmapFactory.decodeStream在解碼過程中直接執行下采樣(例如,請參閱Strange out of memory issue while loading an image to a Bitmap object)。這本身就提出瞭如何確定這個比例因子的問題。

我發現處理大量圖像時與WebView更換ImageView並加載一些最起碼的HTML這樣的最好的解決辦法:

webView.loadUrl("file:///android_res/raw/background.html"); 

其中raw/background.html內容可能是這樣的:

<!DOCTYPE html> 
<html> 
<head> 
    <meta charset="UTF-8"> 
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> 
    <style type="text/css"> 
     body { 
      margin: 0; 
     } 

     img { 
      width: 100%; 
      height: auto; 
      display: block; 
     } 
    </style> 
</head> 
<body> 
    <img src="file:///android_res/drawable/background.png" /> 
</body> 

在WebView中使用的WebKit引擎執行映像裝入以更高效的方式執行,以便這些內存分配錯誤應該消失。

你甚至可以使用不同的肖像和風景圖像,把它們放在drawable-portdrawable-land。但是,您必須在onDestroy()方法的WebView實例上調用clearCache(false),否則將始終使用第一次加載時緩存的圖像(禁用WebView實例上的緩存不起作用)。

0

你可能已經經歷了上述所有的解決方案,但仍然試一試。 在找到解決方案之前,我迷失了方向,直到我找到了解決方案。 這可能會爲你的情況夜間工作(對不起,在這種情況下)。

myIntent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_ACTIVITY_CLEAR_TASK); myIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

在您打電話給您的活動之前放置它們。

+0

我不認爲這是一個「合法的」答案 – Gattsu 2015-05-24 06:03:22

3

我之前有同樣的問題,我正在S4安卓4.3上進行調試。 的解決方案是:複製從MDPI文件夾華電國際文件夾的圖像(所以他們現在都文件夾)

我知道怪異!但是這解決了我的問題。

3

在這個問題上花了很多時間後,我發現解決方案! 所有你需要做的是要求更多的記憶。 將此代碼您的Android清單中下:

<application>: 

地說:

android:largeHeap="true" 

工作對我來說太棒了! 請注意,此解決方案僅適用於API 11及更高版本。

更多信息here.

2

它接縫像烏爾形象是大尺寸的,這樣你們可以解碼圖像,並使用Android的:largeHeap =「真」。裏面的清單應用程序標籤

相關問題