Reputation: 91
I have developed a flutter app using the Bloc pattern. Everything was fine until we encountered a memory crash in iOS. The Android version works well without any memory issues. But the iOS version of the app does have a memory issue. The memory is kept on accumulating when navigating to another page or for every background sync by staying at the same page (increasing memory even without navigation). Eventually, the app CRASHES due to the “Terminated due to memory issue” in the Xcode console. I have tried:
The Android version is working perfectly. What could be the problem for iOS only? Do we need to do something else in flutter explicitly, to free up the memory in iOS? Since I am closing the blocs and streams, ideally the memory should be disposed of while closing them right? See the attached screenshot which shows the memory heap in Xcode.
Upvotes: 9
Views: 2282
Reputation: 2311
You should give more information, even if you don't put code, at least what kind of components you use in your app/web. For example, some components that cause memory crashes are usually Cached Images, Video Players, Video Streaming, poor data caching management, rendering very heavy images...
On image caching there are several well-known proposals, for example, if you are using CachedNetworkImage
, do not forget to add memCacheHeight
and memCacheWidth
parameters.
You also could try to clean objects when the code detects that the state object will never build again.
Documentation : https://api.flutter.dev/flutter/widgets/NavigatorState/dispose.html
Exemple:
@override
void dispose() {
focusScopeNode.dispose();
for (final _RouteEntry entry in _history)
entry.dispose();
super.dispose();
}
Also, you have to review how you are using the navigator to change routes. Do you use navigator Navigator.pushReplacementNamed()
, Navigator.pushReplacement()
, push... ? Maybe your screens are stacking.
Upvotes: 3