Skip to content

[Flutter GPU] GpuImageSurface makes a new texture every frame until GC, pool never shrinks #193775

Open

Description

@pourjour

Steps to reproduce

  1. Make a new windows app, turn on Impeller in windows/runner/main.cpp:
    project.set_impeller_switch(flutter::ImpellerSwitch::Enabled);
  2. Add flutter_gpu (sdk) and vector_math to pubspec, put the code below in lib/main.dart
  3. flutter build windows --profile
  4. Run the exe with flutter gpu on:
    FLUTTER_ENGINE_SWITCHES=1 FLUTTER_ENGINE_SWITCH_1=enable-flutter-gpu ./build/windows/x64/runner/Profile/<app>.exe
    
  5. Watch the console

Expected results

The surface only needs 2 or 3 textures for this (the docs say it reuses a texture when Flutter is done with it), so I expected debugBackingTextureCount to stay around 2-3.

Actual results

It makes a new texture on every frame until the GC runs, then it stays at that number forever. I get 60 after 60 frames and then 62 for the rest of the run, and it never goes down. That's 62 textures for a 512x512 surface.

In my real app (bigger surface, 60 fps) I got 48, 87 and 54 in 3 runs, so it's 140-250 MB of textures that never get freed. The number changes between runs, I guess it depends on when the GC happens.

I looked at the engine code a bit. Surface::IsReusable in lib/gpu/surface.cc only reuses a texture when texture.use_count() <= 3, but the Texture, RenderPass and CommandBuffer I create each frame keep a ref to it until Dart finalizes them (the command buffer even keeps its encodables_ after submit). There's no way to dispose them from Dart, so they just sit there until GC. And PruneTextureRecords only drops textures when the size changes, so the pool never gets smaller.

I think this is kind of the same root problem as #191867 (no way to dispose gpu objects).

For now I made my own ring of 3 textures and show them with Texture.asImage(), and that stays at 3.

Code sample

Code sample
import 'dart:ui' as ui;

import 'package:flutter/scheduler.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_gpu/gpu.dart' as gpu;
import 'package:vector_math/vector_math.dart' as vm;

void main() {
  runApp(const Directionality(textDirection: TextDirection.ltr, child: Repro()));
}

class Repro extends StatefulWidget {
  const Repro({super.key});

  @override
  State<Repro> createState() => _ReproState();
}

class _ReproState extends State<Repro> with SingleTickerProviderStateMixin {
  final gpu.GpuImageSurface surface = gpu.gpuContext.createImageSurface(512, 512);
  late final Ticker ticker = createTicker(onTick);
  ui.Image? image;
  int frames = 0;

  @override
  void initState() {
    super.initState();
    ticker.start();
  }

  void onTick(Duration elapsed) {
    final frame = surface.acquireNextFrame();
    final commandBuffer = gpu.gpuContext.createCommandBuffer();
    final t = (frames % 120) / 120.0;
    commandBuffer.createRenderPass(
      gpu.RenderTarget.singleColor(
        gpu.ColorAttachment(
          texture: frame.colorTexture,
          loadAction: gpu.LoadAction.clear,
          clearValue: vm.Vector4(t, 0.3, 1.0 - t, 1.0),
        ),
      ),
    );
    frame.present(commandBuffer);
    commandBuffer.submit();

    frames++;
    if (frames % 60 == 0) {
      debugPrint('frame $frames: debugBackingTextureCount = ${surface.debugBackingTextureCount}');
    }

    final old = image;
    setState(() => image = surface.currentImage);
    if (old != null) {
      SchedulerBinding.instance.addPostFrameCallback((_) => old.dispose());
    }
  }

  @override
  void dispose() {
    ticker.dispose();
    image?.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return ColoredBox(
      color: const Color(0xFF000000),
      child: Column(
        children: [
          RawImage(image: image, width: 512, height: 512),
          Text(
            'frame $frames  debugBackingTextureCount ${surface.debugBackingTextureCount}',
            style: const TextStyle(color: Color(0xFFFFFFFF), fontSize: 20),
          ),
        ],
      ),
    );
  }
}

No shaders, no draws, it only clears the texture. I dispose every image from currentImage after the next frame, so it's not the images.

