forked from keyou/chromium_demo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo_memory.cc
116 lines (105 loc) · 4.32 KB
/
demo_memory.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/***
* base/memory/sheared_memory.h 在 fa7e8cc73e59e49c7ac12465dee031e7370ce1c0
* 中被删除: [base] Remove base::SharedMemory and base::SharedMemoryHandle
*
* There are no users of the deprecated shared memory left, so its code
* can safely removed.
*
* To use shared memory in Chrome, please refer to
* base::WritableSharedMemoryRegion and base::ReadOnlySharedMemoryRegion
* classes.
*
* 所以在这里只演示使用 base::WritableSharedMemoryRegion 和
* base::ReadOnlySharedMemoryRegion
*/
#include "base/at_exit.h"
#include "base/command_line.h"
#include "base/logging.h"
#include "base/message_loop/message_loop.h"
#include "base/task/thread_pool/thread_pool_instance.h"
// For SharedMemoryTest
#include "base/memory/read_only_shared_memory_region.h"
#include "base/memory/writable_shared_memory_region.h"
#include "base/system/sys_info.h"
// For MemoryPressureTest
#include "base/memory/memory_pressure_monitor.h"
//#include "base/util/memory_pressure/multi_source_memory_pressure_monitor.h"
void SharedMemoryTest() {
base::WritableSharedMemoryRegion memory =
base::WritableSharedMemoryRegion::Create(10);
LOG(INFO) << "memory size: " << memory.GetSize();
{
base::WritableSharedMemoryMapping mapping = memory.Map();
void* buffer = mapping.memory();
memset(buffer, 'a', 9);
LOG(INFO) << "mapping size: " << mapping.size();
}
{
int gra = base::SysInfo::VMAllocationGranularity();
LOG(INFO) << "VMAllocationGranularity: " << gra;
// MapAt 的 offset 必须对齐到 VMAllocationGranularity
base::WritableSharedMemoryMapping mapping = memory.MapAt(0, 5);
DCHECK(mapping.IsValid());
char* buffer = mapping.GetMemoryAs<char>();
LOG(INFO) << "mapping size: " << mapping.size() << ", data=" << buffer;
}
{
// WritableSharedMemoryRegion
// 可以转换为ReadOnly的,但是转换后原来的Region将不可用
base::ReadOnlySharedMemoryRegion memory_read_only =
base::WritableSharedMemoryRegion::ConvertToReadOnly(std::move(memory));
// ReadOnlySharedMemoryRegion 是可以复制的
base::ReadOnlySharedMemoryRegion memory_read_only2 =
memory_read_only.Duplicate();
base::ReadOnlySharedMemoryMapping mapping_read_only =
memory_read_only.Map();
DCHECK(mapping_read_only.IsValid());
base::ReadOnlySharedMemoryMapping mapping_read_only2 =
memory_read_only2.Map();
DCHECK(mapping_read_only2.IsValid());
LOG(INFO) << "mapping size: " << mapping_read_only.size()
<< ", data=" << mapping_read_only.GetMemoryAs<char>();
LOG(INFO) << "mapping2 size: " << mapping_read_only2.size()
<< ", data2=" << mapping_read_only2.GetMemoryAs<char>();
}
}
// 只支持ChromeOS,MAC,WIN,不支持linux
void MemoryPresureTest() {
// https://source.chromium.org/chromium/chromium/src/+/master:content/browser/browser_main_loop.cc;l=367;drc=88c20a4027a89de88d6c559fe2ae49124a01ff8d
// 初始化,要一直保活
// 在新版本才有
//std::unique_ptr<util::MultiSourceMemoryPressureMonitor> monitor;
#if defined(OS_CHROMEOS)
if (chromeos::switches::MemoryPressureHandlingEnabled())
monitor = std::make_unique<util::MultiSourceMemoryPressureMonitor>();
#elif defined(OS_MACOSX) || defined(OS_WIN) || defined(OS_FUCHSIA)
monitor = std::make_unique<util::MultiSourceMemoryPressureMonitor>();
#else
return;
#endif
base::MemoryPressureMonitor* monitor = base::MemoryPressureMonitor::Get();
base::MemoryPressureMonitor::MemoryPressureLevel level =
monitor->GetCurrentPressureLevel();
if (level == base::MemoryPressureMonitor::MemoryPressureLevel::
MEMORY_PRESSURE_LEVEL_CRITICAL) {
LOG(INFO) << "应该释放内存";
// 应该释放内存
}
}
int main(int argc, char** argv) {
// 类似C++的 atexit() 方法,用于管理程序的销毁逻辑,base::Singleton类依赖它
base::AtExitManager at_exit;
// 初始化CommandLine
base::CommandLine::Init(argc, argv);
// 设置日志格式
logging::SetLogItems(true, false, true, false);
// 创建主消息循环
base::MessageLoop message_loop;
// 初始化线程池,会创建新的线程,在新的线程中会创建新消息循环MessageLoop
base::ThreadPoolInstance::CreateAndStartWithDefaultParams("Demo");
SharedMemoryTest();
//MemoryPresureTest();
LOG(INFO) << "running...";
base::RunLoop().Run();
return 0;
}