Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Waiting for a blocking GC Alloc in Android studio logs

I have a pretty large double array of say 1,71,00,000 elements. I need to traverse through the array, slice the array in smaller arrays of 10k points each and the remaining points(if any) into the last slice.

I have a basic code:

public static boolean getSliceOfArray(double[] arr,int slice_len)
    {
        //arr: big array
        //slice_len: 10,000
        System.out.println(arr.length);
        int n_times=(arr.length/slice_len);
        int i=0;
        for(int n=0;n<n_times;n++)
        {
            Arrays.copyOfRange(arr,i,i+slice_len);
            i=i+slice_len;

        }
        System.out.println(n_times);

        if(i!=arr.length)
        {
            System.out.println( Arrays.copyOfRange(arr,i,arr.length).length);

        }

        return true;
    }

It gives me following output:

I/System.out: 17100000
I/xample.styleap: Waiting for a blocking GC Alloc
I/xample.styleap: WaitForGcToComplete blocked Alloc on HeapTrim for 13.428ms
    Starting a blocking GC Alloc
I/xample.styleap: Waiting for a blocking GC Alloc
I/xample.styleap: WaitForGcToComplete blocked Alloc on HeapTrim for 36.867ms
I/xample.styleap: Starting a blocking GC Alloc
I/xample.styleap: Waiting for a blocking GC Alloc
I/xample.styleap: WaitForGcToComplete blocked Alloc on HeapTrim for 26.012ms
    Starting a blocking GC Alloc
I/xample.styleap: Waiting for a blocking GC Alloc
I/xample.styleap: WaitForGcToComplete blocked Alloc on HeapTrim for 32.625ms
    Starting a blocking GC Alloc
I/xample.styleap: Waiting for a blocking GC Alloc
I/xample.styleap: WaitForGcToComplete blocked Alloc on HeapTrim for 21.645ms
    Starting a blocking GC Alloc
I/System.out: 1710

When I do the same with considerably smaller sized array the output is not like this. Why does it show "Waiting for a blocking GC Alloc" and these messages? Is it a serious issue? JVM issue or my logic error?

like image 436
Sayan Sen Avatar asked Oct 15 '22 03:10

Sayan Sen


2 Answers

Using android:largeHeap="true"in my manifest.xml solved this issue.

like image 198
Sayan Sen Avatar answered Oct 20 '22 21:10

Sayan Sen


Your Application is using too much Heap Memory(creating objects on the fly maybe and not getting Garbage collected) , and hence the Java GC is constantly trying to free up memory but it is not able to do it as your app is generating a lot of "leak". So basically your app memory requirements are increasing constantly and hence GC is warning you about it as it will eventually lead to a crash

You should:

  1. Use Android Studio Memory Profiler to analyze the memory allocation
  2. Analyze the Heap Dump
  3. Check for Memory Leaks ( one reason could be too many static variables!)

System.gc() ( as suggested by someone earlier) will not help as you cant guarantee/force GC

Rather than using the flag to increase the Heap Size in JVM args, I would suggest improve/edit your own codebase first.

like image 30
Abhinav Ghosh Avatar answered Oct 20 '22 21:10

Abhinav Ghosh