繁体   English   中英

背景图像内存泄漏

[英]background image memory leak

我发现很少有主题描述类似的问题,但找不到非常简单的Android应用程序创建的内存泄漏解决方案:

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="cz.reloecc.testBackground"
          android:versionCode="1"
          android:versionName="1.0">
    <uses-sdk android:minSdkVersion="10"/>
    <application android:label="@string/app_name"
              android:icon="@drawable/ic_launcher">
        <activity android:name="TestBackgroundActivity"
                  android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
    </application>
</manifest>


main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:background="@drawable/ui">
</LinearLayout>


TestBackgroundActivity.java

public class TestBackgroundActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}


在更改设备的方向(NVIDIA Tegra Note 7)时,logcat注意:

cz.reloecc.testBackground I/dalvikvm-heap﹕ Grow heap (frag case) to 35.625MB for 12904976-byte allocation

每个周转大约增加了13MB(用于最大版本的图片)

最多达到我的堆最大容量(64MB):

cz.reloecc.testBackground E/dalvikvm-heap﹕ Out of memory on a 12904976-byte allocation.
cz.reloecc.testBackground I/dalvikvm﹕ at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
..
cz.reloecc.testBackground I/dalvikvm﹕ at cz.reloecc.testBackground.TestBackgroundActivity.onCreate(TestBackgroundActivity.java:13)


但! dpidrawable- [X | M | L]当我从抽拉-陆地[| | M L X]删除ui.png(其被设置为背景)的问题不会保留dpi的文件夹中的res文件夹..所以,如果我背景图片只有一个版本,我可以将设备打开一个星期。

这是我的问题:如何处理可绘制的多个版本(设置为背景)以避免内存泄漏?


//编辑:我设法进行了几次处置,回收,销毁,清空资源或其持有人的尝试,最后一次是基于Aeshang的建议:


=== 2.0版 ===

Resources.java

public class Resources {

    public Resources(Context context){
        this.context = context;
    }

    public Drawable getImage(int id){
        if(images.indexOfKey(id) < 0){
            Drawable drawable = context.getResources().getDrawable(id);
            images.put(id, drawable);
        }

        return images.get(id);
    }

    public void disposeImages(){
        int key;

        for(int i = 0; i < images.size(); i++) {
            key = images.keyAt(i);
            Drawable drawable = images.get(key);

            if(drawable instanceof BitmapDrawable){
                if(drawable instanceof BitmapDrawable){
                    Log.i(TestBackgroundActivity.LOG_TAG, "Recycling image " + key);
                    ((BitmapDrawable)drawable).getBitmap().recycle();
                }
            }
        }
    }

    public void disposeAll(){
        disposeImages();
        images.clear();
    }

    private SparseArray<Drawable> images = new SparseArray<Drawable>();
    private Context context;
}


TestBackgroundActivity.java

public class TestBackgroundActivity extends Activity {

    public static String LOG_TAG = "[TestBG]";

    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        resources = new Resources(getApplicationContext());
        LinearLayout mainLayout = (LinearLayout)findViewById(R.id.mainLayout);
        mainLayout.setBackgroundDrawable(resources.getImage(R.drawable.ui));
    }

    @Override
    protected void onDestroy(){
        resources.disposeAll();

        super.onDestroy();
    }

    private Resources resources;
}

使用第三方库加载可绘制对象

PicassoUniversal Image Loader

他们将自动处理大位图

来到绘画

用户使用1920X1080分辨率的图像并将其放置在drawable-xxhdpi文件夹中,一切正常

好,

这绝对应该工作。 在另外两个设备上进行了测试,而altrough应用程序则抱怨内存堆增长过快。 之后就来了。

我的整体笔记7是痛苦。 我将不得不找出这个地狱的正确来源。

// edit:很好,调用

System.gc();

在MainActivity的onDestroy()中

和应用程序永远存在..谁知道为什么?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM