我写了一个包含很多活动的应用。 在我运行大约30次(使用数据库IO,设置图像等3个活动)之后,它将崩溃并且logCat说“在1036340字节分配时内存不足。”
我不知道哪个对象没有从内存中释放......
有谁可以告诉我可能的原因和解决方案?
或者我怎样才能看到内存中的内容?
P.S。我的Bitmap事物在另一个活动中是静态的,所以它不会每次都创建。
致user1779222: 我认为它可能是这个问题的解决方案,所以我试着让它静止。但似乎没有帮助。
致Maharith Aditya S S:----------这是我的logcat ---------------
04-19 17:06:50.747: I/dalvikvm-heap(12439): Clamp target GC heap from 97.183MB to 96.000MB
04-19 17:06:50.747: D/dalvikvm(12439): GC_FOR_ALLOC freed 3853K, 8% free 91106K/98272K, paused 218ms, total 218ms
04-19 17:06:50.987: I/dalvikvm-heap(12439): Clamp target GC heap from 97.937MB to 96.000MB
04-19 17:06:50.987: D/dalvikvm(12439): GC_FOR_ALLOC freed 194K, 7% free 91878K/98272K, paused 218ms, total 219ms
04-19 17:06:50.987: I/dalvikvm-heap(12439): Forcing collection of SoftReferences for 1036340-byte allocation
04-19 17:06:51.227: I/dalvikvm-heap(12439): Clamp target GC heap from 97.937MB to 96.000MB
04-19 17:06:51.227: D/dalvikvm(12439): GC_BEFORE_OOM freed <1K, 7% free 91878K/98272K, paused 237ms, total 237ms
04-19 17:06:51.227: E/dalvikvm-heap(12439): Out of memory on a 1036340-byte allocation.
04-19 17:06:51.227: I/dalvikvm(12439): "main" prio=5 tid=1 RUNNABLE
04-19 17:06:51.227: I/dalvikvm(12439): | group="main" sCount=0 dsCount=0 obj=0x41ef0728 self=0x41edb600
04-19 17:06:51.227: I/dalvikvm(12439): | sysTid=12439 nice=0 sched=0/0 cgrp=apps handle=1074143228
04-19 17:06:51.227: I/dalvikvm(12439): | state=R schedstat=( 0 0 0 ) utm=113289 stm=44410 core=0
04-19 17:06:51.237: I/dalvikvm(12439): at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
04-19 17:06:51.237: I/dalvikvm(12439): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:503)
04-19 17:06:51.237: I/dalvikvm(12439): at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:356)
04-19 17:06:51.237: I/dalvikvm(12439): at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:816)
04-19 17:06:51.237: I/dalvikvm(12439): at android.content.res.Resources.loadDrawable(Resources.java:2117)
04-19 17:06:51.247: I/dalvikvm(12439): at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
04-19 17:06:51.247: I/dalvikvm(12439): at android.view.View.<init>(View.java:3545)
04-19 17:06:51.247: I/dalvikvm(12439): at android.widget.ImageView.<init>(ImageView.java:121)
04-19 17:06:51.247: I/dalvikvm(12439): at android.widget.ImageView.<init>(ImageView.java:117)
04-19 17:06:51.247: I/dalvikvm(12439): at java.lang.reflect.Constructor.constructNative(Native Method)
04-19 17:06:51.247: I/dalvikvm(12439): at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
04-19 17:06:51.247: I/dalvikvm(12439): at android.view.LayoutInflater.createView(LayoutInflater.java:594)
04-19 17:06:51.257: I/dalvikvm(12439): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
04-19 17:06:51.257: I/dalvikvm(12439): at android.view.LayoutInflater.onCreateView(LayoutInflater.java:669)
04-19 17:06:51.257: I/dalvikvm(12439): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:694)
04-19 17:06:51.257: I/dalvikvm(12439): at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
04-19 17:06:51.257: I/dalvikvm(12439): at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
04-19 17:06:51.257: I/dalvikvm(12439): at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
04-19 17:06:51.257: I/dalvikvm(12439): at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
04-19 17:06:51.257: I/dalvikvm(12439): at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
04-19 17:06:51.257: I/dalvikvm(12439): at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
04-19 17:06:51.257: I/dalvikvm(12439): at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
04-19 17:06:51.257: I/dalvikvm(12439): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:274)
04-19 17:06:51.257: I/dalvikvm(12439): at android.app.Activity.setContentView(Activity.java:1895)
04-19 17:06:51.277: I/dalvikvm(12439): at easylube.com.guardwatch.ActCheckHome.onCreate(ActCheckHome.java:338)
04-19 17:06:51.277: I/dalvikvm(12439): at android.app.Activity.performCreate(Activity.java:5133)
04-19 17:06:51.277: I/dalvikvm(12439): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
04-19 17:06:51.277: I/dalvikvm(12439): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2230)
04-19 17:06:51.277: I/dalvikvm(12439): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2316)
04-19 17:06:51.277: I/dalvikvm(12439): at android.app.ActivityThread.access$600(ActivityThread.java:150)
04-19 17:06:51.277: I/dalvikvm(12439): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1298)
04-19 17:06:51.277: I/dalvikvm(12439): at android.os.Handler.dispatchMessage(Handler.java:99)
04-19 17:06:51.277: I/dalvikvm(12439): at android.os.Looper.loop(Looper.java:213)
04-19 17:06:51.277: I/dalvikvm(12439): at android.app.ActivityThread.main(ActivityThread.java:5225)
04-19 17:06:51.277: I/dalvikvm(12439): at java.lang.reflect.Method.invokeNative(Native Method)
04-19 17:06:51.277: I/dalvikvm(12439): at java.lang.reflect.Method.invoke(Method.java:525)
04-19 17:06:51.277: I/dalvikvm(12439): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:741)
04-19 17:06:51.277: I/dalvikvm(12439): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
04-19 17:06:51.277: I/dalvikvm(12439): at dalvik.system.NativeStart.main(Native Method)
04-19 17:06:51.277: D/skia(12439): --- decoder->decode returned false
04-19 17:06:51.277: D/AndroidRuntime(12439): Shutting down VM
04-19 17:06:51.277: W/dalvikvm(12439): threadid=1: thread exiting with uncaught exception (group=0x41eef8b0)
04-19 17:06:51.277: W/System.err(12439): java.lang.RuntimeException: Unable to start activity ComponentInfo{easylube.com.guardwatch/easylube.com.guardwatch.ActCheckHome}: android.view.InflateException: Binary XML file line #260: Error inflating class <unknown>
我使用Bitmap为一些ImageView和其他人使用.setBackgroundResource(Resource)。
我的跑步步骤是:activityA-&gt;(A finish())activityB-&gt;(B finish())activityC-&gt;(C finish())activityA .........
我不知道这些信息是否会有所帮助,我找到了一种观看Heap的方法, 看来“数据对象”可能是问题......
Rahul Kumar:
settingImg() {
if(something){
bmp = BitmapFactory.decodeResource(getBaseContext().getResources(),R.drawable.img1);
img.setBitmap(Bitmap.createScaledBitmap(bmp, scaledwidth, scaledhight, false));
}else if(something2){
bmp = BitmapFactory.decodeResource(getBaseContext().getResources(),R.drawable.img2);
img.setBitmap(Bitmap.createScaledBitmap(bmp, scaledwidth, scaledhight, false));
}.........
}
swichActivity(){
Intent intent = new Intent();
intent.setClass(B.this, C.class);
intent .setFlags(intent .getFlags() | intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intent);
bmp.recycle();
bmp=null;
finish();
}
我的图像尺寸= 506 * 506 90.9KB
我刚刚编写了一个只有2个Activity和2layout的应用程序,每个只有1个TextView。 内存使用量仍然在增加,但很长一段时间后,内存使用情况仍在缓慢下降。
为什么????
答案 0 :(得分:0)
使用Picasso
或Glide
加载图片,它会为您处理内存。还会调整大小以使位图大小适合您的ImageView
。