I am using FragmentStatePagerAdapter along with ViewPager to hold my fragments. User will have one image by default and he can keep adding upto 10. When I add 10 pages, I am seeing lots of OutOfMemory issues at different places in my app code, most of them at while creating bitmaps. Also the background images are not getting loaded so I see balck bakground. I currently have setOffscreenPageLimit(10). Due to this all my fragments and it's views (I have many imageviews, textview etc in every fragments) are held in memory. It works fine even at this stage. But if I swipe through the pages few times or run any other feature which needs bitmaps to be created (which eventually need RAM) then it sucks. Sometimes it leads to ANR as it could not space to create even thread or crash due to OOM.
- Could you give me an idea, if you've used already, that if I can randomly change the setOffscreenPageLimit to lower value so that ViewPager will clear the rest of the fragments?
-I am thinking of changing the screen limit in onTrimLowMemory() in my Activity.Thinking I can handle any memory level - TRIM_MEMORY_RUNNING_MODERATE, _LOW or _CRITICAL. - I also got an idea to decide the number of off screen pages based on the available which I get through getMemoryClass() of ActivityManager.
- If I can change setOffscreenPageLimit value dynamically, how can I let ViewPager know that it is been modified.
- I read in the articles that android:largeHeap is of not that useful if device offered RAM is already low.Though it is allowed, it is not a good idea at all.
- Enabling these bits in ViewPageer would help ?
PERSISTENT_NO_CACHE - Used to indicate that no drawing cache should be kept in memory. - Or any other suggestion to avoid OOMs, especially in the context of ViewPager with FragmentStatePagerAdapter?
I could get these points triggered from this http://developer.android.com/training/articles/memory.html#Android and from other articales from stackoverflow related to memory.
Spare few minutes and throw some light.