I'm currently developing a Xamarin.Android app, which contains a lot of activities, working with an API that return images and sometimes huge lists of objects, so I have to take care about memory to avoid OOM.
But, I finally managed to have some, when I started to work with Bitmaps. At first, I thought it was because of those bitmaps, so I ran some tests. And it appears it's not. I deactivated the API, so I work with empty lists of objects, and no images. And I still managed to have my app memory usage go crazy to eventually crash.
As I'm unable to install Xamarin Profiler on Visual Studio (no idea why, but not the main concern here), I installed an app on my device (which is a OnePlus One), called Intel Performance Viewer, that is able to monitor RAM usage of the device in real time. I launch my app, and let's say the memory usage is 1.1GB total out of my 2GB total memory. I click on a button that is supposed to show a list of objects, but as the API is down, the list is empty. So, the activity is containing a custom appbar, two buttons, an empty listview and a text that says "list is empty". RAM consumption rose from about 30MB calling this activity. Then I press my return button, which is calling a Finish(); on the activity, then I'm back on the main. And the RAM consumption goes down from like, one or two MB.
Then, if I go on that activity again, it will start, eating 30 more MB, and if I keep switching between activities like this, it will eventually leads to OOME.
I tried checking "Don't keep Activities" in my device developer options, and it seems like it's freeing more RAM (how logical), but still not everything. I have to switch a lot more before getting out of memory, but the problem is still there.
Isn't Finish supposed to put every data contained in the activity in the garbage collector ? I tried to do GC.Collect(); in my OnDestroy, no effect.
I don't know if I'm misunderstanding how memory works on android, as I'm fairly new to mobile dev, but this is giving me headaches. Any help ?
Thanks !
Edit : this is what I get when it goes OOM.
05-23 10:15:37.973 D/dalvikvm( 5049): GC_FOR_ALLOC freed 1587K, 3% free 248825K/256263K, paused 22ms, total 23ms
05-23 10:15:37.973 I/dalvikvm-heap( 5049): Grow heap (frag case) to 245.840MB for 2908172-byte allocation
05-23 10:15:38.029 D/dalvikvm( 5049): GC_CONCURRENT freed 127K, 2% free 2515
37K/256263K, paused 10ms+10ms, total 52ms
05-23 10:15:38.077 D/dalvikvm( 5049): GC_FOR_ALLOC freed 74K, 2% free 251463K/256263K, paused 23ms, total 23ms
05-23 10:15:38.077 I/dalvikvm-heap( 5049): Forcing collection of SoftReferences for 11632652-byte allocation
05-23 10:15:38.105 D/dalvikvm( 5049): GC_BEFORE_OOM freed 306K, 2% free 251156K/256263K, paused 29ms, total 29ms
05-23 10:15:38.105 E/dalvikvm-heap( 5049): Out of memory on a 11632652-byte allocation.
05-23 10:15:38.105 I/dalvikvm( 5049): "main" prio=5 tid=1 RUNNABLE
05-23 10:15:38.105 I/dalvikvm( 5049): | group="main" sCount=0 dsCount=0 obj=0xa62704b0 self=0xb7c7b510
05-23 10:15:38.105 I/dalvikvm( 5049): | sysTid=5049 nice=0 sched=0/0 cgrp=[fopen-error:2] handle=-1217084352
05-23 10:15:38.105 I/dalvikvm( 5049): | schedstat=( 7886788399 5740305249 19539 ) utm=596 stm=192 core=0
05-23 10:15:38.117 E/mono-rt ( 5049): =================================================================
05-23 10:15:38.117 E/mono-rt ( 5049): Got a SIGSEGV while executing native code. This usually indicates
05-23 10:15:38.117 E/mono-rt ( 5049): a fatal error in the mono runtime or one of the native libraries
05-23 10:15:38.117 E/mono-rt ( 5049): used by your application.
05-23 10:15:38.117 E/mono-rt ( 5049): =================================================================
05-23 10:15:38.117 E/mono-rt ( 5049):
05-23 10:15:38.117 F/libc ( 5049): Fatal signal 11 (SIGSEGV) at 0x00000000 (code=1), thread 5049