Reputation: 37
When a new bitmap variable is created to have the same value as an original bitmap variable, does it build a copy thereby taking memory cache space for two bitmaps or it only returns the original bitmap cache whenever it is called within the app?
An illustration is this;
public static Bitmap originalBmp;
public static Bitmap copyBmp;
originalBmp = ......;
copyBmp = originalBmp;
My question now is this;
in the memory cache, if originalBmp takes 2MB, does calling copyBmp = originalBmp;
result in a total cache memory of 4MB or it maintains 2MB wherever yet copyBmp is called....just to know that there is no memory pile up with new bitmap variables taking values from originalBmp.
Upvotes: 0
Views: 30
Reputation: 1007399
In Java, originalBmp
and copyBmp
point to objects. We often say they "are" objects, but that is a bit of verbal shorthand.
In your case, when you execute copyBmp = originalBmp;
, both originalBmp
and copyBmp
will point to the same Bitmap
object, not each to its own copy of that object. So, your 2MB Bitmap
still consumes only 2MB, despite two fields pointing to it.
Note, though, that putting a 2MB Bitmap
into a static
field is dangerous. static
fields are "intentional memory leaks". That 2MB cannot be freed up for use by other objects until all references to the Bitmap
are gone. In your case, both originalBmp
and copyBmp
are static
, so they will always point to your 2MB Bitmap
, until you reset both of those fields to null
(or point them to some other Bitmap
). Be very careful when using static
fields that you do not wind up consuming too much memory.
Upvotes: 1