Java Code Examples for android.os.Debug#MemoryInfo

The following examples show how to use android.os.Debug#MemoryInfo . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LogDebug.java    From springreplugin with Apache License 2.0 6 votes vote down vote up
/**
 * 打印当前内存占用日志,方便外界诊断。注意,这会显著消耗性能(约50ms左右)
 *
 * @param tag Used to identify the source of a log message.  It usually identifies
 *            the class or activity where the log call occurs.
 * @param msg The message you would like logged.
 */
public static int printMemoryStatus(String tag, String msg) {
    if (RePluginInternal.FOR_DEV) {
        Debug.MemoryInfo mi = new Debug.MemoryInfo();
        Debug.getMemoryInfo(mi);

        String mit = "desc=, memory_v_0_0_1, process=, " + IPC.getCurrentProcessName() +
                ", totalPss=, " + mi.getTotalPss() +
                ", dalvikPss=, " + mi.dalvikPss +
                ", nativeSize=, " + mi.nativePss +
                ", otherPss=, " + mi.otherPss + ", ";

        return Log.i(tag + "-MEMORY", mit + msg);
    }
    return -1;
}
 
Example 2
Source File: PerformanceHelper.java    From Trojan with Apache License 2.0 6 votes vote down vote up
public static void recordMemory() {
    Runtime runtime = Runtime.getRuntime();
    float dalvikMem = (float) ((runtime.totalMemory() - runtime.freeMemory()) * 1.0 / TrojanConstants.FORMAT_MB);
    float nativeMem = (float) (Debug.getNativeHeapAllocatedSize() * 1.0 / TrojanConstants.FORMAT_MB);

    List<String> msgList = new LinkedList<>();
    msgList.add(String.valueOf(decimalFormat.format(dalvikMem + nativeMem)) + TrojanConstants.MB);
    msgList.add(String.valueOf(decimalFormat.format(dalvikMem)) + TrojanConstants.MB);
    msgList.add(String.valueOf(decimalFormat.format(nativeMem)) + TrojanConstants.MB);
    try {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            Debug.MemoryInfo memoryInfo = new Debug.MemoryInfo();
            Debug.getMemoryInfo(memoryInfo);
            float stackSize = Float.parseFloat(memoryInfo.getMemoryStat("summary.stack"));
            msgList.add(String.valueOf(decimalFormat.format(stackSize / TrojanConstants.FORMAT_KB)) + TrojanConstants.MB);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    Trojan.log(LogConstants.MEMORY_TAG, msgList);
}
 
Example 3
Source File: GSAServiceClient.java    From AndroidChromium with Apache License 2.0 6 votes vote down vote up
/**
 * Get the PSS used by the process hosting a service.
 *
 * @param packageName Package name of the service to search for.
 * @return the PSS in kB of the process hosting a service, or INVALID_PSS.
 */
@VisibleForTesting
static int getPssForService(ComponentName componentName) {
    if (componentName == null) return INVALID_PSS;
    Context context = ContextUtils.getApplicationContext();
    ActivityManager activityManager =
            (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningServiceInfo> services =
            activityManager.getRunningServices(1000);
    if (services == null) return INVALID_PSS;
    int pid = -1;
    for (ActivityManager.RunningServiceInfo info : services) {
        if (componentName.equals(info.service)) {
            pid = info.pid;
            break;
        }
    }
    if (pid == -1) return INVALID_PSS;
    Debug.MemoryInfo infos[] = activityManager.getProcessMemoryInfo(new int[] {pid});
    if (infos == null || infos.length == 0) return INVALID_PSS;
    return infos[0].getTotalPss();
}
 
Example 4
Source File: MemoryTask.java    From ArgusAPM with Apache License 2.0 6 votes vote down vote up
/**
 * 获取当前内存信息
 */
private MemoryInfo getMemoryInfo() {
    // 注意:这里是耗时和耗CPU的操作,一定要谨慎调用
    Debug.MemoryInfo info = new Debug.MemoryInfo();
    Debug.getMemoryInfo(info);
    if (DEBUG) {
        LogX.d(TAG, SUB_TAG,
                "当前进程:" + ProcessUtils.getCurrentProcessName()
                        + ",内存getTotalPss:" + info.getTotalPss()
                        + " nativeSize:" + info.nativePss
                        + " dalvikPss:" + info.dalvikPss
                        + " otherPss:" + info.otherPss

        );
    }
    return new MemoryInfo(ProcessUtils.getCurrentProcessName(), info.getTotalPss(), info.dalvikPss, info.nativePss, info.otherPss);
}
 
Example 5
Source File: QosThread.java    From KSYMediaPlayer_Android with Apache License 2.0 5 votes vote down vote up
public QosThread(Context context, Handler handler) {
    mHandler = handler;
    mCpuStats = new Cpu();
    mi = new Debug.MemoryInfo();
    mRunning = true;
    mQosObject = new QosObject();
    if(context != null)
        mPackageName = context.getPackageName();
}
 
Example 6
Source File: MemoryUtil.java    From MemoryMonitor with MIT License 5 votes vote down vote up
/**
 * 获取应用实际占用内存
 *
 * @param context
 * @param pid
 * @return 应用pss信息KB
 */
public static PssInfo getAppPssInfo(Context context, int pid) {
    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    Debug.MemoryInfo memoryInfo = am.getProcessMemoryInfo(new int[]{pid})[0];
    PssInfo pssInfo = new PssInfo();
    pssInfo.totalPss = memoryInfo.getTotalPss();
    pssInfo.dalvikPss = memoryInfo.dalvikPss;
    pssInfo.nativePss = memoryInfo.nativePss;
    pssInfo.otherPss = memoryInfo.otherPss;
    return pssInfo;
}
 
Example 7
Source File: MemInfoDataModule.java    From DebugOverlay-Android with Apache License 2.0 5 votes vote down vote up
@Override
public void run() {
    ActivityManager.MemoryInfo systemMemInfo = new ActivityManager.MemoryInfo();
    am.getMemoryInfo(systemMemInfo);
    Debug.MemoryInfo processMemInfo = am.getProcessMemoryInfo(new int[]{Process.myPid()})[0];
    memInfo = new MemInfo(systemMemInfo, processMemInfo);
    notifyObservers();
    handler.postDelayed(memInfoQueryRunnable, interval);
}
 
Example 8
Source File: Sampler.java    From MegviiFacepp-Android-SDK with Apache License 2.0 5 votes vote down vote up
public static int getMemoryInfo() {
        Debug.MemoryInfo memoryInfo = new Debug.MemoryInfo();
        Debug.getMemoryInfo(memoryInfo);
//        return memoryInfo.getTotalPss()/1000;
        return memoryInfo.nativePss/1000;

    }
 
Example 9
Source File: ActivityThread.java    From AndroidComponentPlugin with Apache License 2.0 5 votes vote down vote up
@Override
public Debug.MemoryInfo dumpMemInfo(FileDescriptor fd, boolean checkin,
        boolean all, String[] args) {
    FileOutputStream fout = new FileOutputStream(fd);
    PrintWriter pw = new PrintWriter(fout);
    try {
        return dumpMemInfo(pw, checkin, all, args);
    } finally {
        pw.flush();
    }
}
 
Example 10
Source File: MemoryQuery.java    From Camera2 with Apache License 2.0 4 votes vote down vote up
/**
 * Measures the current memory consumption and thresholds of the app, from
 * the ActivityManager and Debug.MemoryInfo,
 *
 * @return HashMap of memory metrics keyed by string labels.
 */
public HashMap queryMemory()
{
    // Get ActivityManager.MemoryInfo.
    int memoryClass = mActivityManager.getMemoryClass();
    int largeMemoryClass = mActivityManager.getLargeMemoryClass();
    ActivityManager.MemoryInfo memoryInfo = new ActivityManager.MemoryInfo();
    mActivityManager.getMemoryInfo(memoryInfo);
    long availMem = memoryInfo.availMem / BYTES_IN_MEGABYTE;
    long totalMem = memoryInfo.totalMem / BYTES_IN_MEGABYTE;
    long threshold = memoryInfo.threshold / BYTES_IN_MEGABYTE;
    boolean lowMemory = memoryInfo.lowMemory;

    // Get ActivityManager.RunningAppProcessInfo.
    ActivityManager.RunningAppProcessInfo info = new ActivityManager.RunningAppProcessInfo();
    ActivityManager.getMyMemoryState(info);

    // Retrieve a list of all running processes. Get the app PID.
    int appPID = Process.myPid();

    // Get ActivityManager.getProcessMemoryInfo for the app PID.
    long timestamp = SystemClock.elapsedRealtime();
    long totalPrivateDirty = 0L;
    long totalSharedDirty = 0L;
    long totalPSS = 0L;
    long nativePSS = 0L;
    long dalvikPSS = 0L;
    long otherPSS = 0L;

    if (appPID != 0)
    {
        int pids[] = new int[1];
        pids[0] = appPID;
        Debug.MemoryInfo[] memoryInfoArray = mActivityManager.getProcessMemoryInfo(pids);
        totalPrivateDirty = memoryInfoArray[0].getTotalPrivateDirty() / BYTES_IN_KILOBYTE;
        totalSharedDirty = memoryInfoArray[0].getTotalSharedDirty() / BYTES_IN_KILOBYTE;
        totalPSS = memoryInfoArray[0].getTotalPss() / BYTES_IN_KILOBYTE;
        nativePSS = memoryInfoArray[0].nativePss / BYTES_IN_KILOBYTE;
        dalvikPSS = memoryInfoArray[0].dalvikPss / BYTES_IN_KILOBYTE;
        otherPSS = memoryInfoArray[0].otherPss / BYTES_IN_KILOBYTE;
    }

    HashMap outputData = new HashMap();
    outputData.put(KEY_TIMESTAMP, new Long(timestamp));
    outputData.put(KEY_MEMORY_AVAILABLE, new Long(availMem));
    outputData.put(KEY_TOTAL_MEMORY, new Long(totalMem));
    outputData.put(KEY_TOTAL_PSS, new Long(totalPSS));
    outputData.put(KEY_LAST_TRIM_LEVEL, new Integer(info.lastTrimLevel));
    outputData.put(KEY_TOTAL_PRIVATE_DIRTY, new Long(totalPrivateDirty));
    outputData.put(KEY_TOTAL_SHARED_DIRTY, new Long(totalSharedDirty));
    outputData.put(KEY_MEMORY_CLASS, new Long(memoryClass));
    outputData.put(KEY_LARGE_MEMORY_CLASS, new Long(largeMemoryClass));
    outputData.put(KEY_NATIVE_PSS, new Long(nativePSS));
    outputData.put(KEY_DALVIK_PSS, new Long(dalvikPSS));
    outputData.put(KEY_OTHER_PSS, new Long(otherPSS));
    outputData.put(KEY_THRESHOLD, new Long(threshold));
    outputData.put(KEY_LOW_MEMORY, new Boolean(lowMemory));

    Log.d(TAG, String.format("timestamp=%d, availMem=%d, totalMem=%d, totalPSS=%d, " +
                    "lastTrimLevel=%d, largeMemoryClass=%d, nativePSS=%d, dalvikPSS=%d, otherPSS=%d," +
                    "threshold=%d, lowMemory=%s", timestamp, availMem, totalMem, totalPSS,
            info.lastTrimLevel, largeMemoryClass, nativePSS, dalvikPSS, otherPSS,
            threshold, lowMemory));

    return outputData;
}
 
Example 11
Source File: BenchmarkStats.java    From ig-json-parser with MIT License 4 votes vote down vote up
BenchmarkStats() {
  mMemoryInfoBefore = new Debug.MemoryInfo();
  mMemoryInfoAfter = new Debug.MemoryInfo();
  mState = State.INIT;
}
 
Example 12
Source File: MemInfo.java    From DebugOverlay-Android with Apache License 2.0 4 votes vote down vote up
public MemInfo(ActivityManager.MemoryInfo systemMemInfo,
               Debug.MemoryInfo processMemInfo) {
    this.systemMemInfo = systemMemInfo;
    this.processMemInfo = processMemInfo;
}
 
Example 13
Source File: ProcessManagerEngine.java    From MobileGuard with MIT License 4 votes vote down vote up
/**
 * get running processes info
 * you just can use it before Android 5.0
 * @param context
 * @return
 */
public static List<ProcessInfoBean> getRunningProcessesInfoCompat(Context context) {
    // get manager
    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    PackageManager pm = context.getPackageManager();
    // get processes info
    List<ActivityManager.RunningAppProcessInfo> processes = am.getRunningAppProcesses();
    // create list. Specific it init size
    List<ProcessInfoBean> infos = new ArrayList<>(processes.size());
    // get info
    for (ActivityManager.RunningAppProcessInfo processInfo : processes) {
        // create bean
        ProcessInfoBean bean = new ProcessInfoBean();
        // set value
        bean.setPackageName(processInfo.processName);
        // get package info
        ApplicationInfo applicationInfo = null;
        try {
            applicationInfo = pm.getApplicationInfo(bean.getPackageName(), PackageManager.GET_META_DATA);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
            // if package is empty, continue
            continue;
        }
        // set icon
        bean.setIcon(applicationInfo.loadIcon(pm));
        // app name
        bean.setAppName(applicationInfo.loadLabel(pm).toString());
        // system app
        if ((applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
            bean.setSystemApp(true);
        }// if not, need set false. Actually it was.
        // memory
        Debug.MemoryInfo[] processMemoryInfo = am.getProcessMemoryInfo(new int[]{processInfo.pid});
        if (processMemoryInfo.length >= 1) {
            bean.setMemory(processMemoryInfo[0].getTotalPss() * 1024);
        }
        // add to list
        infos.add(bean);
    }

    return infos;
}
 
Example 14
Source File: MemInfo.java    From DebugOverlay-Android with Apache License 2.0 4 votes vote down vote up
public Debug.MemoryInfo getProcessMemInfo() {
    return null;
}
 
Example 15
Source File: Memory.java    From batteryhub with Apache License 2.0 4 votes vote down vote up
/**
 * Read memory usage using the public Android API methods in ActivityManager,
 * such as MemoryInfo and getProcessMemoryInfo.
 *
 * @param context the Context from the running Activity.
 * @return array of int values with total and used memory, in KB.
 */
@TargetApi(21)
public static int[] readMemory(final Context context) {
    int[] pIds;
    int total;
    int used = 0;
    int counter = 0;
    int memoryUsed = 0;

    ActivityManager manager = (ActivityManager)
            context.getSystemService(Activity.ACTIVITY_SERVICE);
    ActivityManager.MemoryInfo memoryInfo = new ActivityManager.MemoryInfo();

    manager.getMemoryInfo(memoryInfo);
    total = (int) memoryInfo.availMem;

    List<ActivityManager.RunningAppProcessInfo> processInfoList =
            manager.getRunningAppProcesses();
    List<ActivityManager.RunningServiceInfo> serviceInfoList =
            manager.getRunningServices(Integer.MAX_VALUE);

    if (processInfoList == null || serviceInfoList == null) {
        return new int[] {total, used};
    }

    pIds = new int[processInfoList.size() + serviceInfoList.size()];

    for (ActivityManager.RunningAppProcessInfo processInfo : processInfoList) {
        pIds[counter] = processInfo.pid;
        counter++;
    }

    for (ActivityManager.RunningServiceInfo serviceInfo : serviceInfoList) {
        pIds[counter] = serviceInfo.pid;
        counter++;
    }

    // Two approaches one for Debug and other for Production
    // TODO: Do further testing to check Debug.MemoryInfo vs ActivityManager.MemoryInfo...

    // 1) ActivityManager.MemoryInfo
    manager.getMemoryInfo(memoryInfo);

    String message =
            String.format(Locale.US, "%d availMem, %b lowMemory, %d threshold, %d total",
                    memoryInfo.availMem, memoryInfo.lowMemory,
                    memoryInfo.threshold, memoryInfo.totalMem);

    logI(TAG, message);

    // 2) Debug.MemoryInfo
    Debug.MemoryInfo[] memoryInfosArray = manager.getProcessMemoryInfo(pIds);

    for (Debug.MemoryInfo memory : memoryInfosArray) {
        memoryUsed += memory.getTotalPss();
    }

    return new int[] {total, used};
}
 
Example 16
Source File: ActivityThread.java    From AndroidComponentPlugin with Apache License 2.0 4 votes vote down vote up
public void getMemoryInfo(Debug.MemoryInfo outInfo) {
    Debug.getMemoryInfo(outInfo);
}
 
Example 17
Source File: DebugCompatImplGB.java    From android-openslmediaplayer with Apache License 2.0 4 votes vote down vote up
@Override
public long getPss() {
    Debug.MemoryInfo mi = new Debug.MemoryInfo();
    Debug.getMemoryInfo(mi);
    return mi.getTotalPss();
}
 
Example 18
Source File: ActivityManager.java    From android_9.0.0_r45 with Apache License 2.0 3 votes vote down vote up
/**
 * Return information about the memory usage of one or more processes.
 *
 * <p><b>Note: this method is only intended for debugging or building
 * a user-facing process management UI.</b></p>
 *
 * @param pids The pids of the processes whose memory usage is to be
 * retrieved.
 * @return Returns an array of memory information, one for each
 * requested pid.
 */
public Debug.MemoryInfo[] getProcessMemoryInfo(int[] pids) {
    try {
        return getService().getProcessMemoryInfo(pids);
    } catch (RemoteException e) {
        throw e.rethrowFromSystemServer();
    }
}
 
Example 19
Source File: ActivityManager.java    From AndroidComponentPlugin with Apache License 2.0 3 votes vote down vote up
/**
 * Return information about the memory usage of one or more processes.
 *
 * <p><b>Note: this method is only intended for debugging or building
 * a user-facing process management UI.</b></p>
 *
 * @param pids The pids of the processes whose memory usage is to be
 * retrieved.
 * @return Returns an array of memory information, one for each
 * requested pid.
 */
public Debug.MemoryInfo[] getProcessMemoryInfo(int[] pids) {
    try {
        return getService().getProcessMemoryInfo(pids);
    } catch (RemoteException e) {
        throw e.rethrowFromSystemServer();
    }
}
 
Example 20
Source File: MemInfo.java    From DebugOverlay-Android with Apache License 2.0 2 votes vote down vote up
public MemInfo(ActivityManager.MemoryInfo systemMemInfo,
               Debug.MemoryInfo processMemInfo) {

}