Repository navigation
Open
Labels
P2Important issues not at the top of the work listf: routesNavigator, Router, and related APIs.found in release: 2.5Found to occur in 2.5found in release: 2.6Found to occur in 2.6frameworkflutter/packages/flutter repository. See also f: labels.has reproducible stepsThe issue has been confirmed reproducible and is ready to work onperf: memoryPerformance issues related to memoryteam-frameworkOwned by Framework teamtriaged-frameworkTriaged by Framework team
Description
Having even empty BottomNavigationBar causes a memory leak
Removing BottomNavigationBar causes the leak to disappear.
Details
Running code and do the steps:
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: HomePage(),
onGenerateRoute: (RouteSettings settings) {
final args = settings.arguments;
switch (settings.name) {
case "/bar":
return MaterialPageRoute(builder: (_) => BarPage());
}
},
);
}
}
class HomePage extends StatelessWidget {
HomePage({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Home screen'),
),
floatingActionButton: FloatingActionButton(
onPressed: () {
Navigator.of(context).pushNamed("/bar");
},
child: const Icon(Icons.navigation),
backgroundColor: Colors.green,
),
);
}
}
class BarPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Bar screen'),
),
bottomNavigationBar: BottomNavigationBar(
items: [],
),
);
}
}- run the app and open dev tools
- press on FAB to open BarPage and then press back button to close it
- repeat multiple times
- do a heap snapshot in Memory tab of dev tools
- observe multiple instances of BarPage still present in memory
Target Platform: Android, iOS
Target OS version/browser:
Devices:
Logs
Logs
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 2.2.3, on macOS 11.5.2 20G95 darwin-x64, locale en-GB)
[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.3)
[✓] Xcode - develop for iOS and macOS
[✓] Chrome - develop for the web
[!] Android Studio
✗ Android Studio not found at /Volumes/Data/Applications/Android Studio 4.0.1.app/Contents
[✓] IntelliJ IDEA Ultimate Edition (version 2021.2.1)
[✓] VS Code (version 1.60.2)
[✓] Connected device (3 available)
! Doctor found issues in 1 category.
Metadata
Metadata
Assignees
Labels
P2Important issues not at the top of the work listf: routesNavigator, Router, and related APIs.found in release: 2.5Found to occur in 2.5found in release: 2.6Found to occur in 2.6frameworkflutter/packages/flutter repository. See also f: labels.has reproducible stepsThe issue has been confirmed reproducible and is ready to work onperf: memoryPerformance issues related to memoryteam-frameworkOwned by Framework teamtriaged-frameworkTriaged by Framework team
Activity
maheshj01 commentedon Oct 12, 2021
Hi @aaverin Thanks for filing the issue. I verified the issue on the latest
stable 2.5.2and could not reproduce it after several push and pops I see only two Instances of BarPage.flutter doctor -v
modified code
added two bottom navigation bar items
Could you please try upgrading to the latest
stable 2.5.2and see if that fixes the issue?Thanks
tonnyavery commentedon Oct 12, 2021
@maheshmnj Thanks, I will try on latest version.
By the way, why would there be 2 instances of BarPage still in memory? Shouldn't there be 0 instances if BarPage is not on the screen anymore?
maheshj01 commentedon Oct 12, 2021
I agree,
Please try and let us know.
tonnyavery commentedon Oct 12, 2021
@maheshmnj I can reliably reproduce 1-2 instances of BarPage sitting in memory when running in
profilemode on flutter 2.5.2. But removing BottomNavigationBar doesn't seem to solve the leak anymore, so it's probably not related directly to BottomNavigationBar implementation and is rather some core problem of flutter.Is there already an issue that tracks this problem and is there any solution planned?
maheshj01 commentedon Oct 13, 2021
I can reproduce the issue after several push and pops (around 20) I see 2 instances of the popped Page in Memory (
BarPage). I verified this on stable 2.5.2 and the master channel. This also happens when usingMaterialPageRoutesin both debug and release mode.code sample
flutter doctor -v
logs (debug)
[-]Empty BottomNavigationBar leaks screen where it is used[/-][+][Performance] Widget still in Memory when popped[/+]tonnyavery commentedon Oct 22, 2021
@maheshmnj Any update on this ticket?
So far it looks like at least one copy of every widget stays in memory when it shouldn't. For some native-backed widgets it results in big problems when database connection is in use natively.
tonnyavery commentedon Oct 22, 2021
Is this related to #79605?
maheshj01 commentedon Oct 26, 2021
@aaverin you will see an update on this issue once the issue gets prioritized.