public class Memorymanager {private static final String TAG = "Memorymanager";p rivate static final int maxmemory=50*1024*1 024;//Maximum Memory/** * To determine if the system is running in low memory * @param context * @return */public static Boolean hasacailmemory () {//Get phone internal space size long Memory = Getavailableinternalmemorysize (); LOG.I (TAG, memory+ ""), if (Memory < MaxMemory) {//application will run return false in low memory state;} else {return true;}} /** * Get the amount of free space on your phone * * @return */public static long Getavailableinternalmemorysize () {File path = Environment.getdatadire Ctory ();//Get Android Data directory StatFs stat = new StatFs (Path.getpath ());//A class that simulates the DF command of Linux, obtains the use of SD card and cell phone memory long blockSize = Stat.getblocksize ();//returns int, size, in bytes, a file system long Availableblocks = Stat.getavailableblocks ();//returns INT, Gets the currently available storage space return availableblocks * blockSize;} /** * Get phone internal space size * * @return */public static long Gettotalinternalmemorysize () {File path = environment.getdatadirectory ( ); StatFs stat = new StatFs (Path.getpath ()); Long blockSize = Stat.getblocksize (); Long Totalblocks = Stat.getblockcount ();//Gets the number of file systems available for this zone return totalblocks * blockSize;} /** * Get phone external free space size * * @return */public static long Getavailableexternalmemorysize () {if (externalmemoryavailable ()) {Fil E 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 has sdcard.");}} /** * Get phone external space size * * @return */public static long Gettotalexternalmemorysize () {if (externalmemoryavailable ()) {File path = Environment.getexternalstoragedirectory ();//Get External storage directory i.e. Sdcardstatfs stat = new StatFs (Path.getpath ()); Long blockSize = Stat.getblocksize (); Long totalblocks = Stat.getblockcount (); return totalblocks * blockSize;} else {throw new RuntimeException ("Don ' t has sdcard.");}} /** * External Storage available * * @return */public static Boolean externalmemoryavailable () {return android.os.Environment.getExternalSt Oragestate (). EquaLS (Android.os.Environment.MEDIA_MOUNTED);}}
Android Memory Management Tool class