diff --git a/core/java/android/app/ActivityManager.java b/core/java/android/app/ActivityManager.java index 7746ca9aec238..4e61c3c916eca 100644 --- a/core/java/android/app/ActivityManager.java +++ b/core/java/android/app/ActivityManager.java @@ -1078,13 +1078,20 @@ public class ActivityManager { */ public static class MemoryInfo implements Parcelable { /** - * The total available memory on the system. This number should not + * The available memory on the system. This number should not * be considered absolute: due to the nature of the kernel, a significant * portion of this memory is actually in use and needed for the overall * system to run well. */ public long availMem; - + + /** + * The total memory accessible by the kernel. This is basically the + * RAM size of the device, not including below-kernel fixed allocations + * like DMA buffers, RAM for the baseband CPU, etc. + */ + public long totalMem; + /** * The threshold of {@link #availMem} at which we consider memory to be * low and start killing background services and other non-extraneous @@ -1116,6 +1123,7 @@ public class ActivityManager { public void writeToParcel(Parcel dest, int flags) { dest.writeLong(availMem); + dest.writeLong(totalMem); dest.writeLong(threshold); dest.writeInt(lowMemory ? 1 : 0); dest.writeLong(hiddenAppThreshold); @@ -1126,6 +1134,7 @@ public class ActivityManager { public void readFromParcel(Parcel source) { availMem = source.readLong(); + totalMem = source.readLong(); threshold = source.readLong(); lowMemory = source.readInt() != 0; hiddenAppThreshold = source.readLong(); diff --git a/core/java/android/os/Process.java b/core/java/android/os/Process.java index 1df53e88ca713..18fd3cbad52ac 100644 --- a/core/java/android/os/Process.java +++ b/core/java/android/os/Process.java @@ -896,6 +896,9 @@ public class Process { /** @hide */ public static final native long getFreeMemory(); + /** @hide */ + public static final native long getTotalMemory(); + /** @hide */ public static final native void readProcLines(String path, String[] reqFields, long[] outSizes); diff --git a/core/jni/android_util_Process.cpp b/core/jni/android_util_Process.cpp index d20cc9e651d98..027ed16381002 100644 --- a/core/jni/android_util_Process.cpp +++ b/core/jni/android_util_Process.cpp @@ -402,7 +402,7 @@ static int pid_compare(const void* v1, const void* v2) return *((const jint*)v1) - *((const jint*)v2); } -static jlong android_os_Process_getFreeMemory(JNIEnv* env, jobject clazz) +static jlong getFreeMemoryImpl(const char* const sums[], const int sumsLen[], int num) { int fd = open("/proc/meminfo", O_RDONLY); @@ -424,11 +424,8 @@ static jlong android_os_Process_getFreeMemory(JNIEnv* env, jobject clazz) int numFound = 0; jlong mem = 0; - static const char* const sums[] = { "MemFree:", "Cached:", NULL }; - static const int sumsLen[] = { strlen("MemFree:"), strlen("Cached:"), 0 }; - char* p = buffer; - while (*p && numFound < 2) { + while (*p && numFound < num) { int i = 0; while (sums[i]) { if (strncmp(p, sums[i], sumsLen[i]) == 0) { @@ -453,6 +450,20 @@ static jlong android_os_Process_getFreeMemory(JNIEnv* env, jobject clazz) return numFound > 0 ? mem : -1; } +static jlong android_os_Process_getFreeMemory(JNIEnv* env, jobject clazz) +{ + static const char* const sums[] = { "MemFree:", "Cached:", NULL }; + static const int sumsLen[] = { strlen("MemFree:"), strlen("Cached:"), 0 }; + return getFreeMemoryImpl(sums, sumsLen, 2); +} + +static jlong android_os_Process_getTotalMemory(JNIEnv* env, jobject clazz) +{ + static const char* const sums[] = { "MemTotal:", NULL }; + static const int sumsLen[] = { strlen("MemTotal:"), 0 }; + return getFreeMemoryImpl(sums, sumsLen, 1); +} + void android_os_Process_readProcLines(JNIEnv* env, jobject clazz, jstring fileStr, jobjectArray reqFields, jlongArray outFields) { @@ -901,6 +912,7 @@ static const JNINativeMethod methods[] = { {"sendSignal", "(II)V", (void*)android_os_Process_sendSignal}, {"sendSignalQuiet", "(II)V", (void*)android_os_Process_sendSignalQuiet}, {"getFreeMemory", "()J", (void*)android_os_Process_getFreeMemory}, + {"getTotalMemory", "()J", (void*)android_os_Process_getTotalMemory}, {"readProcLines", "(Ljava/lang/String;[Ljava/lang/String;[J)V", (void*)android_os_Process_readProcLines}, {"getPids", "(Ljava/lang/String;[I)[I", (void*)android_os_Process_getPids}, {"readProcFile", "(Ljava/lang/String;[I[Ljava/lang/String;[J[F)Z", (void*)android_os_Process_readProcFile}, diff --git a/services/java/com/android/server/am/ActivityManagerService.java b/services/java/com/android/server/am/ActivityManagerService.java index aa7de826b09dd..54ef724df651a 100644 --- a/services/java/com/android/server/am/ActivityManagerService.java +++ b/services/java/com/android/server/am/ActivityManagerService.java @@ -5360,6 +5360,7 @@ public final class ActivityManagerService extends ActivityManagerNative final long homeAppMem = mProcessList.getMemLevel(ProcessList.HOME_APP_ADJ); final long hiddenAppMem = mProcessList.getMemLevel(ProcessList.HIDDEN_APP_MIN_ADJ); outInfo.availMem = Process.getFreeMemory(); + outInfo.totalMem = Process.getTotalMemory(); outInfo.threshold = homeAppMem; outInfo.lowMemory = outInfo.availMem < (homeAppMem + ((hiddenAppMem-homeAppMem)/2)); outInfo.hiddenAppThreshold = hiddenAppMem;