I run OutOfMemoryErrors in the application I'm working on, and it's hard for me to figure out how to fix it and what the problem is. I am using the FragmentStatePagerAdapter as this seems like the best / recommended alternative. I also use Universal Image Loader v.1.8.4 . It happens that when you look at views, the heap grows and eventually ends up in memory. Here is a small conclusion:
05-06 14:38:23.096: D/dalvikvm(29322): GC_CONCURRENT freed 187K, 5% free 7532K/7868K, paused 7ms+13ms, total 37ms 05-06 14:38:23.176: D/dalvikvm(29322): GC_FOR_ALLOC freed 96K, 4% free 7656K/7920K, paused 17ms, total 17ms 05-06 14:38:23.206: D/dalvikvm(29322): GC_FOR_ALLOC freed <1K, 4% free 8234K/8500K, paused 15ms, total 15ms 05-06 14:38:23.246: D/dalvikvm(29322): GC_CONCURRENT freed 4K, 3% free 8262K/8500K, paused 5ms+5ms, total 31ms 05-06 14:38:23.276: D/dalvikvm(29322): GC_FOR_ALLOC freed 29K, 3% free 8289K/8500K, paused 20ms, total 25ms 05-06 14:38:23.286: D/dalvikvm(29322): GC_FOR_ALLOC freed <1K, 3% free 9342K/9556K, paused 15ms, total 15ms 05-06 14:38:23.316: D/dalvikvm(29322): GC_CONCURRENT freed 27K, 3% free 9327K/9556K, paused 3ms+2ms, total 22ms 05-06 14:38:24.626: D/dalvikvm(29322): GC_FOR_ALLOC freed 83K, 3% free 9462K/9696K, paused 15ms, total 16ms 05-06 14:38:24.646: D/dalvikvm(29322): GC_FOR_ALLOC freed 3K, 3% free 10201K/10440K, paused 16ms, total 16ms 05-06 14:38:24.666: D/dalvikvm(29322): GC_CONCURRENT freed 3K, 3% free 10214K/10440K, paused 3ms+3ms, total 26ms 05-06 14:38:24.976: D/dalvikvm(29322): GC_FOR_ALLOC freed 68K, 3% free 10291K/10508K, paused 16ms, total 17ms 05-06 14:38:24.986: D/dalvikvm(29322): GC_FOR_ALLOC freed <1K, 2% free 11297K/11516K, paused 15ms, total 15ms 05-06 14:38:25.016: D/dalvikvm(29322): GC_CONCURRENT freed 8K, 2% free 11305K/11516K, paused 3ms+4ms, total 24ms 05-06 14:38:25.346: D/dalvikvm(29322): GC_CONCURRENT freed 74K, 2% free 12458K/12680K, paused 2ms+3ms, total 23ms 05-06 14:38:25.976: D/dalvikvm(29322): GC_FOR_ALLOC freed 151K, 3% free 13485K/13792K, paused 22ms, total 22ms 05-06 14:38:26.276: D/dalvikvm(29322): GC_CONCURRENT freed 95K, 2% free 15384K/15636K, paused 3ms+3ms, total 26ms 05-06 14:38:27.196: D/dalvikvm(29322): GC_FOR_ALLOC freed 222K, 3% free 17111K/17484K, paused 24ms, total 24ms 05-06 14:38:28.446: D/dalvikvm(29322): GC_FOR_ALLOC freed 247K, 2% free 19784K/20180K, paused 22ms, total 22ms 05-06 14:38:29.396: D/dalvikvm(29322): GC_FOR_ALLOC freed 248K, 2% free 23137K/23540K, paused 23ms, total 24ms 05-06 14:38:30.976: D/dalvikvm(29322): GC_FOR_ALLOC freed 391K, 2% free 27770K/28308K, paused 29ms, total 29ms 05-06 14:38:33.366: D/dalvikvm(29322): GC_CONCURRENT freed 516K, 2% free 33964K/34628K, paused 3ms+6ms, total 45ms 05-06 14:38:36.126: D/dalvikvm(29322): GC_FOR_ALLOC freed 608K, 2% free 41164K/41920K, paused 37ms, total 37ms 05-06 14:38:38.396: D/dalvikvm(29322): GC_CONCURRENT freed 598K, 2% free 48739K/49484K, paused 4ms+10ms, total 59ms 05-06 14:38:41.496: D/dalvikvm(29322): GC_CONCURRENT freed 723K, 2% free 56176K/57044K, paused 2ms+13ms, total 71ms 05-06 14:38:41.496: D/dalvikvm(29322): WAIT_FOR_CONCURRENT_GC blocked 62ms 05-06 14:38:45.176: I/dalvikvm-heap(29322): Clamp target GC heap from 69.393MB to 64.000MB 05-06 14:38:45.176: D/dalvikvm(29322): GC_FOR_ALLOC freed 597K, 2% free 62724K/63476K, paused 56ms, total 56ms 05-06 14:38:45.936: I/dalvikvm-heap(29322): Clamp target GC heap from 71.379MB to 64.000MB 05-06 14:38:45.936: D/dalvikvm(29322): GC_FOR_ALLOC freed 216K, 1% free 64758K/65412K, paused 50ms, total 50ms 05-06 14:38:45.996: I/dalvikvm-heap(29322): Clamp target GC heap from 71.338MB to 64.000MB 05-06 14:38:45.996: D/dalvikvm(29322): GC_BEFORE_OOM freed 43K, 2% free 64714K/65412K, paused 60ms, total 60ms 05-06 14:38:46.336: I/dalvikvm-heap(29322): Clamp target GC heap from 71.462MB to 64.000MB 05-06 14:38:46.336: D/dalvikvm(29322): GC_FOR_ALLOC freed 139K, 2% free 64841K/65500K, paused 52ms, total 52ms 05-06 14:38:46.396: I/dalvikvm-heap(29322): Clamp target GC heap from 71.462MB to 64.000MB 05-06 14:38:46.396: D/dalvikvm(29322): GC_BEFORE_OOM freed <1K, 2% free 64840K/65500K, paused 62ms, total 62ms
First of all, yes, I did a lot to dig at stackoverflow and other sites, trying to figure out what the problem is. I tried various solutions that process bitmaps, use unbindDrawables and try to remove views / process bitmaps in destroyItem () PagerAdapter ... to name a few. None of them worked for me (I can provide more detailed information about what I did, if necessary).
So, in a nutshell, I can reproduce my problem using a modified version of this example (from the Google Developers site). I changed it a bit to work with Universal Image Loader v.1.8.4 , and also made the layout a bit more complex to mimic somewhat what I have in the application I'm working on. The reason I changed the layout was because everything works fine if you make the layout very simple and include only the image and some text fields. When the layout becomes more complex, and when you start adding additional pages to the adapter, you start working with OOM errors. However, I'm not sure if this is a problem with the layout or UIL ... read on ...
I ran MAT in Eclipse, and here is what it says under a single Leak Suspects post (only 1 problem):
One instance of "com.example.android.animationsdemo.ScreenSlideActivity$ScreenSlidePagerAdapter" loaded by "dalvik.system.PathClassLoader @ 0x42124098" occupies 56,171,048 (86.62%) bytes. The memory is accumulated in one instance of "java.lang.Object[]" loaded by "<system class loader>". Keywords java.lang.Object[] dalvik.system.PathClassLoader @ 0x42124098 com.example.android.animationsdemo.ScreenSlideActivity$ScreenSlidePagerAdapter
If you then view the Dominators Tree and expand ScreenSlidePagerAdapter, it will display 25 of 59 entries for the following:
> java.util.ArrayList @ 0x4216ccf0 > java.lang.Object[60] @ 0x44b07d8 > android.app.Fragment$SavedState @ 0x439bd038
I am not an expert in this tool, and I'm pretty new to Android and development in general, but it seems that the saved state for the adapter is not cleared correctly and that causes the OOM problem (it saves the whole view, not just bitmap images). However, I could not figure out how to clear / release / destroy / regardless of the stored state data or if I am doing something wrong elsewhere. I came across one message that tried to use .setSaveEnabled(false) , but that didn't make any difference, I still had OOM errors. I even tried overriding saveState () for the adapter and returning null, and that didn't matter either.
Here is the relevant code / configuration information which hopefully answers most of the questions:
For UIL, I just use the default configuration, since the changes do not seem to change:
imageLoader.init(ImageLoaderConfiguration.createDefault(this));
ScreenSlideActivity:
public class ScreenSlideActivity extends FragmentActivity { private static final String[] IMAGES = Constants.IMAGES; private ViewPager mPager; private PagerAdapter mPagerAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_screen_slide);
ScreenSlidePageFragment caused by the above activity:
public class ScreenSlidePageFragment extends Fragment {
The layout activity_screen_slide.xml used by ScreenSlideActivity and id for ViewPager:
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/pager" android:layout_width="match_parent" android:layout_height="match_parent" />
fragment_screen_slide_page.xml used by ScreenSlidePageFragment:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/content" android:layout_width="match_parent" android:layout_height="match_parent" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:padding="16dp" > <TextView android:id="@android:id/text1" style="?android:textAppearanceLarge" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="16dp" android:textStyle="bold" /> <TextView style="?android:textAppearanceMedium" android:layout_width="match_parent" android:layout_height="wrap_content" android:lineSpacingMultiplier="1.2" android:text="@string/lorem_ipsum" /> <TextView android:id="@+id/item_details" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:gravity="center_vertical" android:text="@string/item_details" /> <TextView android:id="@+id/item_type_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:text="@string/item_type_text" /> <Spinner android:id="@+id/item_type" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:prompt="@string/item_type_text" android:text="@string/item_type" /> <TextView android:id="@+id/item_name_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:text="@string/item_name_text" /> <EditText android:id="@+id/item_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:gravity="top|left" android:hint="@string/item_name" android:inputType="text" /> <TextView android:id="@+id/item_text_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:text="@string/item_text_text" /> <EditText android:id="@+id/item_text" android:layout_width="wrap_content" android:layout_height="100dp" android:layout_marginLeft="10dp" android:ems="10" android:gravity="top|left" android:hint="@string/item_text" android:inputType="textMultiLine" /> <TextView android:id="@+id/sound_edit_text" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:gravity="center_vertical" android:text="@string/sound_edit" /> <ImageView android:id="@+id/item_add_audio" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="10dp" android:contentDescription="@string/cd_image_add_button" android:src="@android:drawable/ic_menu_add" /> <TextView android:id="@+id/recording_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginBottom="20dp" android:layout_marginTop="20dp" android:text="@string/recording_text" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:padding="16dp" > <ImageButton android:id="@+id/item_play_audio" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="33" android:contentDescription="@string/cd_play_sound_button" android:soundEffectsEnabled="false" android:src="@drawable/play" /> <ImageButton android:id="@+id/item_stop_audio" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="34" android:contentDescription="@string/cd_stop_sound_button" android:soundEffectsEnabled="false" android:src="@drawable/stop" /> <ImageButton android:id="@+id/item_record_audio" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="33" android:contentDescription="@string/cd_record_sound_button" android:soundEffectsEnabled="false" android:src="@drawable/rec" /> </LinearLayout> <TextView android:id="@+id/edit_image" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:gravity="center_vertical" android:text="@string/edit_image" /> <TextView android:id="@+id/image_replace_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginBottom="10dp" android:layout_marginTop="10dp" android:text="@string/image_replace" android:visibility="visible" /> <ImageView android:id="@+id/item_image_to_display" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:adjustViewBounds="true" android:contentDescription="@string/cd_item_image" /> <TextView android:id="@+id/item_image_effect_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:text="@string/item_image_effect_text" /> <Spinner android:id="@+id/item_image_effect" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" /> <TextView android:id="@+id/item_text_color_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:text="@string/item_text_color_text" /> <Spinner android:id="@+id/item_text_color" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" /> <TextView android:id="@+id/item_text_size_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:text="@string/item_text_size_text" /> <EditText android:id="@+id/item_text_size" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:hint="@string/item_text_size" android:inputType="number" /> <TextView android:id="@+id/item_text_style_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:text="@string/item_text_style_text" /> <Spinner android:id="@+id/item_text_style" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" /> <TextView android:id="@+id/item_text_alignment_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:text="@string/item_text_alignment_text" /> <Spinner android:id="@+id/item_text_alignment" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" /> <TextView android:id="@+id/item_background_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:text="@string/item_background_text" /> <Spinner android:id="@+id/item_background" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" /> </LinearLayout> </ScrollView>
Also, here is the OOM error that I am encountering:
05-06 16:37:59.656: E/ImageLoader(3175): null 05-06 16:37:59.656: E/ImageLoader(3175): java.lang.OutOfMemoryError 05-06 16:37:59.656: E/ImageLoader(3175): at android.graphics.BitmapFactory.nativeDecodeStream(Native Method) 05-06 16:37:59.656: E/ImageLoader(3175): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:529) 05-06 16:37:59.656: E/ImageLoader(3175): at com.nostra13.universalimageloader.core.ImageDecoder.decode(ImageDecoder.java:82) 05-06 16:37:59.656: E/ImageLoader(3175): at com.nostra13.universalimageloader.core.LoadAndDisplayImageTask.decodeImage(LoadAndDisplayImageTask.java:270) 05-06 16:37:59.656: E/ImageLoader(3175): at com.nostra13.universalimageloader.core.LoadAndDisplayImageTask.tryLoadBitmap(LoadAndDisplayImageTask.java:224) 05-06 16:37:59.656: E/ImageLoader(3175): at com.nostra13.universalimageloader.core.LoadAndDisplayImageTask.run(LoadAndDisplayImageTask.java:149) 05-06 16:37:59.656: E/ImageLoader(3175): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080) 05-06 16:37:59.656: E/ImageLoader(3175): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573) 05-06 16:37:59.656: E/ImageLoader(3175): at java.lang.Thread.run(Thread.java:856)
What can I see / change to get rid of OOM errors?