blob: e8c4942d6e7c0a68e42fba8fef6ebbb1496a6fd3 [file] [log] [blame] [view]
Albert J. Wong2108fde2017-06-08 17:55:501# Key Concepts in Chrome Memory
2
Albert J. Wongd6381a82017-06-14 22:49:123## What's so hard about memory? Isn't it just malloc and free?
4
5Not really. There are lots of differences and subtleties that change per
6operating system and even per operating system configuration.
7
8Fortunately, these differences mostly disappear when a program is running
9with sufficient resources.
10
11Unfortunately, the distinctions end up being very relevant when
12working near out of memory conditions or analyzing overall performance
13when there is any amount of memory pressure; this makes crafting and
14interpreting memory statistics hard.
15
16Fortunately, the point of this doc is to give succinct background that
17will help you ramp up on the subtleties to work in this space. Yes, this
18is complicated stuff...but don't despair. You work on a multi-process
19browser implementing the web platform with high security guarantees.
20Compared to the rest the system, memory is not THAT complicated.
21
22## An you give specific examples of how it's harder than malloc/free?
23
24Here are some example questions that require a more complex
25view of memory than malloc/free.
26
27 * When Chrome allocates memory, when does it take up swap space?
28 * When memory is `free()`d, when is it made usable by other applications?
29 * Is it always safe to touch the memory returned by malloc()?
30 * How many heaps does Chrome have?
31 * How are memory resources used by the GPU and drivers accounted for?
32 * Is that the same on systems where GPU memory isn't shared with main memory?
33 * How are shared libraries accounted for? How big of a penalty is there for
34 each process that shares the memory?
35 * What types of memory does Task Manager/Activity Monitor/top report?
36 * What about the UMA stats?
37
38In many of the above, the answer actually changes per operating system variant.
39There is at least one major schism between Windows-based machines and more
40unixy systems. For example, it is impossible to return all resources (physical
41ram as well as swap space) to the OS in a way brings them back on demand which
42drastically changes the way one can handle free lists.
43
44However, even in macOS, Android, CrOS, and "standard desktop linux" each
45also have enough divergences (compressed memory, pagefile vs swap partition
46vs no swap, overcommit settings, memory perssure signals etc) that even
47answering "how much memory is Chromium using" is hard to do in a uniform
48manner.
49
50The goal of this document is to give a common set of vocabulary
51and concepts such that Chromium developers can more discuss questions like
52the ones above without misunderstanding each other.
53
54
55## Key gotchas
56
57### Windows allocation uses resources immediately; other OSes use it on first touch.
58
59Arguably the biggest difference for Windows and other OSes is memory granted to
60a process is always "committed" on allocation. Pragmatically this means that in
61Windows, `malloc(10*1024*1024*1024)` will immediately prevent other applications
62from being able to successfully allocate memory thereby causing them to crash
63or not be able to open. In Unix variants, usage usually only consumes system
64resources [TODO(awong): Link to overcommit] when pages are touched.
65
66Not being aware of this difference can cause architecture choices that have a
67larger than expected resource impact on Windows and incorrect interpretation for metrics on Windows
68
69See the following section on "discardable" memory for more info.
70
71
72### Because of the commit guarantee difference, "discarding" memory has completely different meanings across platforms.
73
74In Unix systems, there is an `madvise()` function via which pages that have
75been committed via usage can be returned to the non-resource consuming state.
76Such a page will then be recommitted on demand making it a tempting optimization
77for data structures with freelists. However, there is no such API on Windows.
78The `VirtualAlloc(MEM_RESET)`, `DiscardVirtualMemory()`, and
79`OfferVirtualMemory()` look temptingly similar and on first glance they even
80look like they work because they will immediately reduce the amount of physical
81ram (aka Working Set) a processes uses. However, they do NOT release swap
82meaning they will not help prevent OOM scenarios.
83
84Designing a freelist structure that conflates this behavior (see this
85[PartitionAlloc bug](https://bugs.chromium.org/p/chromium/issues/detail?id=726077))
86will result in a system that only truly reduces resource usage on Unix-like
87systems.
88
89
90## Terms and definitions
91
92TODO(awong): To through Erik's Consistent Memory Metrics doc and pull out bits
93that reconcile with this.
94
95### Commited Memory
96### Discardable memory
97### Proportional Set Size
98### Image memory
99### Shared Memory.
100
Albert J. Wong2108fde2017-06-08 17:55:50101TODO(awong): Write overview of our platform diversity, windows vs \*nix memory models (eg,
102"committed" memory), what "discardable" memory is, GPU memory, zram, overcommit,
103the various Chrome heaps (pageheap, partitionalloc, oilpan, v8, malloc...per
104platform), etc.