I'm figuring out whether my app has memory leak.
So I run flutter app on profile mode, not debug mode and see Memory tab.
This is the Profile Memory tab when I launch my app right away.
It's the just one example case.
You can see SignUpScreen
has 5 instances and MainNavigationScreen
has 14 instances.
But as seeing my code, when app launches it directs to SplashScreen
.
final router = GoRouter(
initialLocation: '/',
routes: [
GoRoute(
path: '/',
builder: (context, state) {
return const SplashScreen();
},
),
GoRoute(
path: '/p/:sendUserId',
builder: (context, state) {
final sendUserId = state.pathParameters["sendUserId"];
return PartnerAgreementScreen(sendUserId: sendUserId);
},
),
],
);
@override
void initState() {
super.initState();
_initializeNavigator();
}
@override
void dispose() {
super.dispose();
}
Future<UserProfile?> _initializeUserProfile() async {
final userProfile =
await ref.read(userProvider.notifier).fetchLoggedUserInfo();
return userProfile;
}
Future<void> _initializeNavigator() async {
try {
final userProfile = await _initializeUserProfile();
if (initialLoginState.value && userProfile != null) {
await Future.wait([
_intializeUserIdPrefs(),
_initializeAppVersion(),
_initializeFcmToken(),
_initializeInvitationCode(),
_initializeFontSize(),
]);
if (!mounted) return;
Navigator.of(context).push(
MaterialPageRoute(
builder: (_) => const MainNavigationScreen(tab: "일기"),
),
// (route) => false,
);
} else {
Future.delayed(const Duration(seconds: 1), () {
if (!mounted) return;
Navigator.of(context).push(
MaterialPageRoute(
builder: (_) => const SignUpScreen(),
),
// (route) => false,
);
});
}
} catch (e) {
// ignore: avoid_print
print("_initializeNavigator -> $e");
Future.delayed(const Duration(seconds: 1), () {
if (!mounted) return;
Navigator.of(context).pushAndRemoveUntil(
MaterialPageRoute(
builder: (_) => const SignUpScreen(),
),
(route) => false,
);
});
}
}
And SplashScreen redirects user to MainNavigationScreen
or SignUpscreen
.
In this case that user is logged in, it will directs user to MainNavigationScreen
, not SignUpScreen
.
I double check that it directs to 'MainNavigationScreen' only once and not 'SignUpScreen'.
But why does memory has 14 instances of MainNavigationScreen and 5 SignUpScreen?
And even DiaryModel
that will be used in another Screen has also 2 instances, etc.