Agarwal's answer was very helpful. I had to change it a little, since I do not calculate the free memory in activity, but transfer the system utilities file in the application context:
From the main activity:
public class MyActivity extends Activity { ... public void onCreate(Bundle savedInstanceState) { ... MySystemUtils systemUtils = new MySystemUtils(this);
In the SystemUtils file:
MySystemUtils (Context appContext) { // called once from within onCreate MemoryInfo mi = new MemoryInfo(); ActivityManager activityManager = ActivityManager)appContext.getSystemService(Activity.ACTIVITY_SERVICE); activityManager.getMemoryInfo(mi); long availableMegs = mi.availMem / 1048576L; }
source share