Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(69)

Unified Diff: content/browser/memory/memory_monitor_android.cc

Issue 2340293003: Add MemoryMonitorAndroid (Closed)
Patch Set: typo Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: content/browser/memory/memory_monitor_android.cc
diff --git a/content/browser/memory/memory_monitor_android.cc b/content/browser/memory/memory_monitor_android.cc
new file mode 100644
index 0000000000000000000000000000000000000000..0628f5a599921faa51c07140968a70869c5e5928
--- /dev/null
+++ b/content/browser/memory/memory_monitor_android.cc
@@ -0,0 +1,72 @@
+// Copyright (c) 2016 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "content/browser/memory/memory_monitor_android.h"
+
+#include "base/android/context_utils.h"
+#include "base/android/jni_android.h"
+#include "base/memory/ptr_util.h"
+#include "jni/MemoryMonitorAndroid_jni.h"
+
+namespace content {
+
+namespace {
+const size_t kMBShift = 20;
+}
+
+// static
+std::unique_ptr<MemoryMonitorAndroid> MemoryMonitorAndroid::Create() {
+ return base::WrapUnique(new MemoryMonitorAndroid);
+}
+
+MemoryMonitorAndroid::MemoryMonitorAndroid() {
+ InitializeFieldIDs();
+}
+
+MemoryMonitorAndroid::~MemoryMonitorAndroid() {}
+
+int MemoryMonitorAndroid::GetFreeMemoryUntilCriticalMB() {
+ if (!CanGetMemoryInfo()) {
+ LOG(ERROR) << "Could not get memory info";
+ return 0;
chrisha 2016/09/26 17:56:48 When can this happen? If this is at all common, th
bashi 2016/09/26 23:30:18 IIUC, it shouldn't happen. Changed to DCHECK().
+ }
+ MemoryInfo info;
+ GetMemoryInfo(&info);
+ return (info.avail_mem - info.threshold) >> kMBShift;
+}
+
+bool MemoryMonitorAndroid::CanGetMemoryInfo() {
+ return (avail_mem_id_ != 0 && low_memory_id_ != 0 && threshold_id_ != 0 &&
+ total_mem_id_ != 0);
+}
+
+void MemoryMonitorAndroid::GetMemoryInfo(MemoryInfo* out) {
+ DCHECK(out);
+ DCHECK(CanGetMemoryInfo());
+ JNIEnv* env = base::android::AttachCurrentThread();
+ base::android::ScopedJavaLocalRef<jobject> info =
+ Java_MemoryMonitorAndroid_getMemoryInfo(
+ env, base::android::GetApplicationContext());
+ out->avail_mem = env->GetLongField(info.obj(), avail_mem_id_);
+ out->low_memory = env->GetBooleanField(info.obj(), low_memory_id_);
+ out->threshold = env->GetLongField(info.obj(), threshold_id_);
+ out->total_mem = env->GetLongField(info.obj(), total_mem_id_);
+}
+
+void MemoryMonitorAndroid::InitializeFieldIDs() {
+ JNIEnv* env = base::android::AttachCurrentThread();
+ base::android::ScopedJavaLocalRef<jclass> clazz =
+ base::android::GetClass(env, "android/app/ActivityManager$MemoryInfo");
+ avail_mem_id_ = env->GetFieldID(clazz.obj(), "availMem", "J");
+ low_memory_id_ = env->GetFieldID(clazz.obj(), "lowMemory", "Z");
+ threshold_id_ = env->GetFieldID(clazz.obj(), "threshold", "J");
+ total_mem_id_ = env->GetFieldID(clazz.obj(), "totalMem", "J");
+}
+
+// Implementation of a factory function defined in memory_monitor.h.
+std::unique_ptr<MemoryMonitor> CreateMemoryMonitor() {
+ return MemoryMonitorAndroid::Create();
+}
+
+} // namespace content

Powered by Google App Engine
This is Rietveld 408576698