Xamarin Profiler - Memory allocated - android

I want to optimize my Xamarin.Forms app, so I am starting to use Xamarin Profiler, and I can see that the initial memory allocated in my Android app is always around 50MB.
The allocated memory is 50MB when the app starts, and it starts to increase around +1.6MB when I do push and around +0.2MB when I pull a page.
Of course, the more I navigate, the app goes slower and slower.
But I don't know if this amount is OK or if the app is consuming too much memory.
Guys, could you orient me a bit more about this? I am using MVVMCross (as MVVM framework) and I am not showing images in the MainPage.

Allocating +1.6MB when navigate to page is normal. But it should be released after navigating back and GC calling.
If you are not caching the pages all memory should be released and you should not see 0.2MB added after pulling a page, so I think you do have a memory leaks. Making memory snapshots and comparing between them can help you to detect memory leaks.
To check where memory leaks could be do the next steps:
Because Profiler manual snapshot is not working in latest (0.32) version set Auto Snapshot in Tools -> Options -> SnapShots. I recommend to set it to GC option.
Make snapshot (call GC if you set autosnapshot to GC option)
Navigate to Page you want to check memory leak.
Navigate back
Make snapshot again,
Compare between two snapshots and check what wasn't released
Try this link for more info.
I hope this post is helpful for you.

Related

Free RAM in Android

I'd like to know some simple code that allows for freeing used memory that is no longer needed, in a similar way as a lot of memory freeing apps do.
Yes, I'm aware that this shouldn't be neccesary because Android manages memory on its own, but it looks like what's causing a non desired behavior in my app is having a lot of opened app occupying memory, so I think this is worthwhile to try, and check if the error happens any longer.
Could anyone hand me such a code? I'm not able to find any.
What I gather from the article is that you don't need to do anything to reclaim memory, but you can make garbage collection happen quicker and at specific times. What this means to me is that any arrays, Lists, large objects, etc. should be set to null when you are done with it. Granted, this should be done automatically when you leave a method or a View, but in case you are in a long running loop or staying on a page with lots of data hanging around, you can clean it up a little faster.
The Android Runtime (ART) and Dalvik virtual machine use paging and memory-mapping (mmapping) to manage memory. This means that any memory an app modifies—whether by allocating new objects or touching mmapped pages—remains resident in RAM and cannot be paged out. The only way to release memory from an app is to release object references that the app holds, making the memory available to the garbage collector. That is with one exception: any files mmapped in without modification, such as code, can be paged out of RAM if the system wants to use that memory elsewhere.
https://developer.android.com/topic/performance/memory-overview
You can also check your memory usage to see if that's really the problem. This is linked in the article above, but I thought I'd pop it out so it's easier to notice.
https://developer.android.com/reference/android/app/ActivityManager.html#getMemoryClass()

Xamarin Android profiler memory dump?

For the few days, I've been trying to find out why my app allocates massive amounts of memory and crashes. I'll open a few layouts that admittedly is not optimized. But I cant figure out why sometimes StartActivity() will allocate up to 80MB at a time, and why that memory wont release when I close the activity.
I created a test application filled with images and two layouts that call each other. Using Xamarin Profiler, I came across this memory dump, and I cant figure out what is calling this and how can I have it called more often for my real app?
Please, any insights would be lovely.
I cant figure out what is calling this and how can I have it called more often for my real app?
The Memory Profiler in Android Studio can help you to see how your app allocates memory over over time. It could also be used for xamarin android app.
And try to use IComponentCallbacks2 interface and implement Application.OnTrimMemory to incrementally release memory based on current system constraints.
You could refer to Manage your app's memory for more information.

Added memory in android monitor Memory management in android

I have this application that, once I go through, there is approximately 4mb that gets added once redirected to the home page(home activity).
I have bitmaps and I do release them, I nullify all member variables at the onDestroy. I have used the GC in the android studio but it does not come back to around the same memory size of the first time that activity was called.
The process of my application is :
Login -> Home -> Image Capture -> Summary (click done at this point to return to Home)
Doing so add's approximately 4mb every run of this flow in the android monitor when you are back to the Home activity.
Is this normal, am I missing something? I have tried with leakcanary but I have not received anything from it(and yes I know its setup because I get the initial access request).
I think this is ridiculous because someone can use this application for 10times or maybe 20 000 times before resetting it and 20 000x 4mb that is way too big.
While LeakCanary can be useful to find some memory leaks, I've found that Android Studio's tools are better (in my humble experience). I recommend you use the Android Studio's HPROF Analyzer to try and find your leak.
Go to the Memory Monitor do the chain of events that cause a leak and then click the Dump Java Heap button. After it's done it should automatically open your dumped heap snapshot. There, on the top right you should see the Analyzer Tasks tab, click it and run a memory leak detection.
Unfortunately without some code to look at this is the best advice I can give you.

React-Native Memory Issues in Android

I'm running my project build on React-Native on android device and as I scroll down the list the app memory continuously increases. Even after navigating to other screen, the memory is not freed up in case of android while a lot of memory is freed in case of ios device. The investigation for the android was done on the android activity monitor and the video for same is available here
How can the memory be freed up in case of android ?
That's the standard behavior of the Android library Fresco used by React Native under the hood. Fresco caches all images in memory to the max authorized by the system. It's a cache, so images stay in memory even if they are removed from the screen, just in case, for later use.
When the app needs memory space, the GC is able to "scrap" this cache. That's what happen at 0'24'' in your screen recording (you can see a small "drop" of the allocated memory).
This article explains the memory management in details.
In conclusion, a React Native app using images will always use almost all the memory allowed by the system. The memory is gradually freed for the needs of the app.
At the moment there are known issues with react native ListView, check the issues related to listview here https://react-native.canny.io/feature-requests/p/fix-various-listview-issues. In the interim check this repo that tries to optimise listview for performance https://github.com/sghiassy/react-native-sglistview

android: How to manage memory between application starts (application failure on second run)?

Noob question!
My whiteboard/drawing app runs fine, using a combination of simple image views and bitmaps with me rendering a path to a bitmap and copying over as needed. I have it multitasking on my ICS Transformer without problems. However, if I exit the app with the Back button and then run it again, it fails; I get a memory error on the second run when I try to draw something.
Out of memory on a 4096016-byte allocation
Although sometimes I don't get that and it runs a second consecutive time. When I run it a third time, it works, and the fourth, again it Out-of-memory's.
What manual cleanup do I have to do when an Android app exits? Should I remove all created objects and bitmaps and paths and listeners and stuff?
Looks like you have a memory leak. Make sure you follow recommendations provided here. Often Memory Analyzer Tool is very useful in such cases. Here is a video how to use it.

Categories

Resources