For Bitmaps, calling System.gc() can help quite a bit. The DalvikVM does not 'see' the raw binary data held by a Bitmap. The process may be getting close to out-of-memory while the DalvikVM thinks there is plenty.... the DalvikVM does not call a garbage collection when needed.
When an allocation happens and the process runs out of memory... kablooi. Calling System.gc() makes sure that the garbage collection is run and finalizers of de-referenced Bitmap instance are run so that the raw binary data can be released. In my experience, it is actually more 'effective' to call System.gc() just before you start allocating a big Bitmap, causing finalizers to run of Bitmaps that then clear/release 'hidden' raw binary data. This cleans up the process-memory beforehand, not after the fact (after an OOM-error). On Jun 9, 9:35 pm, Bob Kerns <[email protected]> wrote: > Carefully study the exception type hierarchy. Error and Exception are > distinct (and deliberately so). You are not catching OutOfMemoryError. > > But catching an OutOfMemoryError and retrying is unlikely to succeed. > The VM does GC before throwing the error. It's not entirely pointless, > however -- occasionally a second GC will be able to collect something > prior GC was not able to, possibly because of running finalizers or > similar issues. Putting the call to System.gc() there isn't a > requirement because of a deficiency in the VM, but rather a > desperation move for when in desperate straits. > > It is quite likely that if that GC call helps, you're doomed to have > another OutOfMemoryError soon, that you won't recover from. > > I'm NOT arguing against doing it, just putting it into context. It's > survival mode programming. > > On Jun 8, 11:39 am, Greg Donald <[email protected]> wrote: > > > > > I used this sort of code to make sure my bitmaps load: > > > try > > { > > foo = BitmapFactory.decodeResource( resources, R.drawable.foo ); > > } > > catch( Exception e ) > > { > > System.gc(); > > foo = BitmapFactory.decodeResource( resources, R.drawable.foo ); > > } > > > In 2.2, it's now failing sometimes and my catch block no longer > > catches anything. Why would that be? > > > Is there a better way to avoid a java.lang.OutOfMemoryError ? > > > Thanks, > > > -- > > Greg Donald > > destiney.com | gregdonald.com- Hide quoted text - > > - Show quoted text - -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/android-developers?hl=en

