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
Related
in leakcanary sometimes i am getting leaks reported as "Library leaks"
HEAP ANALYSIS RESULT
====================================
0 APPLICATION LEAKS
References underlined with "~~~" are likely causes.
Learn more at https://squ.re/leaks.
====================================
1 LIBRARY LEAKS
Library Leaks are leaks coming from the Android Framework or Google libraries.
Leak pattern: instance field android.view.ViewGroup$ViewLocationHolder#mRoot
Description: In Android P, ViewLocationHolder has an mRoot field that is not cleared in its clear() method. Introduced in https://github.com/aosp-mirror/platform_frameworks_base/commit/86b326012813f09d8f1de7d6d26c986a909d Bug report: https://issuetracker.google.com/issues/112792715
66264 bytes retained by leaking objects
Signature: 64becd25d6156daa91df6572a75b6a28ddb1
┬───
│ GC Root: System class
at the leakcanary website it says:
LibraryLeak at leakcanary website
LibraryLeak
data class LibraryLeak :Leak
A leak found by HeapAnalyzer, where the only path to the leaking
object required going through a reference matched by pattern, as
provided to a LibraryLeakReferenceMatcher instance. This is a known
leak in library code that is beyond your control.
is it really out of my control?
might there be something i did to cause it?
Is there anything i can do to prevent it?
leakcanry sometimes places a link to report this memory leak but i don't see any response, is it something that android normally working on? if so, how such issues normally solved and how to keep track?
if indeed there is nothing i can do to solve or prevent it, is there a way i can ask leakcanary to ignore LIBRARY LEAKS?
That's a great question, and in fact this should be better documented so I created an issue to track that: https://github.com/square/leakcanary/issues/1773
is it really out of my control?
Yes and no. Typically this means the leak isn't caused by a bug in your code or using an API incorrectly, it's caused by a bug in either Android X or the Android SDK. That being said, it's not always out of your controls, there can be tricks / hacks to work around leaks.
might there be something i did to cause it?
The ViewLocationHolder in Android P is an unfortunate bug that happens.. when you use views. So yeah, you didn't really do anything special.
Is there anything i can do to prevent it?
Potentially but no one has come up with a clear hack to work around it yet.
leakcanry sometimes places a link to report this memory leak but i don't see any response, is it something that android normally working on? if so, how such issues normally solved and how to keep track?
Not sure what you mean here. If you mean that the link to the bug report (https://issuetracker.google.com/issues/112792715) stopped working, yes that's because Google decide to prevent access.
if indeed there is nothing i can do to solve or prevent it, is there a way i can ask leakcanary to ignore LIBRARY LEAKS?
There's no way for LeakCanary to know whether a leak is a library leak or not prior to dumping the heap and doing the analysis. It's important to notify developers that the analysis is done (even if it only found a library leak and no app leak) because otherwise they wonder what happened to the analysis.
My customer raise a problem about our app which is the app is getting slower when using for long time, but our side cannot simulate the problem as we are not in their real working environment, we are not able to solve the problem before knowing the bug. anyone can help?
If it´s a issue that raises "by the passing of time", you should look for any task you might be running and repeating itself.
You should check the different types of functionalities you use.
For example, if your app it´s using a local database:
Look for unclosed cursors.
I/O work
If your are using animations:
Look for skipped frames
Complex view hierarchies
If using background tasks or threading:
I/O work
Unfinished threads
Increasing thread number
If using networking and Webservices:
Problems with your server database
Connection issues
Proxys
...
You could also use something like Firebase + Crashlytics to see if theres any warning raises but gets silently disposed.
Giving some more info about your app and what it does could be useful for elaborating more accurate solution.
A common source of slowing down apps is memory leaks.
on iOS what you can do is to activate Memory management flags on your target and keep a watch on the logs, you can also use Xcode Memory Graph and instruments leak tools to detect leaks.
on Android you can use tools like LeakCanary to be notified when leaks happens.
Once you have a leak detected you can inspect your code to identify the source and better manage memory.
#axierjhtjz mention lots of good starting points.
Does it occur on both iOS and Android, specific devices, vendors?
If it is mainly on a single platform (Android/iOS), on different devices and the main complaint is slowness over time, I would suspect the following:
Memory leak (maybe UI, fragments)
Data persistent data incremental over time or/and more IO operation over time.
If you unable to debug the problem on the customer site, I would suggest to add a remote real time monitoring capabilities over relevant parameters and logs.
A good tool we used in the past was TestFairy. maybe they have a free trial
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.
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
I faced some issues related to MemoryOutOfBounds exception in android. I found There are two reasons behind this
(1) Thread created are alive and not destroyed anywhere
(2) Memory leak.
We can detect thread information in eclipse. But how to know at which line of my code memory leak occurs when I execute my application. MAT works differently. MAT is static.
Is there any plug-ins or any way to know memory-leak in eclipse?
Thanks
Deepak
This might be useful.
What Android tools and methods work best to find memory/resource leaks?
You don't need a memory leak to get an OutOfMemoryError, simply using too much memory in your app will cause it.
You can have a look at the following links regarding how to avoid memory-leaks:
http://android-developers.blogspot.co.uk/2009/01/avoiding-memory-leaks.html
http://www.ibm.com/developerworks/java/library/j-leaks/
http://www.mindfiresolutions.com/How-to-avoid-Memory-leak-issue-in-Java-1001.php