android 获取内存分配信息

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:关闭大内存模式

运行结果如图:

相关链接:

android android:largeHeap=“true“ 如何使用_Alex老夫子的博客-CSDN博客

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值