我遇到的内存泄露问题
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次,都没有程序崩溃的情况发生,那么问题范围缩小到了动画界面了。
动画界面代码:
@Override
public boolean onTouchEvent(MotionEvent evt)
{
if(evt.getAction() == MotionEvent.ACTION_DOWN)
{
synchronized(mSplashThread)
{
mSplashThread.notifyAll();
}
}
return true;
}
错误: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,解决了这个烦人的问题。这其实就是我的思路,当然,基础很菜,有些东西写的不像大牛们那么全,仅仅是为了做个记录并且给新手童鞋们一个参考而已。
相关文章推荐
- window.open 关闭窗口时,不需要弹出确认框的方法
- Fedora 22下ParaviewWeb安装
- TCP FLAG 标记
- 0/1背包问题与动态规划
- 服务器架构资料整理
- Python图形界面开发初步 4
- CF 616C The Labyrinth(dfs)
- UIScrollerView一直显示
- 小丁带你走进git的世界三-撤销修改(转)
- WCF NetTcpBinding 由于系统缓冲区空间不足或队列已满,不能执行套接字上的操作
- python核心编程-正则表达式-产生测试数据
- DecimalFormat 中的 # 与 0 的区别(中文帮助文档中翻译可能是错误的)
- HTTP状态管理机制之Cookie(转)
- iOS 企业账号申请
- linux 文件类型介绍
- Using Swift with Cocoa and Objective-C(第一部分)
- 苹果运用怎么实现一个动画的启动页面
- linux常用命令—— 系统管理(六)
- 拆分一个字符串,以大写字母作为标识
- python2编码总结(转)