Skip to content
Advertisement

The cause of blocking GC Allocation problem when starting an Android application

I have recently encountered an unusual problem with Garbage Collecter allocation when beginning my application from Android Studio. My code compiles without errors and the program doesn’t throw any exceptions on Runtime. Nevertheless, when I start my program, it doesn’t respond.

My Logcat shows:

JavaScript

Then such logs appear repeatedly, till the program stops responding completely. As I read here and here, such a problem can be caused by many objects being created. However, I got such a problem with a working build without any changes to the code. The only thing I did is that I created release APK of my program, but I do understand it is unlikely to be the cause.

I have profiled the program and it is shown, that the main allocation is for WeakHashMap and SafeIterableMap classes (more detailed image is here). Unfortunately, I don’t create any objects of these classes, but they might be allocated by the libraries I’m using, which are mainly from Jetpack libraries.

I have tried to increase heap size, but it didn’t help.

gradle.properties:

JavaScript

build.gradle (for module: app)

JavaScript

In addition, I am using Nox emulator.

Therefore, is there any solution to this problem? If any extra profiling or code is needed, I’m ready to provide it.

Advertisement

Answer

Fortunately, I was able to solve this issue. After some time of research, I decided to debug my program to see the problematic part. Quite unexpectedly, the problem appeared to be in LiveData I was using. As it was profiled, there has been a great amount of Iterator objects being allocated. To be more precise, it was in void dispatchingValue(ObserverWrapper ...) method:

JavaScript

In one of my fragments, I was setting the value of MutableLiveData to null. It has lead to an infinite loop in dispatchingValue() method. That is why my profiler showed too many WeakHashMap objects, which were in fact created in LiveData.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement