android - Should I trust the Garbage Collector after calling Bitmap.recycle()? -
android - Should I trust the Garbage Collector after calling Bitmap.recycle()? -
i have code loading image opengl texture. in process, end loading 3 bitmaps, since need load original bitmap (sized appropriately display) , reorient bitmap based on exif data. i'm calling .recycle()
on each bitmap, i'm noticing memory doesn't seem change.
here's memory monitor shows:
as can see, after loading image i'm using 60mb of memory. when rotate device drops off bit comes up. leads me think there no leak, since memory never goes above that.
when click gc button in memory analyzer, memory footprint drops dramatically around 8 mb. makes sense 3 bitmaps created during process recycled, can garbage collected. can see when rotate 1 time again , activity rebuilt, memory jumps right up.
here's code show why many bitmaps created , when they're recycled.
void layoutimage() { ... bitmap bitmap = loadorientedconstrainedbitmapwithbackouts(...); imagetexture = new gltexture(bitmap); bitmap.recycle(); // recycle bitmap 2 } bitmap loadorientedconstrainedbitmapwithbackouts(context context, uri uri, int maxsize) { ... bitmap bitmap = loadbitmapwithbackouts(context, uri, samplesize); // create bitmap 1 ... bitmap out = orientbitmap(bitmap, orientation); // create bitmap 2 bitmap.recycle(); // recycle bitmap 1 homecoming out; } bitmap orientbitmap(bitmap source, int orientation) { ... homecoming bitmap.createbitmap(bitmap, 0, 0, bitmap.getwidth(), bitmap.getheight, matrix, true); // create bitmap 3 }
i'm not sure problem, speak, since memory isn't climbing (so no leak), i'm curious when stays high. since forcing garbage collection clears fine, should assume if scheme needs memory collected on next gc pass? it's been running whole time i've been writing , still sitting comfortably @ 60 mb.
question 1: can trust garbage collector take memory if needed?
also, if we're supposed judiciously recycling our bitmaps, why many of bitmap methods things "the new bitmap may same object source, or re-create may have been made." have check equality every time utilize methods recycle bitmap if it's different object?
question 2: when using bitmap creation methods, may or may not homecoming same bitmap or copy, need check source , output equality recycle source if it's copy?
edit:i have tried analyzing mat, using heap dump @ peak usage (should 60 mb), reports 18.2 mb of usage , nil unusual looking. reading things differently?
question 1: can trust garbage collector take memory if needed?
yes. if incoming references cleared, garbage collector take memory when needed (typically new allocation). calling recycle()
doesn't help process along or create happen faster.
the recycle()
method exists because bitmap
objects not counted against heap until android 3.0; method helpful assist gc since didn't otherwise have record of memory counted against heap. in 3.0+, memory tracked against heap bookkeeping isn't necessary anymore.
question 2: when using bitmap creation methods, may or may not homecoming same bitmap or copy, need check source , output equality recycle source if it's copy?
the createbitmap()
method homecoming same object if:
x
, y
both zero width
, height
match source width , height no transformation matrices have been applied since looks passing in transformation matrix, re-create unless matrix identity reason. again, no real need recycle()
unless still supporting 2.x versions.
android memory memory-leaks bitmap garbage-collection
Comments
Post a Comment