I am having a hard time trying to find a possible Memory leak. I've got an Activity which is doing some heavy work in the Background.
After some tasks, the app consumes too much memory. It seems that it does not get cleaned up properly.
This is the Activity in the default state:
If I run the task which the Activity is doing, more and more memory is allocated.
Activity after some work:
At first I thought this must be a Memory Issue, cause the GC can't properly free up the memory. As far as I know, the GC can free the memory, if there are no reference left to the Objects. Is that correct?
Now comes the part which confuses me:
If I run the GC from Android Studio, the memory is cleaned up properly and my Activity never gets closed. I just have to use the Android Studio GC when to much memory is allocated.
This is the one I mean:
In general the question is:
Why can the Android Studio GC clean up the memory properly and why doesn't it work properly with the automatic android GC?
I know this is a pretty general question. I just want to know, if there are different types of garbage collections or something like that.
Also calling System.gc();
doesn't clean up the memory properly.
Additional Info:
Moto G 2nd gen
Android 5.0.2.
The Garbage Collector in Dalvik uses the Concurrent Mark and Sweep algorithm mentioned earlier. unreferenced objects aren't reclaimed immediately. Instead, the gathering is delayed until all available memory has been exhausted, meaning that short-lived objects remain in memory tons longer after they become unused.
The garbage collector considers unreachable objects garbage and releases the memory allocated for them. During a collection, the garbage collector examines the managed heap, looking for the blocks of address space occupied by unreachable objects.
When a JVM runs out of space in the storage heap and is unable to allocate any more objects (an allocation failure), a garbage collection is triggered. The Garbage Collector cleans up objects in the storage heap that are no longer being referenced by applications and frees some of the space.
In Java, garbage collection is the process of managing memory, automatically. It finds the unused objects (that are no longer used by the program) and delete or remove them to free up the memory. The garbage collection mechanism uses several GC algorithms. The most popular algorithm that is used is Mark and Sweep.
Memory leaks can happen because of several reasons. One common reason are bitmaps which are not recycled correctly. Other seed of memory leaks is keeping the context in objects. For example you launch an async task and pass a context because you need it later. While the async task is running it keeps a reference to the context and so the whole activity is in memory. This is also very frequent with anonymous and inner classes which have a reference to the parent class which usually is a fragment or and activity.
I suggest you to use the library leak canary to spot memory leaks and use the Android tools to track the allocations in order to discover exactly where the memory leak is happening.
Perhaps you can try to explicitly call System.gc();
somewhere periodically in your heavy processing code?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With