欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android获取app内存使用

程序员文章站 2022-03-10 20:35:38
int memSize = 0; if( Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP_MR1 ){ memSize = getPackageMemoryUsage( topAppName ); }else{ .....
 int memSize = 0;
                            if( Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP_MR1 ){
                                memSize = getPackageMemoryUsage( topAppName );
                            }else{
                                memSize = getPidMemorySize( getPid() );
                            }
    public int getPackageMemoryUsage(String packagename){
    //dumpsys meminfo com.ztl.helper .然后取 TOTAL那一行
        String sss= FileUtli.execRootCmd("dumpsys meminfo " + packagename);
        Log.e("meminfo","返回" + sss);
        int nIndex = sss.indexOf("TOTAL");
        String subStr = sss.substring(nIndex+9, nIndex + 19); //;( nIndex, 100);
        int eIndex = subStr.indexOf(' ');
        String totalInBytes = subStr.substring(0, eIndex);

        int nTotal = Integer.valueOf(totalInBytes);
        return Integer.valueOf( totalInBytes );
    }
    public int getPidMemorySize(int pid) {
        ActivityManager am = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);
        int[] myMempid = new int[] { pid };
        Debug.MemoryInfo[] memoryInfo = am.getProcessMemoryInfo(myMempid);
        memoryInfo[0].getTotalSharedDirty();
        int memSize = memoryInfo[0].getTotalPss();
        return memSize;
    }

方法如上。 因为7.1并不允许其他程序获取别的程序的内存。所以只能曲线救国。

本文地址:https://blog.csdn.net/glunoy/article/details/111868390

相关标签: android