Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: In which method is better to call the Garbage Collector?

Tags:

java

android

I am experiencing some out of memory issues on my app and want to call the garbage collector, but I am not sure in which method I should call it.

Here is my code:

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

Currently I am calling this method in onStop() but is it better to call it inside onDestroy()?

like image 860
user5035668 Avatar asked Nov 09 '22 08:11

user5035668


1 Answers

It is never a good practice to call the GC manually. Dalvik or ART simply knows better than us.

If your app requires a lot of memory to handle expensive operations, this is a good solution

<application
....
   android:largeHeap="true"> 
like image 56
Aritra Roy Avatar answered Nov 14 '22 22:11

Aritra Roy