|
| 1 | +# Memory Inspection |
| 2 | + |
| 3 | +Tracking WebKit's memory usage is important to ensure we do not use excessive resources. |
| 4 | +The operating system (in combination with WebKit tools) provides numerous ways to inspect WebKit to discover where our memory is being allocated. |
| 5 | + |
| 6 | +## Build Settings |
| 7 | + |
| 8 | +### Malloc Heap Breakdown |
| 9 | + |
| 10 | +Malloc Heap Breakdown allows for fine-grained analysis of memory allocated per class. Classes marked with `WTF_MAKE_FAST_ALLOCATED_WITH_HEAP_IDENTIFIER(ClassName);` will be individually marked when using tools like `footprint`. |
| 11 | + |
| 12 | +To enable this build setting you need to flip two flags. One in `PlatformEnable.h` and the second in `BPlatform.h`. |
| 13 | + |
| 14 | +```cpp |
| 15 | +/* Source/WTF/wtf/PlatformEnable.h */ |
| 16 | + |
| 17 | +/* |
| 18 | + * Enable this to put each IsoHeap and other allocation categories into their own malloc heaps, so that tools like vmmap can show how big each heap is. |
| 19 | + * Turn BENABLE_MALLOC_HEAP_BREAKDOWN on in bmalloc together when using this. |
| 20 | + */ |
| 21 | +#if !defined(ENABLE_MALLOC_HEAP_BREAKDOWN) |
| 22 | +#define ENABLE_MALLOC_HEAP_BREAKDOWN 0 |
| 23 | +#endif |
| 24 | +``` |
| 25 | + |
| 26 | +```cpp |
| 27 | +/* Source/bmalloc/bmalloc/BPlatform.h */ |
| 28 | + |
| 29 | +/* Enable this to put each IsoHeap and other allocation categories into their own malloc heaps, so that tools like vmmap can show how big each heap is. */ |
| 30 | +#define BENABLE_MALLOC_HEAP_BREAKDOWN 0 |
| 31 | +``` |
| 32 | +
|
| 33 | +## Commands |
| 34 | +
|
| 35 | +### Footprint |
| 36 | +
|
| 37 | +Footprint is a macOS specific tool that allows the developer to check memory usage across regions. |
| 38 | +
|
| 39 | +```shell |
| 40 | +> footprint WebKit |
| 41 | +Found process com.apple.WebKit.WebContent [27416] from partial name WebKit |
| 42 | +====================================================================== |
| 43 | +com.apple.WebKit.WebContent [27416]: 64-bit Footprint: 142 MB (16384 bytes per page) |
| 44 | +====================================================================== |
| 45 | +
|
| 46 | + Dirty Clean Reclaimable Regions Category |
| 47 | + --- --- --- --- --- |
| 48 | + 108 MB 0 B 23 MB 11 WebKit malloc |
| 49 | +9664 KB 0 B 0 B 24 MALLOC_TINY |
| 50 | +6384 KB 0 B 16 KB 8 MALLOC_SMALL |
| 51 | +3904 KB 0 B 0 B 768 JS VM Gigacage |
| 52 | +... |
| 53 | + --- --- --- --- --- |
| 54 | + 142 MB 21 MB 23 MB 7001 TOTAL |
| 55 | +
|
| 56 | +Auxiliary data: |
| 57 | + dirty: N |
| 58 | + phys_footprint_peak: 424 MB |
| 59 | + phys_footprint: 142 MB |
| 60 | +``` |
| 61 | + |
| 62 | +#### Results |
| 63 | + |
| 64 | +Refer to `man footprint` for a full guide on this tool. |
| 65 | + |
| 66 | +##### Dirty |
| 67 | + |
| 68 | +Memory that is written to by the process. Includes Swapped, non-volatile, and implicitly written memory. |
| 69 | + |
| 70 | +##### Clean |
| 71 | + |
| 72 | +Memory which is neither dirty nor reclaimable. |
| 73 | + |
| 74 | +##### Reclaimable |
| 75 | + |
| 76 | +Memory marked as available for reuse. |
| 77 | + |
| 78 | +##### Regions |
| 79 | + |
| 80 | +Number of VM Regions that contribute to this row. |
| 81 | + |
| 82 | +##### Category |
| 83 | + |
| 84 | +Descriptive name for this entry. |
0 commit comments