My app is consuming a lot of memory
I have used the following app to measure my app memory consumption
https://play.google.com/store/apps/details?id=mem.usage&hl=en
can any one tell me how can I optimize the memory used by my app?
1) you can use LeakCanry for optimize memory. leakCanry will give you report for each individual leaks or when multiple instance of an activity will be on stack.
here is more info and integration guide https://github.com/square/leakcanary
2) you can check android studio memory monitor for your app's memory use. from that graph you can check when GC is being called and which activity or function is taking more memory so you can optimize through it.
more info regarding this http://developer.android.com/tools/performance/memory-monitor/index.html
3) i have gone through udacity tutorial and videos. you can see how to improving rendering for your layouts
Need to reduce unnecessary views.
dont apply unnecessary background.
more stuff https://www.udacity.com/course/android-performance--ud825
4) one more important thing use ApplicationContext where you can use it because Activity context keeps in stack and wont be eligible for GC until that context related task is being finished.
https://developer.android.com/tools/performance/comparison.html
Start with running Memory Profiler Tool to see what is the causes that leak/consume much memory in your app.
Related
Why my app freezes for some time on the first launch?
For an example animations of screen transitions or collapsing toolbar freezes on first showing time. If I kill the app completely and open it again there is no issues with performance. It happens even with release build type
If releasing a memory by killing your app helps to 'resolve' this issue, it a sign of memory leak. Do you use leak canary in your project (https://github.com/square/leakcanary)? If not, please add and test your app to figure out if there is a memory leak.
Freeze is also might mean some resource consuming task in the MainThread. Do you use a StrictMode? If not, please add it and see reports\crash -- it will help you to figure out if you or 3rd party library execute some time consuming operations in the MainThread. However even if your app passes StrictMode, you still can have other types of heavy operations, e.g. solving a complex differential equation, in the MainThread
Finally, Android Studio provides a good tools for app performance analysis. Please check them to find out less obvious performance issues.
There are many ways to freeze your app, the most common is to already have a heavy task run in long time at main thread, such as: load a large photo resource, call a restful api..etc
you should check more information at https://developer.android.com/topic/performance/vitals/tracking_jank
After I finished from my App, I did a test for Leak activity. I have found my App has a Leak but the leak is not showing all the time.
How to analyze the Reference Tree?
My Screenshots:
Analyzing memory leaks and fixing them is for every project different it is good to get the understanding about how to analyze them and how to solve them. There are some great tutorials available online, look for Memory leaks android, or Intellij detection/analyze. Enough material.
It is good to have the knowledge and know what the values means in the analyze screen. For instance https://examples.javacodegeeks.com/core-java/intellij-idea-profiler-example/
Is there something in Eclipse or possibly a 3rd party tool that lets me see how my app is using up memory while it is running? When my app runs, there appears to be something that over time starts eating up memory. It would be nice to see what class is responsible for it so that I can zoom in on the area that is causing the problem. It would be nice to see how memory is being used dynamically over time and what know what classes/methods are "leaking".
Traceview is there for the rescue. See the official documentation http://developer.android.com/tools/debugging/debugging-tracing.html .
You can check the memory leaks through it too. DDMS provides a UI for profiling also.
I have developed a small application which used a shared library. When i run that android application in my device heap memory is increasing rapidly. I try to reduce that using gc() but that is not work for me. At one time am getting a message like Low memory no more background process and my app quit. How could i resolve this?
You have not only to use GC, but also drop references to the objects you do not need anymore - GC will not reclaim referenced objects. You also have towatch carefully what JNI library does and also take necessary precautions in case it allocated memory of start threads.
More detailed answer is not posssible until you say what you are using and post the sources
Well, the title says it.
It would also be handy to know how many memory is still available.
I am writing a memory hungry application that tends to crash randomly (in native Code),
and my suspicion is that it gets out-of-memory.
I think you'd struggle to find a more comprehensive answer than this on the subject:
How do I discover memory usage of my application in Android?
i agree with hackbod's reply. As far as my understanding goes, your app wont crash , rather it will be killed.
you may find this discussion interesting
Is there a need to check for NULL after allocating memory, when kernel uses overcommit memory.
I guess there is some call back to know lowmemory conditions(onLowMemory()), you can use it to identify low memory conditions, I havent tried it though.
Applications on Android generally don't crash due to low memory. If you are using a lot of memory, you may cause most all other applications to be killed. If you keep on using memory, you may cause the system to kill your app as well (not crashing it, just killing it), though you will probably get to the point of noticeable paging before that happens.
If you are dealing with native code, the more likely explanation is that you are corrupting memory somewhere.
You might want to catch the OutOfMemoryException and then call System.gc() to perform a manual garbage collection, then retry that piece of code that failed. You might be able to use native exceptions with JNI to detect when the C++ code fails due to lack of memory, or anticipate.
Check http://kohlerm.blogspot.com/2010/02/android-memory-usage-analysis-slides.html to find out which java objects use the most memory