I profiled my Android App using Android studio profiler to see memory usage after opening an Activity. Before opening the activity, the memory usage was around 200 MB, after opening the Activity and close it again, the memory usage was around 300 MB. I had expected the memory usage to decrease to the number before opening the Activity after it was destroyed.
I have tried forcing garbage collection and capturing the heap dump but I did not find memory leaks. What caused the memory to not be released after the Activities are destroyed? Does it indicate any underlying problems related to the memory usage?
Here Firebase can get the most memory consuming interfaces, it can also display the time each interface and activity takes when the application is started, install it on the project and then open all the activities in your application to get information on which activities are using up the memory and how long it takes to load each screen.
When you open the activity, you will find the information in the Run app in console of Android Studio.
Related
I am new to android Profiler and still a noob at memory management. At first, I am having a notifications from LeakCanary so I fix them and successfully removed the leakage so the LeakCanary stops sending me notifications. But when I use the android Profiler I just noticed that the memory usage was rising bit by bit when I open a certain activity and then destroy it using finish(); method. Below are the sample screenshots of my Profiler when I open and close the OrdersActivity.
Here is the SS of total memory used before opening and closing the OrdersActivity
And here is the SS of total memory used after opening and closing the OrdersActivity
Also here is the stacktrace of the LeakCanary
I want to know if this occurrence of rising memory is normal? If not, is there a way to stop this? I am willing to show my codes If you need them in order to help me. I just didn't include it because it was too long.
LeakCanary will detect memory leaks, in the sense of objects that remain in memory when the activity is closed.
If you want to understand why memory usage is increasing as an activity is running, you can track allocations using the profile.
Edit: Also, it's hard to read a screenshot, but it seems to me that was is increasing when you start the activity is code (Code: Memory that your app uses for code and resources, such as dex bytecode, optimized or compiled dex code, .so libraries, and fonts.). In that case, that's perfectly normal: starting an activity need to load extra code (the activity itself, which is probably small, and all of required AppCompat if it hasn't been loaded yet). The usual strategy to test against memory leaks is to start and close the activity hundred times, and verify there is no memory increase between the 2nd time and the 100th time.
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.
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.
I was looking at the memory usage in android studio and noticed that every time I launch my app, the memory increases by a few MB. I've never got a OutOfMemory exception crash report from any of my supported devices. I'm not familiar with a lot of the tools included in the android sdk so I'm not sure if this signifies a memory leak or not.
In the android studio memory monitor, this is what the graph looks like after 3 launches:
As you can see, the memory increases a few MB each launch.
Does this mean I have a memory leak?
If so what steps can I take to find the source of the memory leaks?
To make sure your app doesn't have memory leaks you need to do two things:
Make a heap dump.
Analyze it with some memory analyzer.
The way to obtain a heap dump is described in the official documentation.
Once you have your dump, you can load it into the memory analyzer tool (I prefer YourKit, but you can pick up any analyzer you like) and look for leaked objects (the steps to do it can be found in the documentation of chosen memory analyzer).
Simple YourKit workflow: start the analyzer, open the heap dump via Ctrl + O. You'll see the screen similar to attached below:
Click the Inspections button on the left side and then Alt + A (or Run All button). The inspection you're interested in is Objects retained by inner class back references contained within Possible leaks group. Here I have only String objects, which can be considered normal. Any time you see here some of your own classes, you have a memory leak. You can click on your objects (if you have them listed here) to see which references it has and who references it to understand what's the leak source.
I have some problems with the memory usage of my android app and don't know what causes the high memory usage. When I start my app, it uses up to 40 mb ram (says DDMS) and when I open another app, my app gets immediately killed.
I read a lot about memory leaks and I'm unbinding drawables, running the GC and so on but my app still needs a lot of memory.
I have about 3mb resources in my app, but afaik they are loaded into ram on demand. Am I wrong? May this cause the 40mb of ram usage?
EDIT: I think I'm not having memory leaks because I can switch the orientation on each activity as often as I want and the app does not crash because of low memory. So it can't be a memoryleak, can it?
you need to do memory management into your android application, please free the resources which is no longer used, try to override onStop(), onDestroy(), onPause() methods of Activity which will keep track of activity stack.
in OnDestroy() method free your whole availed resources, so that another app can use the same resources again.
What data structures are you using? Very large data structures (long Lists, big graphs, big maps, etc) can quickly use up RAM.
It could also be that you're leaking the Context on orientation change in your app.
It could also be that your layouts are really badly designed along with some heavy data structures.
It's difficult to tell unless you describe a bit more about what your app tries to do.