How does Android Studio initiate the garbage collector and how does it work?

I find it difficult to find a possible memory leak. I have an Activity that does the hard work in the background.

After some tasks, the application consumes too much memory. It seems that it is not being cleaned properly.

This action is in default state:

enter image description here

If I run the task performed by the Activity, more and more memory is allocated.

Activity after some work: enter image description here

At first, I thought it was a memory problem, because the GC cannot properly free the memory. As far as I know, GC can free memory if references to objects are missing. It is right?

Now comes the part that bothers me:

If I start GC from Android Studio, the memory is cleared properly and my activity never closes. I just need to use Android Studio GC when a lot of memory is allocated. enter image description here

This I mean:

enter image description here

In general, the question is:

Why can Android Studio GC properly clear the memory and why it does not 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 collectors or something like that.

Also call System.gc(); Does not clear memory properly.

Additional Information:

Moto G 2nd gen

Android 5.0.2.

+7
java garbage-collection android
source share
2 answers

Memory leaks can occur for several reasons. One common cause is bitmap images that are not properly processed. Other seeds of memory leaks maintain context in objects. For example, you run the async task and pass in the context because you need it later. While the asynchronous task is running, it retains a reference to the context, and therefore all activity is in memory. It is also very common with anonymous and inner classes that have a reference to the parent class, which is usually a fragment and / or activity.

I suggest you use the canary leak library to detect memory leaks and use the Android tools to track distribution to find out exactly where the memory leak is.

+4
source share

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

+2
source share

All Articles