问题描述
我开发一个Android游戏应用程序在那里我有大约15 screens.When我连续播放,我得到一个内存不足错误的问题随机,有时在15日的屏幕,有时在第12届屏幕左右。
另外,检查中的一个画面如下的XML。
< XML版本=1.0编码=UTF-8&GT?;
<的FrameLayout的xmlns:机器人=http://schemas.android.com/apk/res/android
的xmlns:工具=http://schemas.android.com/tools
机器人:ID =@ + ID /大型机
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT>
< RelativeLayout的
机器人:ID =@ + ID / mainlayout
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
机器人:背景=@可绘制/ woodenbg1>
<天文台表
机器人:ID =@ + ID /时计
机器人:layout_width =0dp
机器人:layout_height =0dp
机器人:TEXTSIZE =20SP
机器人:TEXTSTYLE =黑体
机器人:字体=SANS
机器人:能见度=水涨船高/>
< ImageView的
机器人:ID =@ + ID / imageline
机器人:layout_width =10000dp
机器人:layout_height =WRAP_CONTENT
机器人:layout_marginTop =105dp
机器人:SRC =@可绘制/ lineblackfornormal/>
< ImageView的
机器人:ID =@ + ID / imageviewunderalphac
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_centerHorizontal =真
机器人:layout_marginTop =350dp
机器人:SRC =@可绘制/ whitec/>
< ImageView的
机器人:ID =@ + ID / backgroundofalphab
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_centerHorizontal =真
机器人:layout_marginTop =35dp
机器人:SRC =@可绘制/ backgroundfordragimages
机器人:能见度=隐形/>
< ImageView的
机器人:ID =@ + ID / backgroundofalphaa
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_marginLeft =20dp
机器人:layout_marginTop =35dp
机器人:SRC =@可绘制/ backgroundfordragimages
机器人:能见度=隐形/>
< ImageView的
机器人:ID =@ + ID / backgroundofalphac
机器人:layout_width =100dp
机器人:layout_height =WRAP_CONTENT
机器人:layout_alignParentRight =真
机器人:layout_marginTop =33dp
机器人:SRC =@可绘制/ backgroundfordragimages
机器人:能见度=隐形/>
< ImageView的
机器人:ID =@ + ID / imageviewunderleftalphab
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_alignParentBottom =真
机器人:layout_marginBottom =250dp
机器人:layout_marginLeft =15dp
机器人:SRC =@可绘制/ whiteb/>
< ImageView的
机器人:ID =@ + ID / imageviewunderrightalphaa
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_alignParentBottom =真
机器人:layout_alignParentRight =真
机器人:layout_marginBottom =250dp
机器人:layout_marginRight =10dp
机器人:SRC =@可绘制/ whitea/>
< ImageView的
机器人:ID =@ + ID / imageviewabovealphab
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_centerHorizontal =真
机器人:layout_marginTop =35dp
机器人:SRC =@可绘制/ colouredb/>
< ImageView的
机器人:ID =@ + ID / imageviewabovealphaa
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_marginLeft =20dp
机器人:layout_marginTop =35dp
机器人:SRC =@可绘制/ coloureda/>
< ImageView的
机器人:ID =@ + ID / imageviewabovealphac
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_alignLeft =@ + ID / backgroundofalphac
机器人:layout_marginTop =33dp
机器人:SRC =@可绘制/ colouredc/>
< / RelativeLayout的>
< /的FrameLayout>
logcat的
12月5日至一十六号:22:18.989:E / GraphicsJNI(6745):虚拟机不会让我们分配2225664字节
12月5日至16日:22:18.999:E / AndroidRuntime(6745):致命异常:主要
12月5日至16日:22:18.999:E / AndroidRuntime(6745):java.lang.OutOfMemoryError:位图大小超过VM预算
12月5日至16日:22:18.999:E / AndroidRuntime(6745):在android.graphics.Bitmap.nativeCreate(本机方法)
12月5日至16日:22:18.999:E / AndroidRuntime(6745):在android.graphics.Bitmap.createBitmap(Bitmap.java:477)
12月5日至16日:22:18.999:E / AndroidRuntime(6745):在android.graphics.Bitmap.createBitmap(Bitmap.java:444)
12月5日至16日:22:18.999:E / AndroidRuntime(6745):在android.graphics.Bitmap.createScaledBitmap(Bitmap.java:349)
12月5日至16日:22:18.999:E / AndroidRuntime(6745):在android.graphics.BitmapFactory.finishDe code(BitmapFactory.java:498)
12月5日至16日:22:18.999:E / AndroidRuntime(6745):在android.graphics.BitmapFactory.de codeStream(BitmapFactory.java:473)
12月5日至16日:22:18.999:E / AndroidRuntime(6745):在android.graphics.BitmapFactory.de codeResourceStream(BitmapFactory.java:336)
12月5日至16日:22:18.999:E / AndroidRuntime(6745):在android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:697)
12月5日至16日:22:18.999:E / AndroidRuntime(6745):在android.content.res.Resources.loadDrawable(Resources.java:1709)
12月5日至16日:22:18.999:E / AndroidRuntime(6745):在android.content.res.Resources.getDrawable(Resources.java:581)
12月5日至16日:22:18.999:E / AndroidRuntime(6745):在android.view.View.setBackgroundResource(View.java:7533)
12月5日至16日:22:18.999:E / AndroidRuntime(6745):在com.ssn.myapp.NumbersLevel3.onCreate(NumbersLevel3.java:173)
12月5日至16日:22:18.999:E / AndroidRuntime(6745):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
12月5日至16日:22:18.999:E / AndroidRuntime(6745):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
12月5日至16日:22:18.999:E / AndroidRuntime(6745):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
12月5日至16日:22:18.999:E / AndroidRuntime(6745):在android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:2832)
12月5日至16日:22:18.999:E / AndroidRuntime(6745):在android.app.ActivityThread.access $ 1600(ActivityThread.java:117)
12月5日至16日:22:18.999:E / AndroidRuntime(6745):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:935)
12月5日至16日:22:18.999:E / AndroidRuntime(6745):在android.os.Handler.dispatchMessage(Handler.java:99)
12月5日至16日:22:18.999:E / AndroidRuntime(6745):在android.os.Looper.loop(Looper.java:130)
12月5日至16日:22:18.999:E / AndroidRuntime(6745):在android.app.ActivityThread.main(ActivityThread.java:3683)
12月5日至16日:22:18.999:E / AndroidRuntime(6745):在java.lang.reflect.Method.invokeNative(本机方法)
12月5日至16日:22:18.999:E / AndroidRuntime(6745):在java.lang.reflect.Method.invoke(Method.java:507)
12月5日至16日:22:18.999:E / AndroidRuntime(6745):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:839)
12月5日至16日:22:18.999:E / AndroidRuntime(6745):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
12月5日至16日:22:18.999:E / AndroidRuntime(6745):在dalvik.system.NativeStart.main(本机方法)
回收位图时不使用。经过蜂窝位图留在堆上。
bitmaps.recycle();
http://android-developers.blogspot.de/ 2009/01 /避免内存-leaks.html 。
链接上面有如何避免内存泄漏的话题。
此外,如果您有高分辨率的图像,则应该缩小。请参阅主题下加载一个缩小版到内存中。
http://developer.android.com/training/displaying-位图/负载bitmap.html
假设你浏览的形式actiivty A到B.在onResume()加载位图。在的onPause()循环使用位图。这样做的每一个地方加载位图的活动。
您可以使用MAT分析仪作为shwon在矿井下面的视频来分析和解决memeory泄漏 http://www.youtube.com/watch?v=_CruQY55HOk
在清单下的应用程序标签largeHeap您可以使用机器人。
警告:由于堆的大小增加了GC踢更频繁和更频繁的应用程序停顿。因此,仅仅因为你需要一个更大的堆,你不应该使用这个。你可以看到上面的视频的家伙发出警告差不多。
安卓largeHeap
无论您的应用程序的进程应该具有较大的Dalvik的堆创建。这适用于为应用程序创建的所有进程。它仅适用于装载到一个过程中的第一应用程序;如果您使用的是共享的用户ID,允许多个应用程序使用过程中,他们都必须始终如一地使用这个选项,否则将有联合国predictable结果。
大多数应用程序不应该需要这个,而应着眼于减少其整体的内存使用以提高性能。启用此也不能保证一个固定的增加可用内存,因为有些设备是由它们的总可用内存受限
要查询在运行时,可用内存的大小,使用的方法getMemoryClass()或getLargeMemoryClass()。
I am developing an android game application where i have about 15 screens.When i continuously play, i get an out of memory error problem randomly, sometimes in the 15th screen, and sometimes in the 12th screen or so.
Also, check out the xml below, of one the screens.
<?xml version="1.0" encoding="UTF-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/MainFrame"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<RelativeLayout
android:id="@+id/mainlayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/woodenbg1" >
<Chronometer
android:id="@+id/chronometer"
android:layout_width="0dp"
android:layout_height="0dp"
android:textSize="20sp"
android:textStyle="bold"
android:typeface="sans"
android:visibility="gone" />
<ImageView
android:id="@+id/imageline"
android:layout_width="10000dp"
android:layout_height="wrap_content"
android:layout_marginTop="105dp"
android:src="@drawable/lineblackfornormal" />
<ImageView
android:id="@+id/imageviewunderalphac"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="350dp"
android:src="@drawable/whitec" />
<ImageView
android:id="@+id/backgroundofalphab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="35dp"
android:src="@drawable/backgroundfordragimages"
android:visibility="invisible" />
<ImageView
android:id="@+id/backgroundofalphaa"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="35dp"
android:src="@drawable/backgroundfordragimages"
android:visibility="invisible" />
<ImageView
android:id="@+id/backgroundofalphac"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginTop="33dp"
android:src="@drawable/backgroundfordragimages"
android:visibility="invisible" />
<ImageView
android:id="@+id/imageviewunderleftalphab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="250dp"
android:layout_marginLeft="15dp"
android:src="@drawable/whiteb" />
<ImageView
android:id="@+id/imageviewunderrightalphaa"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="250dp"
android:layout_marginRight="10dp"
android:src="@drawable/whitea" />
<ImageView
android:id="@+id/imageviewabovealphab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="35dp"
android:src="@drawable/colouredb" />
<ImageView
android:id="@+id/imageviewabovealphaa"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="35dp"
android:src="@drawable/coloureda" />
<ImageView
android:id="@+id/imageviewabovealphac"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/backgroundofalphac"
android:layout_marginTop="33dp"
android:src="@drawable/colouredc" />
</RelativeLayout>
</FrameLayout>
Logcat
05-16 12:22:18.989: E/GraphicsJNI(6745): VM won't let us allocate 2225664 bytes
05-16 12:22:18.999: E/AndroidRuntime(6745): FATAL EXCEPTION: main
05-16 12:22:18.999: E/AndroidRuntime(6745): java.lang.OutOfMemoryError: bitmap size exceeds VM budget
05-16 12:22:18.999: E/AndroidRuntime(6745): at android.graphics.Bitmap.nativeCreate(Native Method)
05-16 12:22:18.999: E/AndroidRuntime(6745): at android.graphics.Bitmap.createBitmap(Bitmap.java:477)
05-16 12:22:18.999: E/AndroidRuntime(6745): at android.graphics.Bitmap.createBitmap(Bitmap.java:444)
05-16 12:22:18.999: E/AndroidRuntime(6745): at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:349)
05-16 12:22:18.999: E/AndroidRuntime(6745): at android.graphics.BitmapFactory.finishDecode(BitmapFactory.java:498)
05-16 12:22:18.999: E/AndroidRuntime(6745): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:473)
05-16 12:22:18.999: E/AndroidRuntime(6745): at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:336)
05-16 12:22:18.999: E/AndroidRuntime(6745): at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:697)
05-16 12:22:18.999: E/AndroidRuntime(6745): at android.content.res.Resources.loadDrawable(Resources.java:1709)
05-16 12:22:18.999: E/AndroidRuntime(6745): at android.content.res.Resources.getDrawable(Resources.java:581)
05-16 12:22:18.999: E/AndroidRuntime(6745): at android.view.View.setBackgroundResource(View.java:7533)
05-16 12:22:18.999: E/AndroidRuntime(6745): at com.ssn.myapp.NumbersLevel3.onCreate(NumbersLevel3.java:173)
05-16 12:22:18.999: E/AndroidRuntime(6745): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-16 12:22:18.999: E/AndroidRuntime(6745): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
05-16 12:22:18.999: E/AndroidRuntime(6745): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
05-16 12:22:18.999: E/AndroidRuntime(6745): at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:2832)
05-16 12:22:18.999: E/AndroidRuntime(6745): at android.app.ActivityThread.access$1600(ActivityThread.java:117)
05-16 12:22:18.999: E/AndroidRuntime(6745): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
05-16 12:22:18.999: E/AndroidRuntime(6745): at android.os.Handler.dispatchMessage(Handler.java:99)
05-16 12:22:18.999: E/AndroidRuntime(6745): at android.os.Looper.loop(Looper.java:130)
05-16 12:22:18.999: E/AndroidRuntime(6745): at android.app.ActivityThread.main(ActivityThread.java:3683)
05-16 12:22:18.999: E/AndroidRuntime(6745): at java.lang.reflect.Method.invokeNative(Native Method)
05-16 12:22:18.999: E/AndroidRuntime(6745): at java.lang.reflect.Method.invoke(Method.java:507)
05-16 12:22:18.999: E/AndroidRuntime(6745): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
05-16 12:22:18.999: E/AndroidRuntime(6745): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
05-16 12:22:18.999: E/AndroidRuntime(6745): at dalvik.system.NativeStart.main(Native Method)
Recycle bitmaps when not in use. After honeycomb bitmaps stay on heap.
bitmaps.recycle();
http://android-developers.blogspot.de/2009/01/avoiding-memory-leaks.html.
The link above has a topic on how to avoid memory leaks.
Also if you have high resolution image , you should scale down. See the topic under Load a Scaled Down Version into Memory.
http://developer.android.com/training/displaying-bitmaps/load-bitmap.html
Suppose you navigate form actiivty A to B. In onResume() load bitmaps. In onPause() recycle bitmaps. Do this for every activity where you load bitmaps.
You can use MAT Analyzer as shwon inthe video below to analyze and fix memeory leakshttp://www.youtube.com/watch?v=_CruQY55HOk
You could use android:largeHeap in manifest under application tag.
Warning : As the heap size increases the GC kicks in more frequently and more frequently app pauses. So just because you require a bigger heap you should not use this. You can see the video above and the guy warns about the same.
android:largeHeap
Whether your application's processes should be created with a large Dalvik heap. This applies to all processes created for the application. It only applies to the first application loaded into a process; if you're using a shared user ID to allow multiple applications to use a process, they all must use this option consistently or they will have unpredictable results.
Most apps should not need this and should instead focus on reducing their overall memory usage for improved performance. Enabling this also does not guarantee a fixed increase in available memory, because some devices are constrained by their total available memory.
To query the available memory size at runtime, use the methods getMemoryClass() or getLargeMemoryClass().
这篇关于内存不足的错误的android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!