Skip to content

Instantly share code, notes, and snippets.

@jezell
Last active September 27, 2024 17:25
Show Gist options
  • Save jezell/bd2db0fba7834b11106f8ada372ab542 to your computer and use it in GitHub Desktop.
Save jezell/bd2db0fba7834b11106f8ada372ab542 to your computer and use it in GitHub Desktop.
Flutter web compositor memory leak
// Copyright 2019 the Dart project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license
// that can be found in the LICENSE file.
import 'package:flutter/material.dart';
import 'dart:js_interop';
void main() => runApp(const MyApp());
@JS('window.flutterCanvasKit.HEAP8.byteLength')
external JSNumber get _canvasKitMemory;
int getMemoryUsageBytes() {
return _canvasKitMemory.toDartInt;
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
debugShowCheckedModeBanner: false,
theme: ThemeData(
colorSchemeSeed: Colors.blue,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
final String title;
const MyHomePage({
super.key,
required this.title,
});
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() {
setState(() {
_counter++;
});
}
@override
void initState() {
super.initState();
queueFrame();
}
void queueFrame() {
WidgetsBinding.instance.addPostFrameCallback((_) {
if (!mounted) {
} else {
_incrementCounter();
queueFrame();
}
});
}
final focusNodes = List.generate(500, (x) => FocusNode());
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Expanded(
child: Stack(children: [
for (var focusNode in focusNodes)
Positioned(
top: 15.0 * focusNodes.indexOf(focusNode),
left: 15.0 * focusNodes.indexOf(focusNode),
child: Container(
decoration: BoxDecoration(
color:
Color.fromARGB(0xff, _counter % 255, 255, 255)),
width: 400,
height: 400,
clipBehavior: Clip.antiAlias,
child: SelectableRegion(
focusNode: focusNode,
selectionControls: materialTextSelectionControls,
child: Text(
'memory: ${getMemoryUsageBytes() / 1024 / 1024}MB $_counter ${focusNodes.indexOf(focusNode)}',
style: Theme.of(context).textTheme.bodySmall,
)))),
]))
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: const Icon(Icons.add),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment