1Triggering the GC to reclaim non-Java memory
2--------------------------------------------
3
4Android applications and libraries commonly allocate "native" (i.e. C++) objects that are
5effectively owned by a Java object, and reclaimed when the garbage collector determines that the
6owning Java object is no longer reachable. Various mechanisms are used to accomplish this. These
7include traditional Java finalizers, more modern Java `Cleaner`s, Android's `SystemCleaner` and,
8for platform code, `NativeAllocationRegistry`. Internally, these all rely on the garbage
9collector's processing of `java.lang.ref.Reference`s.
10
11Historically, we have encountered issues when large volumes of such "native" objects are owned by
12Java objects of significantly smaller size. The Java garbage collector normally decides when to
13collect based on occupancy of the Java heap. It would not collect if there are few bytes of newly
14allocated Java objects in the Java heap, even if they "own" large amounts of C++ memory, which
15cannot be reclaimed until the Java objects are reclaimed.
16
17This led to the development of the `VMRuntime.registerNativeAllocation` API, and eventually the
18`NativeAllocationRegistry` API. Both of these allow the programmer to inform the ART GC that a
19certain amount of C++ memory had been allocated, and could only be reclaimed as the result of a
20Java GC.
21
22This had the advantage that the GC would theoretically know exactly how much native memory was
23owned by Java objects. However, its major problem was that registering the exact size of such
24native objects frequently turned out to be impractical. Often a Java object does not just own a
25single native object, but instead owns a complex native data structure composed of many objects in
26the C++ heap. Their sizes commonly depended on prior computations by C++ code. Some of these might
27be shared between Java objects, and could only be reclaimed when all of those Java objects became
28unreachable. Often at least some of the native objects were allocated by third-party libraries
29that did not make the sizes of its internal objects available to clients.
30
31In extreme cases, underestimation of native object sizes could cause native memory use to be so
32excessive that the device would become unstable. At one time, a particularly nasty arithmetic
33expression would cause the Google Calculator app to allocate sufficiently large native arrays of
34digits backing Java `BigInteger`s to force a restart of system processes. (This has since been
35addressed in other ways as well.)
36
37Thus we switched to a scheme in which sizes of native objects are commonly no longer directly
38provided by clients. The GC instead occasionally calls `mallinfo()` to determine how much native
39memory has been allocated, and assumes that any of this may need the collector's help to reclaim.
40C++ memory allocated by means other than `malloc`/`new` and owned by Java objects should still be
41explicitly registered. This loses information about Java ownership, but results in much more
42accurate information about the total size of native objects, something that was previously very
43difficult to approximate, even to within an order of magnitude.
44
45The triggering heuristic
46------------------------
47
48We normally check for the need to trigger a GC due to native memory pressure only as a result of
49calls to the `NativeAllocationRegistry` or `VMRuntime.registerNativeAllocation` APIs. Thus an
50application not using these APIs, e.g. because it is running almost entirely native code, may
51never do so. This is generally a feature, rather than a bug.
52
53The actual computation for triggering a native-allocation-GC is performed by
54`Heap::NativeMemoryOverTarget()`. This computes and compares two quantities:
55
561. An adjusted heap size for GC triggering. This consists of the Java heap size at which we would
57   normally trigger a GC plus an allowance for native heap size. This allowance currently consists
58   of one half (background processes) or three halves (foreground processes) of
59   `NativeAllocationGcWatermark()`. The latter is HeapMaxFree (typically 32MB) plus 1/8 of the
60   currently targeted heap size. For a foreground process, this allowance would typically be in
61   the 50-100 MB range for something other than a low-end device.
62
632. An adjusted count of current bytes allocated. This is basically the number of bytes currently
64   allocated in the Java heap, plus half the net number of native bytes allocated since the last
65   GC. The latter is computed as the change since the last GC in the total-bytes-allocated
66   obtained from `mallinfo()` plus `native_bytes_registered_`. The `native_bytes_registered_`
67   field tracks the bytes explicitly registered, and not yet unregistered via
68   `registerNativeAllocation` APIs. It excludes bytes registered as malloc-allocated via
69   `NativeAllocationRegistry`. (The computation also considers the total amount of native memory
70   currently allocated by this metric, as opposed to the change since the last GC. But that is
71   currently de-weighted to the point of insignificance.)
72
73A background GC is triggered if the second quantity exceeds the first. A stop-the-world-GC is
74triggered if the second quantity is at least 4 times larger than the first, and the native heap
75currently occupies a large fraction of device memory, suggesting that the GC is falling behind and
76endangering device usability.
77
78The fact that we consider both Java and native memory use at once means that we are more likely to
79trigger a native GC when we are closer to the normal Java GC threshold.
80
81The actual use of `mallinfo()` to compute native heap occupancy reflects experiments with
82different `libc` implementations. These have different performance characteristics, and sometimes
83disagree on the interpretation of `struct mallinfo` fields. We believe our current implementation
84is solid with scudo and jemalloc on Android, and minimally usable for testing elsewhere.
85
86(Some of this assumes typical current (May 2024) configuration constants, and may need to be
87updated.)
88