Android Memory Management Tool class

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.