Obtain free memory size,unit is MB - Android Hardware

Android examples for Hardware:Memory

Description

Obtain free memory size,unit is MB

Demo Code


//package com.java2s;

import android.app.ActivityManager;
import android.app.ActivityManager.MemoryInfo;
import android.content.Context;

public class Main {
    /**/*from ww w  .java 2s  . c o  m*/
     * Obtain free memory size,unit is MB
     * @param context
     * @return
     */
    public static final long obtainFreeMemory(Context context) {
        MemoryInfo mi = new MemoryInfo();
        ActivityManager activityManager = (ActivityManager) context
                .getSystemService(Context.ACTIVITY_SERVICE);
        activityManager.getMemoryInfo(mi);
        long availableMegs = mi.availMem / 1024 / 1024;
        ;
        return availableMegs;
    }
}

Related Tutorials