跪拜 Guibai
← Back to the summary

Cutting FlutterBoost Background Memory from 827MB to 100MB: The Boundary-First Playbook

Background Memory 827MB: I Cut It to 100MB Without Changing a Single Line of Business Logic

This is not a quick-fix article about "just configure this and optimize 90%". It's a complete review of architecture-level memory governance: a FlutterBoost hybrid stack app had a TOTAL PSS of 827MB after going to the background. Without upgrading Flutter, without changing the render mode, and without touching a single line of business logic, I brought it down.

After reading this, you'll get three things: a correct way to detect "true background", two boundaries you must never cross, and a release method that follows the framework's natural timing.

cover image

One-sentence conclusion: In cross-platform architectures, the hardest part is not getting pages to run, but understanding the lifecycle mismatch between the engine and the containers. Performance optimization requires first setting boundaries, then observing the framework's natural timing, and finally validating with observable data, not by "it feels faster".


1. The Problem: Memory Doesn't Drop When Going to Background

A FlutterBoost hybrid stack app, measured after going to the background:

Figure 1 Memory composition

Figure 1 · Composition of the 827MB background memory; GL alone is the largest at 354MB

That 354MB of GL mainly comes from two sources:

  1. Map PlatformView — a single TextureMapView occupies 100MB+.
  2. Flutter engine's GL context — under a single-engine architecture, multiple containers share a common set of rendering resources.

The goal is clear: only release regenerable resources, never touch the Flutter engine's rendering state. Maps can be rebuilt, image caches can be re-decoded — these are regenerable; once you mess with the engine's surface and rendering state, you get crashes and white screens.


2. Set Boundaries First: Two Red Lines You Must Never Cross

Many "optimization plans" fail because they secretly cross boundaries. I first list the unshakable premises:

  1. Don't upgrade Flutter (version locked, don't touch dependencies);
  2. Don't change RenderMode (keep FlutterBoost's default texture mode).

These two are not conservative; they are validated by blood and tears. Under these constraints, any operation that forces the engine lifecycle to "resumed" will introduce native crashes.

The principle is this: in texture mode, the underlying is FlutterTextureView + native JNISurfaceTextureThread. Forcing resumed drives recreateSurface → NotifyChanged(SkISize), delivering a surface resize to that thread. But at that moment the app is already in the background, and SurfaceTexture is being destroyed by the system; the onscreen surface referenced by the resize task is already empty — the result is a libflutter.so SIGSEGV (fault addr 0x0, stack lands on AndroidSurfaceGLSkia::OnScreenSurfaceResize).

Figure 2 Two untouchable boundaries

Figure 2 · Two unshakable boundaries; crossing them means crashes

Methodology One: List the unshakable boundaries first, then find solutions within them. If you start without clarifying the boundaries, optimization becomes a gamble.


3. Wrong Path: A Single Activity's onPause ≠ App Going to Background

The first version was intuitive: hook resource release into MainActivity.onPause / onStop.

Two serious problems appeared immediately after going live:

The root cause lies in FlutterBoost's single-engine multi-container architecture: when opening any Flutter page from the home page, MainActivity also goes through onPause → onStop. And at that moment I called:

Figure 3 Wrong path comparison

Figure 3 · In a single-engine architecture, container lifecycle ≠ application lifecycle

Methodology Two: In a single-engine architecture, container lifecycle ≠ application lifecycle. To judge "went to background", you must count the app-level foreground activities (1 → 0), never rely on a single activity's callbacks.


4. Final Solution: Detect True Background + Release Along the Framework's Natural Timing

4.1 Detecting True Background

Use Application.ActivityLifecycleCallbacks to count the number of foreground activities, and after it reaches zero, delay 2 seconds to confirm; also let TRIM_MEMORY_UI_HIDDEN trigger immediately.

The 2-second delay is not arbitrary; it filters two kinds of false positives: quick switch-away-and-return, and the brief zero caused by screen rotation.

4.2 Release Regenerable Resources

4.3 Key Finding: The Engine Already Does It for You

The part that saves real effort is observing what the framework has already done.

When going to the background, FlutterBoost dispatches onPageHide first — the engine is still rendering frames at that point, and the map gets removed from the tree and disposed in a regular frame. In other words, there's no need to "wake up" the engine to proactively rebuild or intervene; afterward the engine then naturally enters the paused state.

I only use scheduleWarmUpFrame() as a fallback for extreme timing: it drives a frame of build/layout/paint via a Timer, without relying on vsync, without changing lifecycle, and without rebuilding surfaces.

Figure 4 Final solution three steps

Figure 4 · Detect true background → release regenerable resources → fallback for extreme timing

Methodology Three: Work with the framework's existing natural timing rather than fighting it. First observe what the framework already does for you during lifecycle transitions, then fill in the last mile. This is often much more stable than "proactive intervention".


5. The Devil in Two Details

If the details aren't handled right, the plan still fails.

Map double gate: use suspendMap = Platform.isAndroid && (background || coveredByRoute). Here coveredByRoute is driven only by onPageShow / onPageHide; it represents pure "covered / detach" semantics — never use a visibility provider that can be overwritten by bottom tab switching, because switching tabs would mistakenly destroy the map (at that point the container is still attached, and creating the map is actually free).

No timeout for recovery: keep polling until the engine has truly resumed; it's strictly forbidden to force-flip after a timeout. Rebuilding a PlatformView on a detached container is the direct source of white screens. When the signal is lost, fall back to onPageShow.

Figure 5 Pitfall checklist

Figure 5 · Two details: map double gate + no timeout for recovery


6. Results and Boundaries

Figure 6 Result comparison

Figure 6 · 827MB → about 100MB, without changing a single line of business logic


7. Three Reusable Methodologies

I compressed this review into three sentences you can take away:

  1. Set boundaries first, then act: list the unshakable premises and find solutions within them;
  2. Distinguish container lifecycle from application lifecycle: to judge "went to background", look at the app-level foreground activity count, not a single activity's callbacks;
  3. Follow the timing, don't fight it: first observe what the framework has already done during the switch, then fill in the last mile.

In cross-platform architectures, the hardest part is not getting pages to run, but understanding the lifecycle mismatch between the engine and the containers. Performance optimization first requires setting boundaries, then looking at natural timing, and finally validating with observable data — not by "it feels faster".

How do you manage memory in the background in your projects? Have you stepped on the single-engine multi-container pit? Leave a comment; I'll read each one.


About me: An independent developer, building an app by myself, handling HarmonyOS, Android, and backend all on my own. 15 years of coding, from Android to Flutter and then HarmonyOS native; the developer of Quhui Jianbi, open-source author of AutoPage / Ulog, and national award winner in the Blue Bridge Cup. I write the pitfalls from real projects into solutions you can directly reuse.

#Flutter #性能优化 #Android