致:java.lang.OutOfMemoryError:位图大小超过VM预算 [英] Caused by: java.lang.OutOfMemoryError: bitmap size exceeds VM budget
问题描述
在我的应用程序,当我尝试启动它强制关闭,错误指示行的setContentView(R.layout.Menu);的布局。而在XML文件,它显示在我的布局的OutOfMemoryError图像视图。我真的糊涂了。请指导我进行进一步的举动。
In my application when I try to launch it Force Closed and the error pointing the line "setContentView(R.layout.Menu);" of the layout. And in the XML file it show the "OutOfMemoryError" image view in my layout. I am realy confused. Please guide me for further move.
编辑:
我的应用程序使用的数据库,并在第一时间它解析一些XML数据插入到SQLite数据库。我的内存不足的问题只在第一次出现。第二次,它工作正常。我试过的System.gc()。是否有任何的概率。
My application uses database, and at the very first time it parse some XML data and insert into the Sqlite database. My Outofmemory problem occurs only at the first time. Second time it works fine. I tried System.gc(). Is there any prob on that.
这是我的日志:
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 code:
<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>
推荐答案
我想问题是不是在你的布局,问题是,别的地方在你的code。和也许你正在泄漏的情况下某处。
I guess problem is not in your layout, problem is somewhere else in your code. and probably you are leaking context somewhere.
其他可能的原因是,你必须建立庞大的多个对象在解析您的XML(如你所提到的这种情况,当你解析XML第一次)。虽然Java有自动垃圾收集方法,但仍不能完全依赖它。这是一个很好的做法,废止的集合实例或清除时,你不需要他们了你的对象的内容。
Other probable reason is that you must be creating bulky multiple objects while parsing your xml(as you mentioned this occurs first time when you parse xml). Though java has auto garbage collection approach but still you can not completely rely on it. it is a good practice to nullify your collection instance or clear your objects content when you dont need them anymore.
但我仍然有prepared的,你应该记住,而与位图处理的机器人要点清单。
But still i have prepared a list of important points which you should remember while dealing with bitmaps in android.
1)您可以拨打回收每个位图,并将它们设置为null。( bitmap.recycle()
将释放所有使用的内存该位图,但不坏的位图对象)。
1) you can call recycle on each bitmap and set them to null.(bitmap.recycle()
will release all the memory used by this bitmap but does not nullify the bitmap object ).
2)您也可以解除绑定可绘制
与布局相关的活动时被销毁。尝试下面给出的code,也看看这个链接<一个href="http://stackoverflow.com/questions/1949066/java-lang-outofmemoryerror-bitmap-size-exceeds-vm-budget-android">link.
2) you can also unbind the drawables
associated with layouts when activity is destroyed. Try the code given below and also have a look at this link 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)您可以将您的包含HashMap
到 WeakHashmaps
。这样,当系统运行内存不足的内存会得到释放。
3) you can convert your hashmaps
to WeakHashmaps
. so that its memory would get released when system runs low on memory.
4)您可以缩放/调整所有的位图。为了扩大位图,你可以尝试这样的事。
4) you can scale/resize all your bitmaps. To scale bitmaps you can try something like this.
BitmapFactory.Options options=new BitmapFactory.Options();
options.inSampleSize = 8;
Bitmap preview_bitmap=BitmapFactory.decodeStream(is,null,options);
这个inSampleSize选项可减少内存消耗。
This inSampleSize option reduces memory consumption.
下面是一个完整的方法。首先,它读取图像大小没有内容本身进行解码。然后它找到最好inSampleSize值,它应该是2的幂最后图像是德$ C $光盘。
Here's a complete method. First it reads image size without decoding the content itself. Then it finds the best inSampleSize value, it should be a power of 2. And finally the image is decoded.
//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;
}
看看这个→<一个href="http://stackoverflow.com/questions/477572/android-strange-out-of-memory-issue/823966#823966">ink..
5)您可以覆盖 onLowMemory()
方法,该活动得到了打电话时,整个系统运行内存不足。你可以有释放的资源很少。
5) you can override onLowMemory()
method in activity which gets a call when entire system runs low on memory. you can release few resources there.
6)你可以让你的对象 SoftReference
或的WeakReference
,让他们得到释放的内存不足的情况。
6) you can make your objects SoftReference
or Weakreference
, so that they get released in low memory condition.
编辑:
一个非常常见的内存泄漏,我观察到的是,由于内部类的活动
的实施和贯彻处理程序
。 这个链接有关详细相同的会谈。
A very common Memory leak that i observed is due to the implementation of inner classes and implementing Handler
in Activity
. This links talks about the same in more detail.
希望这有助于消除你的问题。
Hope this helps to eliminate your problem.
这篇关于致:java.lang.OutOfMemoryError:位图大小超过VM预算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!