Out of memory in Android 2.3.2
I have an application which upon start request around 10-12KB of data开发者_开发技巧 from server (in onResume, I have a thread which starts to get data from server) and then paints it in tabular form on the view. Each row of view consist of 5 TextView
s and 2 Drawable
s.
Now, the application works perfectly fine on 2.2 and previous version of OS, but crashes on 2.3.2 with out of memory error in onCreate method (while setting the layout from R.main). The way to recreate the error is to keep on rotating the device (around 20-25 times), so that app keeps on switching from portrait to landscape mode. I looked at the DDMS output and I see a pattern. If I switch between portrait and landscape mode pretty fast ... system tries to run GC... showing messages such as
02-09 12:20:08.617: DEBUG/dalvikvm(109): GC_EXPLICIT freed 426K, 47% free 6201K/11655K, external 4752K/5934K, paused 122ms
But just before crashing it prints out lot of GC messages
02-09 12:20:12.875: DEBUG/dalvikvm(184): GC_EXTERNAL_ALLOC freed 112K, 52% free 3022K/6215K, external 5127K/5136K, paused 110ms 02-09 12:20:12.933: DEBUG/dalvikvm(28163): GC_EXTERNAL_ALLOC freed 108K, 34% free 7894K/11847K, external 20252K/20560K, paused 155ms 02-09 12:20:13.007: INFO/dalvikvm-heap(28163): Clamp target GC heap from 32.031MB to
32.000MB 02-09 12:20:13.007: DEBUG/dalvikvm(28163): GC_FOR_MALLOC freed 2K, 34% free 7891K/11847K, external 20254K/20560K, paused 47ms 02-09 12:20:13.074: DEBUG/dalvikvm(28163): GC_EXTERNAL_ALLOC freed 0K, 34% free ....
more such lines. and then crash.
I'm unable to understand why it crashes in one OS and not other. My device is a Samsung Nexus S. Any ideas? I ran the MAT tool on the Heap dump and I see 5-6 instances of main activity class all with some retained memory. Sshouldn't the memory be garbage collected?
I see 5-6 instances of main activity class...
You are leaking Activities. Do your worker threads keep references to Activities (or Contexts or View-derived types) by any chance?
I strongly urge you to read Retaining an Object During a Configuration Change in the SDK. It tells you exactly what you need to change.
The way to recreate the error is to keep on rotating the devise (around 20-25 times), so that app keeps on switching from portrait to landscape mode).
Most likely you're re-creating objects in onCreate
which gets called when the screen orientation changes. The re-creation of these objects eventually overwhelms the system. You may also have other memory leaks.
精彩评论