【问题标题】:Android - Releasing memory of bitmapsAndroid - 释放位图的内存
【发布时间】:2019-01-20 10:34:41
【问题描述】:

我认为这个问题经常被问到,但在我问过之后

bitmap.release;
bitmap = null;

在Fragment的onDestroy中,使用的内存和以前一样多。

片段:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    initializeViews();
    croppedBitmap = BitmapFactory.decodeFile("/.../0001.jpeg");
    imageView.setImageBitmap(croppedBitmap);
    fabCreate.setOnClickListener(...); //never called
}

@Override
public void onDestroy() {
    clearMemory();
    super.onDestroy();

}
void clearMemory(){

    fabCreate.setOnClickListener(null);
    imageView.setImageBitmap(null);
    imageView = null;
    croppedBitmap.recycle();
    croppedBitmap = null;
    java.lang.System.gc();
}

【问题讨论】:

  • 还有其他的参考吗?有内存泄漏吗? GC 真的运行了吗?这些都可能是问题。
  • @GabeSechan 位图用于 Imageview。但在我回收位图之前,我将 imageBitmap 从 imageView 设置为 null。而且调用 System.gc(); 后也没有变化
  • System.gc 是一个建议,不是强制执行的。但是您的应用程序中可能存在内存泄漏,通常会在某处保持对它的引用。这也取决于您的 Android 版本 - 保留位图内存的位置已经更改了几次。
  • @GabeSechan 我编辑了我的问题。 Fragment 中没有太多内容,所以我不知道哪里有内存泄漏......

标签: java android memory memory-management


【解决方案1】:

尝试使用

if(bitmap != null){
   bitmap.recycle();
   bitmap = null;
}

更多信息请阅读similar question

【讨论】:

  • 应该达到什么目的?
  • 根据文档 bitmap.recycle();释放位图中使用的本机堆。而设置为null是为了协助GC快速收集你的reference
  • 正如我在问题中所说,这对我不起作用。但我不知道为什么。
猜你喜欢
  • 2013-09-21
  • 1970-01-01
  • 2015-09-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-02
  • 2014-06-13
  • 2017-09-22
相关资源
最近更新 更多