Mobile Development 10 min read

How Flutter Manages Memory: Dart Runtime, Image & FlutterView Insights

This article explores Flutter's memory management, detailing the Dart runtime's generational GC, comparing Image widget memory usage across Android versions, and evaluating the impact of reusing or not reusing FlutterView on Java and native memory consumption, with practical test results and optimization tips.

Alibaba Cloud Developer
Alibaba Cloud Developer
Alibaba Cloud Developer
How Flutter Manages Memory: Dart Runtime, Image & FlutterView Insights

Dart Runtime Overview

Flutter framework is built with Dart, so each app includes a Dart VM. The VM compiles Dart to native bytecode via AOT, eliminating interpretation and improving performance. The article focuses on Dart VM memory allocation and garbage collection.

Dart isolates have separate heaps and stacks, each with its own GC, communicating via message channels. This isolation prevents data races; the entire Flutter widget rendering runs in a single isolate.

Exploring Image Memory

Flutter provides an Image widget. The article investigates whether Android ImageView uses Java heap or native memory by adding images incrementally and profiling with Android Profiler on Android 6.0, 7.0, and 8.0.

Results show Java heap growth on Android 6.0/7.0 and native memory growth on Android 8.0, indicating ImageView uses Java memory on older versions and native memory on newer ones.

Flutter Image uses neither Java nor native memory but Graphics memory, avoiding OOM risks and leveraging native memory.

Flutter's ImageCache caches images by count rather than precise memory size.

FlutterView Memory Exploration

When integrating Flutter into existing apps, developers must decide whether to create a new FlutterView per Activity or reuse an existing one. Reusing requires attaching the FlutterNativeView to each Activity, which can introduce memory‑leak risks if not detached properly.

Tests with empty pages show that not reusing a FlutterView adds about 0.73 MB native memory per page, while reusing adds about 0.65 MB, indicating native memory savings.

Reusing a single FlutterView can cause visual glitches during Activity switches; a possible mitigation is to capture a screenshot to mask the transition.

A hybrid approach using a FlutterViewProvider that maintains multiple reusable FlutterViews balances reuse benefits with stability.

Pre‑warming the first frame of a FlutterView in a 1‑pixel window can reduce startup latency.

The article concludes with practical insights from the Xianyu team on Flutter memory management and encourages further experimentation.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

DARTFlutterMobile Developmentperformance
Alibaba Cloud Developer
Written by

Alibaba Cloud Developer

Alibaba's official tech channel, featuring all of its technology innovations.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.