Java Code Examples for android.os.Debug#getMemoryInfo()

The following examples show how to use android.os.Debug#getMemoryInfo() . 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: 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 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: 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 4
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 5
Source File: QosThread.java    From ksyhttpcache_android with Apache License 2.0 6 votes vote down vote up
@Override
public void run() {
    while(mRunning) {
        mCpuStats.parseTopResults();

        Debug.getMemoryInfo(mi);

        if(mHandler != null) {
            mQosObject.cpuUsage = mCpuStats.getProcessCpuUsage();
            mQosObject.pss = mi.getTotalPss();
            mQosObject.vss = mi.getTotalPrivateDirty();
            mHandler.obtainMessage(VideoPlayerActivity.UPDATE_QOSMESS, mQosObject).sendToTarget();
        }
        try {
            sleep(3);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
 
Example 6
Source File: BenchmarkStats.java    From ig-json-parser with MIT License 6 votes vote down vote up
/** Grabs the post-snapshot. */
synchronized void after() {
  mAfterTimestamp = SystemClock.elapsedRealtime();
  Debug.stopAllocCounting();
  Debug.getMemoryInfo(mMemoryInfoAfter);

  mAllocCount = Debug.getGlobalAllocCount();
  mAllocSize = Debug.getGlobalAllocSize();
  mFreeCount = Debug.getGlobalFreedCount();
  mFreeSize = Debug.getGlobalFreedSize();

  if (mState != State.BEFORE_CALLED) {
    throw new IllegalStateException("unexpected state");
  }
  mState = State.AFTER_CALLED;
}
 
Example 7
Source File: PerformanceDataManager.java    From DoraemonKit with Apache License 2.0 5 votes vote down vote up
private float getMemoryData() {
    float mem = 0.0F;
    try {
        Debug.MemoryInfo memInfo = null;
        //28 为Android P
        if (Build.VERSION.SDK_INT > 28) {
            // 统计进程的内存信息 totalPss
            memInfo = new Debug.MemoryInfo();
            Debug.getMemoryInfo(memInfo);
        } else {
            //As of Android Q, for regular apps this method will only return information about the memory info for the processes running as the caller's uid;
            // no other process memory info is available and will be zero. Also of Android Q the sample rate allowed by this API is significantly limited, if called faster the limit you will receive the same data as the previous call.

            Debug.MemoryInfo[] memInfos = mActivityManager.getProcessMemoryInfo(new int[]{Process.myPid()});
            if (memInfos != null && memInfos.length > 0) {
                memInfo = memInfos[0];
            }
        }

        int totalPss = memInfo.getTotalPss();
        if (totalPss >= 0) {
            // Mem in MB
            mem = totalPss / 1024.0F;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return mem;
}
 
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: SystemUtils.java    From tilt-game-android with MIT License 5 votes vote down vote up
public static MemoryInfo getMemoryInfo() {
	/* Lazy allocation. */
	if (SystemUtils.sMemoryInfo == null) {
		SystemUtils.sMemoryInfo = new MemoryInfo();
	}

	Debug.getMemoryInfo(SystemUtils.sMemoryInfo);

	return SystemUtils.sMemoryInfo;
}
 
Example 10
Source File: BenchmarkStats.java    From ig-json-parser with MIT License 5 votes vote down vote up
/** Grabs the pre-snapshot. Forces a {@link System#gc()} before collecting stats. */
synchronized void before() {
  if (mState != State.INIT) {
    throw new IllegalStateException("unexpected state");
  }

  mState = State.BEFORE_CALLED;
  System.gc();
  Debug.getMemoryInfo(mMemoryInfoBefore);
  Debug.startAllocCounting();
  mBeforeTimestamp = SystemClock.elapsedRealtime();
}
 
Example 11
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 12
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 13
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 14
Source File: UiTest.java    From MegviiFacepp-Android-SDK with Apache License 2.0 3 votes vote down vote up
public static int getNativeMemoryInfo() {
    Debug.MemoryInfo memoryInfo = new Debug.MemoryInfo();
    Debug.getMemoryInfo(memoryInfo);


    return memoryInfo.nativePss;
}