0

I think this question is asked often enough but after I did

bitmap.release;
bitmap = null;

in onDestroy of a Fragment, there is as much memory used as before.

The Fragment:

@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();
}
Johannes
  • 481
  • 1
  • 5
  • 16
  • Does something else have a reference to it? Is there a memory leak? DId the GC actually run? Any of these could be the problem. – Gabe Sechan Aug 13 '18 at 17:10
  • @GabeSechan The Bitmap is used in an Imageview. But before I recycle the Bitmap I set the imageBitmap from the imageView to null. And there is also no change after calling System.gc(); – Johannes Aug 13 '18 at 17:23
  • System.gc is a recommendation, it isn't enforced. But you could have a memory leak in your app in general keeping a reference to it alive somewhere. It also depends on your version of Android- where bitmap memory is kept has changed a few times. – Gabe Sechan Aug 13 '18 at 17:24
  • @GabeSechan I edited my question. There isn't much in the Fragment so I don't no where there is an memory leak... – Johannes Aug 13 '18 at 17:36

1 Answers1

1

Try using

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

For more info read a similar question