android記憶體管理工具類

來源:互聯網
上載者:User

標籤:android   android應用   記憶體管理   

public class MemoryManager {private static final String TAG = "MemoryManager";private static final int MAXMEMORY=50*1024*1024;//程式啟動並執行最大記憶體/** * 判斷系統是否在低記憶體下運行 * @param context * @return */public static boolean hasAcailMemory() {// 擷取手機內部空間大小long memory = getAvailableInternalMemorySize();Log.i(TAG, memory+"");if (memory < MAXMEMORY) {//應用將處於低記憶體狀態下運行return false;} else {return true;}}/** * 擷取手機內部可用空間大小 *  * @return */public static long getAvailableInternalMemorySize() {File path = Environment.getDataDirectory();// 擷取 Android 資料目錄StatFs stat = new StatFs(path.getPath());// 一個類比linux的df命令的一個類,獲得SD卡和手機記憶體的使用方式long blockSize = stat.getBlockSize();// 返回 Int ,大小,以位元組為單位,一個檔案系統long availableBlocks = stat.getAvailableBlocks();// 返回 Int ,擷取當前可用的儲存空間return availableBlocks * blockSize;}/** * 擷取手機內部空間大小 *  * @return */public static long getTotalInternalMemorySize() {File path = Environment.getDataDirectory();StatFs stat = new StatFs(path.getPath());long blockSize = stat.getBlockSize();long totalBlocks = stat.getBlockCount();// 擷取該地區可用的檔案系統數return totalBlocks * blockSize;}/** * 擷取手機外部可用空間大小 *  * @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.");}}/** * 擷取手機外部空間大小 *  * @return */public static long getTotalExternalMemorySize() {if (externalMemoryAvailable()) {File path = Environment.getExternalStorageDirectory();// 擷取外部儲存目錄即 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.");}}/** * 外部儲存是否可用 *  * @return */public static boolean externalMemoryAvailable() {return android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);}}

android記憶體管理工具類

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.