Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit bcf0f2fc authored by Byron Lee's avatar Byron Lee
Browse files

Add new API to get free memory

Bug: 430454744
Bug: 417906997
Flag: android.app.get_free_memory
Test: m api-stubs-docs-non-updatable-update-current-api
Test: m
Change-Id: Idf87bd65af281ab69f45bd4c7a5805534d799611
parent f6613ab4
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -4863,6 +4863,7 @@ package android.app {
    field @NonNull public static final android.os.Parcelable.Creator<android.app.ActivityManager.MemoryInfo> CREATOR;
    field public long advertisedMem;
    field public long availMem;
    field @FlaggedApi("android.app.get_free_memory") public long freeMem;
    field public boolean lowMemory;
    field public long threshold;
    field public long totalMem;
+12 −0
Original line number Diff line number Diff line
@@ -3440,6 +3440,15 @@ public class ActivityManager {
         */
        public long availMem;

        /**
         * The free memory on the system.  This is the unused RAM size of the
         * device. Unlike {@link #availMem}, it's a basic snapshot of free RAM,
         * not accounting for reclaimable memory.
         */
        @FlaggedApi(Flags.FLAG_GET_FREE_MEMORY)
        @SuppressLint("MutableBareField")
        public long freeMem;

        /**
         * The total memory accessible by the kernel.  This is basically the
         * RAM size of the device, not including below-kernel fixed allocations
@@ -3483,6 +3492,7 @@ public class ActivityManager {
        public void writeToParcel(Parcel dest, int flags) {
            dest.writeLong(advertisedMem);
            dest.writeLong(availMem);
            dest.writeLong(freeMem);
            dest.writeLong(totalMem);
            dest.writeLong(threshold);
            dest.writeInt(lowMemory ? 1 : 0);
@@ -3495,6 +3505,7 @@ public class ActivityManager {
        public void readFromParcel(Parcel source) {
            advertisedMem = source.readLong();
            availMem = source.readLong();
            freeMem = source.readLong();
            totalMem = source.readLong();
            threshold = source.readLong();
            lowMemory = source.readInt() != 0;
@@ -3508,6 +3519,7 @@ public class ActivityManager {
        public void copyTo(MemoryInfo other) {
            other.advertisedMem = advertisedMem;
            other.availMem = availMem;
            other.freeMem = freeMem;
            other.totalMem = totalMem;
            other.threshold = threshold;
            other.lowMemory = lowMemory;
+8 −0
Original line number Diff line number Diff line
@@ -171,3 +171,11 @@ flag {
        purpose: PURPOSE_BUGFIX
    }
}

flag {
     namespace: "desktop_serviceability"
     name: "get_free_memory"
     is_exported: true
     description: "Add API to get free memory."
     bug: "417906997"
}
+9 −0
Original line number Diff line number Diff line
@@ -1494,6 +1494,15 @@ public class Process {
    }

    /** @hide */
    public static final native long getMemAvailable();

    /** @hide */
    public static final native long getMemFree();

    /**
     * This function returns available memory. Use getMemAvailable instead.
     * @hide
     */
    @UnsupportedAppUsage
    public static final native long getFreeMemory();

+17 −11
Original line number Diff line number Diff line
@@ -678,18 +678,14 @@ 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)
{
    std::array<std::string_view, 1> memFreeTags = {
            ::android::meminfo::SysMemInfo::kMemAvailable,
    };
    std::vector<uint64_t> mem(memFreeTags.size());
static jlong android_os_Process_getMemory(JNIEnv* env, jobject clazz, std::string_view tag) {
    std::array<std::string_view, 1> memTags = {tag};
    std::vector<uint64_t> mem(memTags.size());
    ::android::meminfo::SysMemInfo smi;

    if (!smi.ReadMemInfo(memFreeTags.size(),
                         memFreeTags.data(),
                         mem.data())) {
        jniThrowRuntimeException(env, "SysMemInfo read failed to get Free Memory");
    if (!smi.ReadMemInfo(memTags.size(), memTags.data(), mem.data())) {
        jniThrowRuntimeException(env,
                                 ("SysMemInfo read failed to get " + std::string(tag)).c_str());
        return -1L;
    }

@@ -698,6 +694,14 @@ static jlong android_os_Process_getFreeMemory(JNIEnv* env, jobject clazz)
    return sum * 1024;
}

static jlong android_os_Process_getAvailableMemory(JNIEnv* env, jobject clazz) {
    return android_os_Process_getMemory(env, clazz, ::android::meminfo::SysMemInfo::kMemAvailable);
}

static jlong android_os_Process_getFreeMemory(JNIEnv* env, jobject clazz) {
    return android_os_Process_getMemory(env, clazz, ::android::meminfo::SysMemInfo::kMemFree);
}

static jlong android_os_Process_getTotalMemory(JNIEnv* env, jobject clazz)
{
    struct sysinfo si;
@@ -1424,7 +1428,9 @@ static const JNINativeMethod methods[] = {
        {"sendTgSignalThrows", "(III)V", (void*)android_os_Process_sendTgSignalThrows},
        {"setProcessFrozen", "(IIZ)V", (void*)android_os_Process_setProcessFrozen},
        {"isProcessFrozen", "(II)Z", (void*)android_os_Process_isProcessFrozen},
        {"getFreeMemory", "()J", (void*)android_os_Process_getFreeMemory},
        {"getFreeMemory", "()J", (void*)android_os_Process_getAvailableMemory},
        {"getMemAvailable", "()J", (void*)android_os_Process_getAvailableMemory},
        {"getMemFree", "()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},
Loading