Any way to avoid garbage collection on these lines? Bitmap.createBitmap / new Canvas(bitmap)

by admin.androidsl...@googlemail.com » Tue, 14 Apr 2009 21:23:13 GMT


Sponsored Links
 Running these many times in my app loop :

Bitmap bitmap = Bitmap.createBitmap(320, 480, Config.RGB_565);
Canvas canvas = new Canvas(bitmap);

I'm thinking of caching either / both of these objects, but don't want
to get out of memory errors either!



--~--~---------~--~----~------------~-------~--~----~



Any way to avoid garbage collection on these lines? Bitmap.createBitmap / new Canvas(bitmap)

by Streets Of Boston » Tue, 14 Apr 2009 21:51:56 GMT


 If this bitmap is used for displaying only one item (image), why do
you create a new bitmap over and over again?

once:
mBitmap = Bitmap.createBitmap(320,480,Config.RGB_565);
...
...
many times:
Canvas canvas = new Canvas(mBitmap);
// clear canvas
// draw into canvas
...
..

On Apr 14, 9:22am, "admin.androidsl...@googlemail.com"


--~--~---------~--~----~------------~-------~--~----~


Sponsored Links


Any way to avoid garbage collection on these lines? Bitmap.createBitmap / new Canvas(bitmap)

by admin.androidsl...@googlemail.com » Tue, 14 Apr 2009 22:05:48 GMT


 I'm happy to keep a static or member reference to the Bitmap object
for future usage - but I'm worried if that will reduce the amount of
memory available to my app?

Alex





--~--~---------~--~----~------------~-------~--~----~



Any way to avoid garbage collection on these lines? Bitmap.createBitmap / new Canvas(bitmap)

by Streets Of Boston » Tue, 14 Apr 2009 22:57:07 GMT


 In one of my apps i have similar concerns. I cache a bunch of bitmaps
to improve the user experience (using Map). However, at some point in
my app, the app needs the memory (preparing for image-editing on full-
sized image).

This is what i did:
I wrote a class to which bitmap-caches can register them selves.
This class provides a static method called 'tryToReclaimBitmapMemory
()' which will loop over all the registered bitmap-caches and asks
them to clear their caches and, if possible, recycle their bitmaps.
After this it'll call 'System.gc()' for good measure :)

Whenever my app reaches a point where it needs as much (bitmap) memory
as possible, it will call this static method 'tryToReclaimBitmapMemory
()'.

In this way, i can keep cached bitmaps and i'm only zapping them when
really necessary.

On Apr 14, 10:05am, "admin.androidsl...@googlemail.com"




--~--~---------~--~----~------------~-------~--~----~



Other Threads

1. ListActivity loses selected item position if behind a submenu

I have a ListActivity backed by an Adapter, which is NOT a
SimpleCursorAdapter, but an extension of BaseAdapter. I create my own
view in the adapter's GetView() method, which builds up a list on-the-
fly.

Now I have a menu structure when this list is in focus. This menu
structure contains a submenu, too, and I have noticed that the
ListActivity's getSelectedItemPosition() returns -1 when I reach this
submenu. I also noticed that this value is correct when I activate the
top-level menu item that eventually "opens" the submenu (I queried the
afore-mentioned function in onMenuItemSelected() and it was fine). But
when onMenuItemSelected() gets called after the activation of a sub-
menu item getSelectedItemPosition() always returns -1.

Can anyone point out if my expectation is wrong to assume that a list
selection shall not change when it's behind other UI components? Is it
a bug in the system? How shall I overcome it? I could implement dirty
tricks such as saving the position temporarily for a later query, but
I'd like to know first if I'm doing something wrong and/or if it's a
system malfunction.

Thanks for your help in advance,

Tote

--~--~---------~--~----~------------~-------~--~----~

2. Adding padding between buttons in a LinearLayout

Hello Inder, nice to see you here. :-)

padding{Left,Top,Right,Bottom} is - like in CSS - within the
bounds/border of the widget you apply it to.

You can use layout_margin{Left,Top,Right,Bottom} attributes to
accomplish what you intend to do.

Cheers,

Christoph





--~--~---------~--~----~------------~-------~--~----~

3. Internet & maps on Emulator

4. String[]

5. Rotation Redux (resolved!)

6. Very Large Views

7. How to run more fast on Eclipse