Memory information : Memory « Hardware « Android






Memory information

  
//package org.beryl.util;

import java.util.Locale;

public class Memory {

  public static final long KB_IN_BYTES = 1024;
  public static final long MB_IN_BYTES = 1024 * KB_IN_BYTES;
  public static final long GB_IN_BYTES = 1024 * MB_IN_BYTES;
  public static final long TB_IN_BYTES = 1024 * GB_IN_BYTES;

  public static final long FOUR_MB_IN_BYTES = 4 * MB_IN_BYTES;

  final static long [] sizeArray = { TB_IN_BYTES, GB_IN_BYTES, MB_IN_BYTES, KB_IN_BYTES, 1 };
  final static String [] sizeUnitsArray = { "TB", "GB", "MB", "KB", "bytes" };

  public static long getReasonableMemoryCushion() {
    return FOUR_MB_IN_BYTES;
  }

  public static long bytesToKilobytes(long bytes) {
    return bytes / KB_IN_BYTES;
  }

  public static long bytesToMegabytes(long bytes) {
    return bytes / MB_IN_BYTES;
  }

  public static long bytesToGigabytes(long bytes) {
    return bytes / GB_IN_BYTES;
  }

  public static long bytesToTerabytes(long bytes) {
    return bytes / TB_IN_BYTES;
  }

  public static long freeMemory() {
    final Runtime rt = Runtime.getRuntime();
    return rt.freeMemory();
  }

  public static long memoryLimit() {
    final Runtime rt = Runtime.getRuntime();
    return rt.maxMemory();
  }

  public static double consumptionRatio() {
    return freeMemory() / (double)memoryLimit();
  }

  public static String toString(long bytes) {
    String byteSizeString = "<unknown> bytes";
    final int length = sizeArray.length;
    double size;
    double dBytes = (double) bytes;

    for(int i = 0; i < length; i++) {
      size = dBytes / sizeArray[i];

      if(size >= 1.0) {
        byteSizeString = String.format(Locale.ENGLISH, "%.4f %s", size, sizeUnitsArray[i]);
        break;
      }
    }

    return byteSizeString;
  }
}

   
    
  








Related examples in the same category

1.Get memory information
2.Get Memory Size Strings
3.Get Used Memory Size
4.Get Free Memory Size
5.Get Total Memory Size
6.Calculates the free memory of the device. This is based on an inspection of the filesystem, which in android devices is stored in RAM.
7.Calculates the total memory of the device. This is based on an inspection of the filesystem, which in android devices is stored in RAM.
8.Get Memory Total
9.Get Memory Free
10.get MemoryInfo