Logs

Logs
[IMPORTANT:flutter/shell/platform/embedder/embedder_surface_gl_impeller.cc(126)] Using the Impeller rendering backend (OpenGLESSDF).
frame 60: debugBackingTextureCount = 60
frame 120: debugBackingTextureCount = 62
frame 180: debugBackingTextureCount = 62
frame 240: debugBackingTextureCount = 62
frame 300: debugBackingTextureCount = 62
frame 360: debugBackingTextureCount = 62
frame 420: debugBackingTextureCount = 62
frame 480: debugBackingTextureCount = 62
frame 540: debugBackingTextureCount = 62
frame 600: debugBackingTextureCount = 62
frame 660: debugBackingTextureCount = 62
frame 720: debugBackingTextureCount = 62
frame 780: debugBackingTextureCount = 62
frame 840: debugBackingTextureCount = 62
frame 900: debugBackingTextureCount = 62
frame 960: debugBackingTextureCount = 62
frame 1020: debugBackingTextureCount = 62
frame 1080: debugBackingTextureCount = 62

Same numbers on a second run.

Flutter Doctor output

Doctor output
Flutter 3.47.0 • channel [user-branch] (3.47.0 tag checkout) • https://github.com/flutter/flutter.git
Framework • revision 4cf2416426 • 2026-08-11
Engine • revision 5f77625673
Tools • Dart 3.13.0 • DevTools 2.60.0

Windows 11 Pro (10.0.26200)
GPU: NVIDIA GeForce RTX 3050 Laptop GPU + Intel UHD Graphics
Impeller OpenGLES (ANGLE), profile build

