您的位置:首页 > 其它

我遇到的内存泄露问题

2016-01-14 12:52 295 查看
从开始写博客开始到现在,一边学网页一边在修内存泄露的bug。解决了后保存下解决方法,同时也给有和我一样遭遇的童鞋们一个参考。

错误:java.util.concurrent.ExecutionException: java.lang.OutOfMemoryError: Failed to allocate a 6144012 byte allocation with 5750980 free bytes and 5MB until OOM

错误描述:在其他手机里面是好的,在三星部分手机里面时好时坏,有时候会直接崩溃。

错误中的位置:setContentView(R.layout.activity_dev_list);

解决办法:看到错误提示中有如下

at dalvik.system.VMRuntime.newNonMovableArray(Native Method)

01-14 09:23:31.849: E/Resources(11482): at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)

01-14 09:23:31.849: E/Resources(11482): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:747)

01-14 09:23:31.849: E/Resources(11482): at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:568)

我就知道和图片是离不开的。然而我的代码里面没有用到Bitmap,那么是哪儿来的呢?

解决思路:无头绪,先把静态图片全部干掉,还是不行,怎么办?

   把所有静态context 线程啥的在onDestroy()里面释放。试了下,还是不行。

  仔细想了下,想起来在登录按钮按下去时程序崩溃的,那么这个溢出可能在动画界面,或者在登录界面。先干掉动画界面,看了下,果然,连续测了20次,都没有程序崩溃的情况发生,那么问题范围缩小到了动画界面了。

动画界面代码:

         

<pre name="code" class="html">
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
 private Thread mSplashThread;
    AnimationDrawable frameAnimation;
setContentView(R.layout.splash);

final ImageView splashImageView = (ImageView) findViewById(R.id.SplashImageView);
splashImageView.setBackgroundResource(R.drawable.flag);
frameAnimation = (AnimationDrawable)splashImageView.getBackground();
splashImageView.post(new Runnable(){
@Override
public void run() {
frameAnimation.start();
}
});

final SplashScreen sPlashScreen = this;
mSplashThread =  new Thread(){
@Override
public void run(){
try {
synchronized(this){
// Wait given period of time or exit on touch
wait(5000);
}
}
catch(InterruptedException ex){
}

finish();
Intent intent = new Intent();
intent.setClass(sPlashScreen, MainActivity.class);
startActivity(intent);

}
};

mSplashThread.start();

}

protected void onDestroy() {
super.onDestroy();
frameAnimation = null;
}

@Override 

public boolean onTouchEvent(MotionEvent evt) 



if(evt.getAction() == MotionEvent.ACTION_DOWN)

 { 

synchronized(mSplashThread)



mSplashThread.notifyAll(); 





return true; 

}



仔细看了下,与图片有关的就只有AnimationDrawable,然后查了下AnimationDrawable,使用它在动画结束跳转页面后要stop(),不然释放不了资源,顿时泪奔~,在startActivity(intent);后面添加frameAnimation.stop();
OK,解决了这个烦人的问题。这其实就是我的思路,当然,基础很菜,有些东西写的不像大牛们那么全,仅仅是为了做个记录并且给新手童鞋们一个参考而已。

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: