Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does the Android Studio initiates the Garbage Collector and how does it work?

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:

enter image description here

If I run the task which the Activity is doing, more and more memory is allocated.

Activity after some work: enter image description here

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. enter image description here

This is the one I mean:

enter image description here

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.

like image 229
foxy Avatar asked Dec 14 '15 14:12

foxy


People also ask

How does the garbage collector work in Android?

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.

How does the garbage collector works?

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.

How is garbage collection triggered?

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.

How does the garbage collector work when does it get used?

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.


2 Answers

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.

like image 60
jorgemf Avatar answered Oct 15 '22 18:10

jorgemf


Perhaps you can try to explicitly call System.gc(); somewhere periodically in your heavy processing code?

like image 21
James Wierzba Avatar answered Oct 15 '22 18:10

James Wierzba