7

I'm making a sketching app, mostly for fun. But i've run into a problem.

Within my app, the user can "flip" through sketches they've already drawn. These sketches are just 400x800 Bitmaps. But i'm constantly running into out of memory errors.

Sketches are saved to SD as soon as the user flips away from them, and they are then deallocated with...

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

So, only one or two Bitmaps should be loaded into memory at any given time, before they are saved and deallocated. And with a few debug prints, i've determined that this is true (max. two bitmaps in memory at the same time). My problem is that after 14 loads/deallocations, the program crashes with an OutOfMemoryError. Always 14, no matter how long i wait between "flips".

I've tried opening DDMS to see what the device's memory allocation is like. Strangely, every time an image is deallocated, memory goes to the "unknown" section, instead of the "free" section. This continues until there is only a sliver of "free" memory left, at which point the crash occurs (and the "unknown" memory goes to "free").

After a lot of googling, i've found a few resources which say that bitmaps should be deallocated as expected, and that Dalvik garbage collections always occur before an out of memory error. And a few miscellaneous pages that suggested that ALL memory used by a Bitmap is deallocated upon GC (native and JVM heap alike)

I should note, at this point, that i'm testing on two different phones. Motorola Droid X (stock image, Android 2.3.3), and Samsung Galaxy S3 (Cyanogenmod 9, Android 4.0.4). Both exhibit the exact same behavior. My android tools are up to date with all SDK versions, and i've checked a million times to make sure that nothing else in my code is doing any weird allocations (practically nothing is allocated after program startup).

So, how should i properly deallocate a Bitmap so that it is freed (not just "unknown"), and i can use the memory elsewhere? As a sort of supplemental question, what is this "unknown" memory pool, and what is it for?

EDIT: Grammar and spelling, added some notes on platform.

Community
  • 1
  • 1
Knetic
  • 2,099
  • 1
  • 20
  • 34
  • 1
    I've tried to reproduce your problem, but no luck so far - I can't see any "unknown" memory in DDMS. Are you refering to "unknown object" row displayed in the stats table, btw? Do you mind showing the code you use to create `Bitmap` instances? – Code Painters Nov 07 '12 at 19:31
  • How do you draw your Bitmap? Is it attached to a ImageView via setImageBitmap? – Freddroid Feb 15 '13 at 09:44
  • After the user flips, what happens to the imageview that held it ? – Sagi Antebi Feb 17 '13 at 23:23

1 Answers1

0

Use large image (also 1mb near). You may be use androidmanifest in application tag set large heap true.and than use custom View not use imageview (because too complex and need more than other views)

nurisezgin
  • 1,530
  • 12
  • 19