41

Is it possible to get the amount of free memory on an Android device (not on the SD CARD) via the Android SDK?

If so, how?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
clamp
  • 33,000
  • 75
  • 203
  • 299
  • 2
    Possible duplicate of [Android get free size of internal/external memory](https://stackoverflow.com/questions/8133417/android-get-free-size-of-internal-external-memory) – Jason C May 23 '17 at 14:32

7 Answers7

64

this post might fit well to your question.

also check this thread. there is so much info here on SO.

googled a bit and here is the solution (found at android git)

File path = Environment.getDataDirectory();
StatFs stat = new StatFs(path.getPath());
long blockSize = stat.getBlockSize();
long availableBlocks = stat.getAvailableBlocks();
return Formatter.formatFileSize(this, availableBlocks * blockSize);
Community
  • 1
  • 1
mad
  • 3,493
  • 4
  • 23
  • 31
25
/*************************************************************************************************
Returns size in bytes.

If you need calculate external memory, change this: 
    StatFs statFs = new StatFs(Environment.getRootDirectory().getAbsolutePath());
to this: 
    StatFs statFs = new StatFs(Environment.getExternalStorageDirectory().getAbsolutePath());        
**************************************************************************************************/
    public long TotalMemory()
    {
        StatFs statFs = new StatFs(Environment.getRootDirectory().getAbsolutePath());   
        long   Total  = ( (long) statFs.getBlockCount() * (long) statFs.getBlockSize());
        return Total;
    }

    public long FreeMemory()
    {
        StatFs statFs = new StatFs(Environment.getRootDirectory().getAbsolutePath());
        long   Free   = (statFs.getAvailableBlocks() * (long) statFs.getBlockSize());
        return Free;
    }

    public long BusyMemory()
    {
        StatFs statFs = new StatFs(Environment.getRootDirectory().getAbsolutePath());   
        long   Total  = ((long) statFs.getBlockCount() * (long) statFs.getBlockSize());
        long   Free   = (statFs.getAvailableBlocks()   * (long) statFs.getBlockSize());
        long   Busy   = Total - Free;
        return Busy;
    }

Converting bytes to human readable format (like 1 Mb, 1 Gb)

    public static String floatForm (double d)
    {
       return new DecimalFormat("#.##").format(d);
    }


    public static String bytesToHuman (long size)
    {
        long Kb = 1  * 1024;
        long Mb = Kb * 1024;
        long Gb = Mb * 1024;
        long Tb = Gb * 1024;
        long Pb = Tb * 1024;
        long Eb = Pb * 1024;

        if (size <  Kb)                 return floatForm(        size     ) + " byte";
        if (size >= Kb && size < Mb)    return floatForm((double)size / Kb) + " Kb";
        if (size >= Mb && size < Gb)    return floatForm((double)size / Mb) + " Mb";
        if (size >= Gb && size < Tb)    return floatForm((double)size / Gb) + " Gb";
        if (size >= Tb && size < Pb)    return floatForm((double)size / Tb) + " Tb";
        if (size >= Pb && size < Eb)    return floatForm((double)size / Pb) + " Pb";
        if (size >= Eb)                 return floatForm((double)size / Eb) + " Eb";

        return "???";
    }
XXX
  • 8,996
  • 7
  • 44
  • 53
  • 4
    You need to cast the blockCount and blockSize into a long before doing the math. Otherwise these methods could return a negative value, since the total block count times the block size will exceed the maximum value for an int. Yes, I used the code and got the negatives. – christophercotton Jul 18 '12 at 15:40
  • 7
    You will actually need to do (long) statFs.getBlockCount() * (long) statFs.getBlockSize() to make sure they get cast correctly. – christophercotton Jul 18 '12 at 20:58
  • @christophercotton: Spot on, mate! Most novice Java programmers will often make the mistake of only casting `long` to one side of the equation and can't figure out why they got a negative result. Rule of thumb: In Java equation scenario, always cast to the same type to **both** side of the equation. You can't go wrong. – ChuongPham Jun 30 '13 at 13:57
  • Since api18+ you can remove the cast & use getAvailableBlocksLong() & getBlockCountLong() instead. – Jordy Jan 16 '14 at 11:45
10

It looks like the StatFs class might be what you need to use. I'm not sure what path would be considered the root of the device, but I believe the result would be the same regardless of directory, as long as it's part of the internal storage. Something like this may work:

StatFs stats = new StatFs("/data");
int availableBlocks = stats.getAvailableBlocks();
int blockSizeInBytes = stats.getBlockSize();
int freeSpaceInBytes = availableBlocks * blockSizeInBytes;

If nothing else, the StatFs class should give you a good start on where to look.

Kevin Coppock
  • 133,643
  • 45
  • 263
  • 274
7

There are some methods which were deprecated by Google since 2013 and you might change these methods (API 18+ only):

  • getAvailableBlocks() to getAvailableBlocksLong()
  • getBlockCount() to getBlockCountLong()
  • getBlockSize() to getBlockSizeLong()
  • getFreeBlocks() to getFreeBlocksLong()
Anggrayudi H
  • 14,977
  • 11
  • 54
  • 87
2

On devices where internal memory size is very big it doesn't work because int value is too small. For example on Motorola xum it doesn't work. You have to use something like this:

int freeSpaceInKilobytes = availableBlocks * (blockSizeInBytes / 1024);

Ilyas
  • 21
  • 1
2
/**
 * @return Number of bytes available on internal storage
 */
public static long getInternalAvailableSpace() {
    long availableSpace = -1L;
    try {StatFs stat = new StatFs(Environment.getDataDirectory()
            .getPath());
        stat.restat(Environment.getDataDirectory().getPath());
        availableSpace = (long) stat.getAvailableBlocks() * (long) stat.getBlockSize();
    } catch (Exception e) {
        e.printStackTrace();
    }

    return availableSpace;
}
Muhammad Nabeel Arif
  • 19,140
  • 8
  • 51
  • 70
0

Please refer this link: Android get free size of internal/external memory This might help. Here, everything is explained in a nice way.

Community
  • 1
  • 1