0

I need to pass the same Bitmap to a bunch of Objects which are later put into an ArrayList.

The Bitmap is 1024x512 pixels big and I'm always passing the same Bitmap Object to about ~1000 Objects.

I need to do this because every Object does something with the Bitmap later in the code.

Is this safe to do memory-wise?

  • 1
    How many times will you call new Bitmap? – Eric S. Apr 02 '15 at 17:55
  • 1
    And make sure to remove the objects from the list when they're no longer needed. "Forgetting" to remove obsolete objects stored in collections is a common way to produce memory leaks in Java. – JimmyB Apr 02 '15 at 18:03
  • @HannoBinder is calling ```.remove(xyz)``` enough? Or should I invoke the garbage collector? –  Apr 02 '15 at 18:59
  • @EricS. What do you mean by "call new Bitmap"? Only one ```Bitmap``` is created (using ```BitmapFactory.decodeFile```) and this ```Bitmap``` is passed to the Objects –  Apr 02 '15 at 19:00
  • Yes, removing object references from the collection is enough. The GC will then take care of releasing the memory when necessary. – JimmyB Apr 02 '15 at 20:44

1 Answers1

0

What you are trying to do is safe in the sense that it won't create 1000 bitmaps. Java won't pass around all 1024x512x3 bytes of bitmap, only the value of the reference of the one bitmap you are passing around.

Very important on how Java passes objects: java passing

Community
  • 1
  • 1
Eric S.
  • 1,502
  • 13
  • 31