Android memory management tool

Source: Internet
Author: User

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 );}}

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.