Do we need to call the Garbage Collector manually ( System.gc() ) in Android?

By | July 28, 2017

This is a common question. Eventhough the Android System calls garabage collector at appropriate times, this usually will not be adequate. Because the system calls GC at intervals, most of the time it wont work perfect and causing unnecessary pauses in the application.
If these pauses are more than 16ms, then the user will notice some lag in the Android UI. Google is improving this in every release and in latest Android O, this is down to 0.4ms. This is a huge improvement. How ever there are situations in which we will run out of memory, in cases where a lot of Bitmaps are used which consumes a lot of memory.

So sometimes it is advised to call the GC manually such as…

VM out of memory error

Sometimes during development you may have encountered this error, in this case it is good to call System.gc() for invoking the garabage collector.
While creating objects, System.gc will be called automatically if needed, but not for creating bitmaps. It just fails.

Out of memory in android application is very common if we not handle the bitmap properly, The solution for the problem would be

if(imageBitmap != null) {
     imageBitmap.recycle();
     imageBitmap = null;
}
System.gc();

BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 3;
imageBitmap = BitmapFactory.decodeFile(URI, options);
Bitmap  scaledBitmap = Bitmap.createScaledBitmap(imageBitmap, 200, 200, true);
imageView.setImageBitmap(scaledBitmap);

In the above code Have just tried to recycle the bitmap which will allow you to free up the used memory space ,so out of memory may not happen.I have tried it worked for me.

If still facing the problem you can also add these line as well

BitmapFactory.Options options = new BitmapFactory.Options();
options.inTempStorage = new byte[16*1024];
options.inPurgeable = true;

But, Generally speaking, in the presence of a garbage collector, it is never good practice to manually call the GC. A GC is organized around heuristic algorithms which work best when left to their own devices. Calling the GC manually often decreases performance.

If you get an OutOfMemoryError then it’s usually too late to call the garbage collector…

An Android Developer says:

Most of the time, garbage collection occurs because of tons of small, short-lived objects and some garbage collectors, like generational garbage collectors, can optimize the collection of these objects so that the application does not get interrupted too often. The Android garbage collector is unfortunately not able to perform such optimizations and the creation of short-lived objects in performance critical code paths is thus very costly for your application.

Using large Heap

Sometimes using large heaps may solve the VM memory Error like this…

   <application
      ....
      android:largeHeap="true">

You can check below link the Best way to avoid OOM during Bitmap creation,

http://developer.android.com/training/displaying-bitmaps/index.html

Android 6.0 and above

System.gc() do not work on Art Android 6.0.1 Nexus 5x, So you should use Runtime.getRuntime().gc(); instead.

Leave a Reply

Your email address will not be published. Required fields are marked *