Ram Mohan
Ram Mohan

Reputation: 91

Flutter iOS version app crashed due to consuming high memory

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:

  1. Closing the streams
  2. Optimising the code
  3. Closing all the blocs.
  4. Used Xcode Instruments to see the memory heap (It shows memory leaks but not much info available about any methods or objects that are retained.)
  5. Used Dart DevTools to profile the app. Still, not much info is available.
  6. Used ‘Observatory’ to see whether the memory is accumulating in any specific area. But no use.

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.

memory heap in Xcode

Upvotes: 9

Views: 2282

Answers (1)

Ricardo
Ricardo

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

Related Questions