简体   繁体   English

清空后从内存中清除图像资源

[英]Clear image resource from memory after nullifying

I have an Android Activity that needs to be held in memory for the lifetime of the app (don't ask why). 我有一个Android活动,需要在应用程序的生命周期中保留在内存中(不要问为什么)。 The background of this Activity takes up about 12MB of memory. 本活动的后台占用约12MB的内存。 I want to free up this memory space when this Activity isn't being viewed. 当不查看此活动时,我想释放此内存空间。 I tried adding the below code 我尝试添加以下代码

@Override
protected void onPause() {
    setBackground(null);
    super.onPause();
}


@Override
protected void onResume() {
    super.onResume();
    setBackgroundResource(R.drawable.bg);
}

But this results in the memory space still being allocated. 但这导致仍分配内存空间。 The below tree was retrieved via MAT, and as you can see, there is no direct reference to my app itself. 下面的树是通过MAT检索的,正如您所看到的,没有直接引用我的应用程序本身。

I suspect the Bitmap is somewhere in memory, waiting to be gc'd, but for whatever reason, it never does. 我怀疑位图在内存中的某个地方,等待gc,但是无论出于什么原因,它都永远不会。

在此处输入图片说明

FYI, I also tried the following 仅供参考,我还尝试了以下方法

private Drawable background;

@Override
protected void onResume() {
    background = activity.getResources().getDrawable(R.drawable.bg);
    setBackground(background);
}

@Override
protected void onPause() {
    setBackground(null);
    background = null;
}

but that resulted in this madness 但这导致了这种疯狂

在此处输入图片说明

Turns out calling 原来打电话

((BitmapDrawable)background).getBitmap().recycle();

clears the data from memory, while nulling the object does not! 从内存中清除数据,而清空对象则不会!

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

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