OLD | NEW |
---|---|
1 // Copyright (c) 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/browser/memory/memory_monitor_android.h" | 5 #include "content/browser/memory/memory_monitor_android.h" |
6 | 6 |
7 #include "base/android/context_utils.h" | 7 #include "base/android/context_utils.h" |
8 #include "base/android/jni_android.h" | 8 #include "base/android/jni_android.h" |
9 #include "base/memory/ptr_util.h" | 9 #include "base/memory/ptr_util.h" |
10 #include "jni/MemoryMonitorAndroid_jni.h" | 10 #include "jni/MemoryMonitorAndroid_jni.h" |
11 | 11 |
12 namespace content { | 12 namespace content { |
13 | 13 |
14 namespace { | 14 namespace { |
15 const size_t kMBShift = 20; | 15 const size_t kMBShift = 20; |
16 } | 16 } |
17 | 17 |
18 // static | 18 // static |
19 std::unique_ptr<MemoryMonitorAndroid> MemoryMonitorAndroid::Create() { | 19 std::unique_ptr<MemoryMonitorAndroid> MemoryMonitorAndroid::Create() { |
20 return base::WrapUnique(new MemoryMonitorAndroid); | 20 return base::WrapUnique(new MemoryMonitorAndroid); |
21 } | 21 } |
22 | 22 |
23 MemoryMonitorAndroid::MemoryMonitorAndroid() { | 23 // static |
24 InitializeFieldIDs(); | 24 bool MemoryMonitorAndroid::Register(JNIEnv* env) { |
25 return RegisterNativesImpl(env); | |
25 } | 26 } |
26 | 27 |
28 MemoryMonitorAndroid::MemoryMonitorAndroid() {} | |
29 | |
27 MemoryMonitorAndroid::~MemoryMonitorAndroid() {} | 30 MemoryMonitorAndroid::~MemoryMonitorAndroid() {} |
28 | 31 |
29 int MemoryMonitorAndroid::GetFreeMemoryUntilCriticalMB() { | 32 int MemoryMonitorAndroid::GetFreeMemoryUntilCriticalMB() { |
30 DCHECK(CanGetMemoryInfo()); | |
31 MemoryInfo info; | 33 MemoryInfo info; |
32 GetMemoryInfo(&info); | 34 GetMemoryInfo(&info); |
33 return (info.avail_mem - info.threshold) >> kMBShift; | 35 return (info.avail_mem - info.threshold) >> kMBShift; |
bashi
2016/09/27 03:51:36
agrieve@ gave me great advice not to call GetMemor
| |
34 } | 36 } |
35 | 37 |
36 bool MemoryMonitorAndroid::CanGetMemoryInfo() { | 38 void MemoryMonitorAndroid::GetMemoryInfo(MemoryInfo* out) { |
37 return (avail_mem_id_ != 0 && low_memory_id_ != 0 && threshold_id_ != 0 && | 39 DCHECK(out); |
38 total_mem_id_ != 0); | 40 JNIEnv* env = base::android::AttachCurrentThread(); |
41 Java_MemoryMonitorAndroid_getMemoryInfo( | |
42 env, base::android::GetApplicationContext(), | |
43 reinterpret_cast<intptr_t>(out)); | |
39 } | 44 } |
40 | 45 |
41 void MemoryMonitorAndroid::GetMemoryInfo(MemoryInfo* out) { | 46 // Called by JNI to populate ActivityManager.MemoryInfo. |
42 DCHECK(out); | 47 static void GetMemoryInfoCallback( |
43 DCHECK(CanGetMemoryInfo()); | 48 JNIEnv* env, |
44 JNIEnv* env = base::android::AttachCurrentThread(); | 49 const base::android::JavaParamRef<jclass>& clazz, |
45 base::android::ScopedJavaLocalRef<jobject> info = | 50 jlong avail_mem, |
46 Java_MemoryMonitorAndroid_getMemoryInfo( | 51 jboolean low_memory, |
47 env, base::android::GetApplicationContext()); | 52 jlong threshold, |
48 out->avail_mem = env->GetLongField(info.obj(), avail_mem_id_); | 53 jlong total_mem, |
49 out->low_memory = env->GetBooleanField(info.obj(), low_memory_id_); | 54 jlong out_ptr) { |
50 out->threshold = env->GetLongField(info.obj(), threshold_id_); | 55 DCHECK(out_ptr); |
51 out->total_mem = env->GetLongField(info.obj(), total_mem_id_); | 56 MemoryMonitorAndroid::MemoryInfo* info = |
52 } | 57 reinterpret_cast<MemoryMonitorAndroid::MemoryInfo*>(out_ptr); |
53 | 58 info->avail_mem = avail_mem; |
54 void MemoryMonitorAndroid::InitializeFieldIDs() { | 59 info->low_memory = low_memory; |
55 JNIEnv* env = base::android::AttachCurrentThread(); | 60 info->threshold = threshold; |
56 base::android::ScopedJavaLocalRef<jclass> clazz = | 61 info->total_mem = total_mem; |
57 base::android::GetClass(env, "android/app/ActivityManager$MemoryInfo"); | |
58 avail_mem_id_ = env->GetFieldID(clazz.obj(), "availMem", "J"); | |
59 low_memory_id_ = env->GetFieldID(clazz.obj(), "lowMemory", "Z"); | |
60 threshold_id_ = env->GetFieldID(clazz.obj(), "threshold", "J"); | |
61 total_mem_id_ = env->GetFieldID(clazz.obj(), "totalMem", "J"); | |
62 } | 62 } |
63 | 63 |
64 // Implementation of a factory function defined in memory_monitor.h. | 64 // Implementation of a factory function defined in memory_monitor.h. |
65 std::unique_ptr<MemoryMonitor> CreateMemoryMonitor() { | 65 std::unique_ptr<MemoryMonitor> CreateMemoryMonitor() { |
66 return MemoryMonitorAndroid::Create(); | 66 return MemoryMonitorAndroid::Create(); |
67 } | 67 } |
68 | 68 |
69 } // namespace content | 69 } // namespace content |
OLD | NEW |