public class MemoryManager extends Manager {
private ActivityManager mActivityManager;
public MemoryManager(Config config) {
super(config);
//获得ActivityManager服务的对象。
mActivityManager = (ActivityManager) SDKUtil.getAppContext().getSystemService(Context.ACTIVITY_SERVICE);
}
public ActivityManager.MemoryInfo getMemoryInfo() {
//获得MemoryInfo对象。
ActivityManager.MemoryInfo memoryInfo = new ActivityManager.MemoryInfo();
//获得系统可用内存,保存在MemoryInfo对象上。
mActivityManager.getMemoryInfo(memoryInfo);
return memoryInfo;
}
/**
* 获取进程最大物理内存。
* 系统为应用分配的内存,没有额外的扩充。
*
* @return
*/
public int getMemoryClass() {
return mActivityManager.getMemoryClass();
}
/**
* 在Android 3.0之后,manifest支持largeheap选项,设置为true后可以为应用申请分配更多内存。
* 开发者通过设置manifest文件中的<application>标签中largeHeap属性的值为"true",获得应用可使用的最大内存。
*
* @return
*/
public int getLargeMemoryClass() {
return mActivityManager.getLargeMemoryClass();
}
/**
* Returns the size of the native heap.
*
* @return The size of the native heap in bytes.
*/
public long getNativeHeapSize() {
return android.os.Debug.getNativeHeapSize();
}
/**
* Returns the amount of allocated memory in the native heap.
*
* @return The allocated size in bytes.
*/
public long getNativeHeapAllocatedSize() {
return android.os.Debug.getNativeHeapAllocatedSize();
}
/**
* Returns the amount of free memory in the native heap.
*
* @return The freed size in bytes.
*/
public long getNativeHeapFreeSize() {
return android.os.Debug.getNativeHeapFreeSize();
}
/**
* Returns the maximum amount of memory that the Java virtual machine will
* attempt to use. If there is no inherent limit then the value {@link
* java.lang.Long#MAX_VALUE} will be returned.
*
* @return the maximum amount of memory that the virtual machine will
* attempt to use, measured in bytes
*/
public long maxMemory() {
return Runtime.getRuntime().maxMemory();
}
}