Finding Memory Leak in android app programmatically - android

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.

Related

what are LIBRARY LEAKS in leakcanary?

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.

How to analyze the memory-Leak in the android Studio

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/

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 detect memory leak

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

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