Android memory management tool
Public class MemoryManager {private static final String TAG = "MemoryManager"; private static final int MAXMEMORY = 50*1024*1024; // maximum memory for running the program/*** determines whether the system is running in low memory * @ param context * @ return */public static boolean hasAcailMemory () {// get the internal space size of the mobile phone long memory = getAvailableInternalMemorySize (); Log. I (TAG, memory + ""); if (memory <MAXMEMORY) {// The application will run return false in the low memory status;} else {return true ;}} /*** get the internal available space size of the mobile phone ** @ return */public static long getAvailableInternalMemorySize () {File path = Environment. getDataDirectory (); // get the Android data directory StatFs stat = new StatFs (path. getPath (); // a class that simulates the df command of linux to obtain the usage of SD card and mobile phone memory. long blockSize = stat. getBlockSize (); // return Int, size, in bytes, a file system long availableBlocks = stat. getAvailableBlocks (); // return Int to get the currently available bucket return availableBlocks * blockSize ;} /*** get the internal space size of the mobile phone ** @ return */public static long getTotalInternalMemorySize () {File path = Environment. getDataDirectory (); StatFs stat = new StatFs (path. getPath (); long blockSize = stat. getBlockSize (); long totalBlocks = stat. getBlockCount (); // obtain the number of available file systems in the region. return totalBlocks * blockSize ;} /*** get the external available space size of the mobile phone ** @ return */public static long getAvailableExternalMemorySize () {if (externalMemoryAvailable () {File path = Environment. getExternalStorageDirectory (); StatFs stat = new StatFs (path. getPath (); long blockSize = stat. getBlockSize (); long availableBlocks = stat. getAvailableBlocks (); return availableBlocks * blockSize;} else {throw new RuntimeException ("Don't have sdcard. ") ;}}/**** get the external space size of the mobile phone ** @ return */public static long getTotalExternalMemorySize () {if (externalMemoryAvailable () {File path = Environment. getExternalStorageDirectory (); // obtain the external storage directory SDCardStatFs stat = new StatFs (path. getPath (); long blockSize = stat. getBlockSize (); long totalBlocks = stat. getBlockCount (); return totalBlocks * blockSize;} else {throw new RuntimeException ("Don't have sdcard. ") ;}}/*** whether external storage is available ** @ return */public static boolean externalMemoryAvailable () {return android. OS. environment. getExternalStorageState (). equals (android. OS. environment. MEDIA_MOUNTED );}}