Potential Causes of Android Memory Leaks

I use leakcanery to search for memory leaks in Android. I detected and fixed all Activity leaks. (surprised to learn that there were so many BTW!). I also added a refWatcher watch to all my Fragment s.

Question 1: Is there anything else I should observe that could cause a noticeable memory leak?

Question 2: Does not view the Fragment redundancy leak, since a Fragment contains a link to the Activity ? I get a notification anyway, right ?: - /

Question 3: When I check the memory monitor in android studio, it shows the increase in memory usage over time. Is this a sign of a giant memory leak or is the Android OS kind and does it just give me more memory? How can I find for sure?

+6
source share
1 answer

Is there anything else I should observe that could cause a noticeable memory leak?

  • Declaring a field on a static element almost guarantees a memory leak.
  • Anonymous classes that go beyond the life of the parent class, such as Volley Request s, also cause a memory leak, since they contain an implicit reference to the parent Activity , and if the Activity gone before the request call ends, a memory leak occurs.

Doesn’t view Fragment leak redundancy, since a Fragment contains a link to Activity ?

A Fragment does not "hold" a link to an Activity . Link provided by FragmentManager . But the structure manages this internally, so you do not need to worry about it.

When I check the memory monitor in android studio, it shows the increase in memory usage over time. Is this a sign of a giant memory leak or is the Android OS kind and does it just give me more memory? How can I find for sure?

Application memory growth is natural, and memory is cleared during subsequent passes of the garbage collector. In languages ​​where there are virtual machines and automatic garbage collectors, the programmer has virtually no control over memory allocation. Besides creating tiny memory leaks, very little programmer can do to spoil the memory management process.

+5
source

Source: https://habr.com/ru/post/987844/


All Articles