YumeYume
YumeYume

Reputation: 991

Memory leak with simple activities navigation

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

Upvotes: 0

Views: 1201

Answers (2)

YumeYume
YumeYume

Reputation: 991

Ok, good news (sort of). The problem isn't where I first thought it was. I put this memory problem aside for a few hours and keep working on the last features of the app. When testing my new code, I wasn't able to navigate more than three times between activities before getting an OOM. I was like "wait a second, I could switch for hours without error before". So I used Git to find the first commit with this memory leak problem. And it first appeared with a photo upload functionnality. So, my memory leak is probably because of some non-recycled bitmaps. There's a lot of bitmap memory errors that can be found on Stack and elsewhere on the internet, I should be able to figure it out. Thanks to everyone that replied !

Upvotes: 1

Hitesh Sahu
Hitesh Sahu

Reputation: 45062

Replace activities with fragment. Activities are heavy and I guess you are keep on craeting new activites on navigation . Before you know things will easily goes out of control and your app will reach OOM threshold. Keep single activity and replace fragments in that.

Here is a tutorial about fragment https://guides.codepath.com/android/Creating-and-Using-Fragments

Upvotes: 1

Related Questions