ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
ActivityManager.MemoryInfo mi = new ActivityManager.MemoryInfo();
//设备内存
manager.getMemoryInfo(mi);
String totalMem = Formatter.formatFileSize(context, mi.totalMem);
Log.d("Alex", "设备的运行总内存 totalMem: " + totalMem);
String availMem = Formatter.formatFileSize(context, mi.availMem);
Log.d("Alex", "设备剩余运行内存 availMem: " + availMem);
int memory = manager.getMemoryClass();
Log.d("Alex", "非大内存模式,app可分配最大内存 memory: " + memory);
int largeMemory = manager.getLargeMemoryClass();
Log.d("Alex", "大内存模式,app可分配最大内存 largeMemory: " + largeMemory);
//运行时最大分配内存
float maxMemory = (float) (Runtime.getRuntime().maxMemory() * 1.0 / (1024 * 1024));
Log.d("Alex", "运行时app当前可分配最大内存 maxMemory: " + maxMemory);
//当前分配的总内存
float totalMemory = (float) (Runtime.getRuntime().totalMemory() * 1.0 / (1024 * 1024));
Log.d("Alex", "运行时app当前分配内存 totalMemory: " + totalMemory);
//当前分配使用剩余内存
float freeMemory = (float) (Runtime.getRuntime().freeMemory() * 1.0 / (1024 * 1024));
Log.d("Alex", "运行时app当前分配剩余内存 freeMemory: " + freeMemory);
<application
android:name=".MyApp"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.MyApplication"
android:largeHeap="true">
android:largeHeap true:开启大内存模式; false:关闭大内存模式
运行结果如图:
相关链接: