Cutting FlutterBoost Background Memory from 827MB to 100MB: The Boundary-First Playbook
A background memory cut from 827MB to roughly 100MB directly affects how long an Android process survives after the app is hidden, and it eliminates a crash that can hit flagships. The boundary-first approach transfers to any hybrid engine that shares a single rendering context across multiple containers.
FlutterBoost's single-engine multi-container architecture keeps several Flutter pages and native screens on one engine, and that coupling shows up in memory numbers. One app measured 827MB TOTAL PSS in the background, with 354MB going to Graphics (GL) — the bulk from a map PlatformView and the engine's shared GL context. The author set two hard constraints: no Flutter upgrade and no change to the texture RenderMode, because forcing the engine lifecycle back to resumed in texture mode triggers a SIGSEGV through OnScreenSurfaceResize when the surface is already destroyed. That boundary defines everything else.
The first attempt hooked into MainActivity.onPause and onStop, but that fired whenever any Flutter page was pushed, not just when the app actually went to background. Calling stopRenderingToSurface() on the shared engine left every page permanently stalled, and detachFromEngineIfNeeded() corrupted the container stack. The correct signal tracks the count of foreground activities via Application.ActivityLifecycleCallbacks, with a 2-second delay to filter screen rotation and quick switch-aways, plus TRIM_MEMORY_UI_HIDDEN.
The release list includes map PlatformView removal, image cache clearing (limited to 64MB / 120 images), GIF frame pause, and native image cache cleanup. The key observation: FlutterBoost already dispatches onPageHide when the app backgrounds, and the engine naturally goes paused afterward, so the map gets disposed on a regular frame. scheduleWarmUpFrame() only covers extreme timing cases. Two details matter: the map removal uses a double gate suspendMap = Platform.isAndroid && (background || coveredByRoute) where coveredByRoute is driven solely by onPageShow/onPageHide, never a visibility provider that tab switches rewrite; and recovery has no timeout — poll until the engine actually resumes, because rebuilding a PlatformView on a detached container is a direct white-screen source. The result: GL dropped from 354MB, total from 827MB to about 100MB, white screen and the resize crash eliminated, all new code guarded by Platform.isAndroid, with a 0.5-second map rebuild as the only side effect.
The core insight is that in hybrid stacks, the framework's own lifecycle handling is often sufficient if you give it the right hook; the most stable optimization is the one that observes what the engine already does on background rather than forcing extra states.
The failed onPause approach illustrates a common trap: container callbacks are not app-level signals, and any optimization must verify that it's measuring the right lifecycle; connecting to a single activity's callbacks in a single-engine multi-container app is fundamentally wrong.
The 2-second delay and the use of TRIM_MEMORY_UI_HIDDEN are a strong example of filtering transient states; without that, screen rotation or a quick switch would cause false background detection and unnecessary teardown, which could lead to visible jank or even crashes.
The decision to treat the engine's rendering state as untouchable and instead release only regenerable resources is what makes the whole approach safe; it draws a clear line between what can be rebuilt and what cannot, and everything else follows from that.