Handle memory leak in android - android

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

Related

What is the NDK memory limit for an app on Android?

There are really old posts on SO that suggest that there is not real memory limit on Android NDK (Maximum native memory that can be allocated to an android app). Is this still true?
I have a memory intensive program written in c++ which I am calling on Android. This is taking quite an unusual amount of time to run. To me, this suggests that there is a memory hard limit that the program is using.
I would like an update on this since NDK memory related posts are quite old.
Why do not just try a "malloc()" using a very big number like 500MB or 1GB? It will be more easy and quick than ask a question and wait an answer somewhere...
However there isn't any limit to memory allocation using NDK (only using malloc and similar methods). Instead using reflection via NDK (like Java objects creation from NDK) follows normal JVM limits.

My android app consume a lot of memory

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.

Finding Memory Leak in android app programmatically

I have an android app developed in eclipse and i need to determine whether there is any memory leak in it. I need to achieve it without using any memory leak anaylzer tools, I want to write a piece of code for checking memory leak in the app. Is it possible? Any suggestion would help me.
You're in luck, someone at Square wrote a small library that finds all activity memory leaks https://github.com/square/leakcanary. I recommend turning it on only in debug build so you can find all leaks.

Profiling Android app for memory usage

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.

How to find out the memory footprint of my Android App?

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

Categories

Resources