How to explicitly free memory in android OR Release unwanted memory in Android?

By | April 24, 2012

If you are writing memory consuming applications there is a bigger chance that your application may be running out of memory after sometime. So there are some methods in android that invokes the garbage collector explicitly.

See the following function which invokes the garbage collector to free a lot of memory.

public void freeMemory(){		
    System.runFinalization();
    Runtime.getRuntime().gc();
    System.gc();
}

A little explanation about the methods.

Java.Lang.Runtime.RunFinalization Method
Provides a hint to the VM that it would be useful to attempt to perform any outstanding object finalization.

Both System.gc() is effectively equivalent to Runtime.gc(). System.gc() internally calls Runtime.gc().
The only diff is System.gc() is a class method where as Runtime.gc() is an instance method. So, System.gc() is more convenient.

One thought on “How to explicitly free memory in android OR Release unwanted memory in Android?

Leave a Reply

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