【问题标题】:Memory leaks caused by bitmaps in Android's layoutsAndroid 布局中的位图导致的内存泄漏
【发布时间】:2014-01-18 11:07:25
【问题描述】:

我试图弄清楚为什么我的应用程序的位图(仅在 xml 布局中使用)永远不会被回收,所以在用户浏览 10/12 页面后,应用程序崩溃并出现内存不足异常。

我尝试使用 Mat 分析内存使用情况,我可以在 Retained Heap 中找到每个位图。

以下是图像在 XML 布局中的使用方式(每个布局大约 1 个):

<ImageView
android:id="@+id/imageView"
android:layout_width="fill_parent"
android:layout_height="2000dp"
android:scaleType="fitStart"
android:src="@drawable/welcome"/>

这里是相关的活动代码:

 public void onCreate(Bundle savedInstanceState)
 {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.splash);

     ImageView but = (ImageView)findViewById(R.id.imageView2);

        View.OnClickListener listener = new View.OnClickListener() 
        {
            public void onClick(View view) 
            { 
                        Intent myIntent = new Intent(view.getContext(), EtaActivity.class);
                startActivity(myIntent);
                overridePendingTransition(0,0);    
                finish();
            }
        };

        but.setOnClickListener(listener); 
 }

public void onDestroy()
{
    super.onDestroy();
}

有什么想法吗?

【问题讨论】:

    标签: android memory-leaks bitmap garbage-collection


    【解决方案1】:

    使用图片加载器

    1) Universal image Loader

    【讨论】:

    • 这与在 XML 布局中包含图像的需求有何联系?当我必须在我的应用程序中动态加载图像时,我使用图像加载器之类的东西,而不是在使用静态图像作为背景时。
    • imageLoader.displayImage(imageUri, imageView);
    猜你喜欢
    • 2016-03-14
    • 1970-01-01
    • 2016-06-18
    • 2011-09-03
    • 2020-10-24
    • 1970-01-01
    • 2013-06-05
    • 2012-08-23
    • 2012-07-18
    相关资源
    最近更新 更多