Activity

  1. added
    in triage
    on Oct 5, 2026
  2. saurabh-mirajkar commented on Oct 5, 2026

    @saurabh-mirajkar
    Contributor

    Thanks for the report. I was able to reproduce on macOS with Flutter master using Impeller Metal. Routing this to the FlutterGPU team for further investigation.

    master
    Flutter 3.49.0-1.0.pre-306 • channel master • https://github.com/saurabh-mirajkar/flutter.git
    Framework • revision 9dfb14d6af (2 hours ago) • 2026-10-05 09:07:40 +0000
    Engine • hash 56950623b2f1f4ea7f1281c0bfb81cb6e988521d (revision 9dfb14d6af) (2 hours ago) • 2026-10-05 09:07:40.000Z
    Tools • Dart 3.14.0 (build 3.14.0-294.0.dev) • DevTools 2.61.0
    
    logs
    admin@Mac gpu_surface_repro % flutter run -d macos --profile --enable-flutter-gpu
    Launching lib/main.dart on macOS in profile mode...
    Building macOS application...                                           
    ✓ Built build/macos/Build/Products/Profile/gpu_surface_repro.app (57.2MB)
    [IMPORTANT:flutter/shell/platform/embedder/embedder_surface_metal_impeller.mm(53)] Using the Impeller rendering backend (MetalSDF).
    Failed to foreground app; open returned 1
    
    Flutter run key commands.
    h List all available interactive commands.
    c Clear the screen
    q Quit (terminate the application on the device).
    
    A Dart VM Service on macOS is available at: http://127.0.0.1:61881/LbF3JXGjdj4=/
    The Flutter DevTools debugger and profiler on macOS is available at:
    http://127.0.0.1:61881/LbF3JXGjdj4=/devtools/?uri=ws://127.0.0.1:61881/LbF3JXGjdj4=/ws
    flutter: frame 60: debugBackingTextureCount = 60
    flutter: frame 120: debugBackingTextureCount = 108
    flutter: frame 180: debugBackingTextureCount = 108
    flutter: frame 240: debugBackingTextureCount = 108
    flutter: frame 300: debugBackingTextureCount = 108
    flutter: frame 360: debugBackingTextureCount = 108
    flutter: frame 420: debugBackingTextureCount = 108
    flutter: frame 480: debugBackingTextureCount = 108
    flutter: frame 540: debugBackingTextureCount = 108
    flutter: frame 600: debugBackingTextureCount = 108
    flutter: frame 660: debugBackingTextureCount = 108
    flutter: frame 720: debugBackingTextureCount = 108
    flutter: frame 780: debugBackingTextureCount = 108
    flutter: frame 840: debugBackingTextureCount = 108
    flutter: frame 900: debugBackingTextureCount = 108
    flutter: frame 960: debugBackingTextureCount = 108
    flutter: frame 1020: debugBackingTextureCount = 108
    flutter: frame 1080: debugBackingTextureCount = 108
    flutter: frame 1140: debugBackingTextureCount = 108
    flutter: frame 1200: debugBackingTextureCount = 108
    flutter: frame 1260: debugBackingTextureCount = 108
    flutter: frame 1320: debugBackingTextureCount = 108
    flutter: frame 1380: debugBackingTextureCount = 108
    flutter: frame 1440: debugBackingTextureCount = 108
    flutter: frame 1500: debugBackingTextureCount = 108
    flutter: frame 1560: debugBackingTextureCount = 108
    flutter: frame 1620: debugBackingTextureCount = 108
    flutter: frame 1680: debugBackingTextureCount = 108
    flutter: frame 1740: debugBackingTextureCount = 108
    flutter: frame 1800: debugBackingTextureCount = 108
    flutter: frame 1860: debugBackingTextureCount = 108
    flutter: frame 1920: debugBackingTextureCount = 108
    flutter: frame 1980: debugBackingTextureCount = 108
    flutter: frame 2040: debugBackingTextureCount = 108
    flutter: frame 2100: debugBackingTextureCount = 108
    flutter: frame 2160: debugBackingTextureCount = 108
    flutter: frame 2220: debugBackingTextureCount = 108
    flutter: frame 2280: debugBackingTextureCount = 108
    flutter: frame 2340: debugBackingTextureCount = 108
    flutter: frame 2400: debugBackingTextureCount = 108
    flutter: frame 2460: debugBackingTextureCount = 108
    flutter: frame 2520: debugBackingTextureCount = 108
    flutter: frame 2580: debugBackingTextureCount = 108
    flutter: frame 2640: debugBackingTextureCount = 108
    flutter: frame 2700: debugBackingTextureCount = 108
    flutter: frame 2760: debugBackingTextureCount = 108
    flutter: frame 2820: debugBackingTextureCount = 108
    flutter: frame 2880: debugBackingTextureCount = 108
    flutter: frame 2940: debugBackingTextureCount = 108
    flutter: frame 3000: debugBackingTextureCount = 108
    flutter: frame 3060: debugBackingTextureCount = 108
    flutter: frame 3120: debugBackingTextureCount = 108
    flutter: frame 3180: debugBackingTextureCount = 108
    flutter: frame 3240: debugBackingTextureCount = 108
    flutter: frame 3300: debugBackingTextureCount = 108
    flutter: frame 3360: debugBackingTextureCount = 108
    flutter: frame 3420: debugBackingTextureCount = 108
    flutter: frame 3480: debugBackingTextureCount = 108
    flutter: frame 3540: debugBackingTextureCount = 108
    flutter: frame 3600: debugBackingTextureCount = 108
    flutter: frame 3660: debugBackingTextureCount = 108
    flutter: frame 3720: debugBackingTextureCount = 108
    flutter: frame 3780: debugBackingTextureCount = 108
    flutter: frame 3840: debugBackingTextureCount = 108
    flutter: frame 3900: debugBackingTextureCount = 108
    flutter: frame 3960: debugBackingTextureCount = 108
    flutter: frame 4020: debugBackingTextureCount = 108
    flutter: frame 4080: debugBackingTextureCount = 108
    flutter: frame 4140: debugBackingTextureCount = 108
    flutter: frame 4200: debugBackingTextureCount = 108
    flutter: frame 4260: debugBackingTextureCount = 108
    flutter: frame 4320: debugBackingTextureCount = 108
    
  3. added
    c: performance
    platform-macos
    a: desktop
    perf: memory
    team-fluttergpu
    and removed
    in triage
    on Oct 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    a: desktopc: performanceflutter-gpuperf: memoryplatform-macosteam-fluttergpu

    Type

    No type

    Projects

    • Status
      🤔 Needs Triage

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions