Background concurrent copying GC freed - Flutter

前端 未结 5 1234
栀梦
栀梦 2021-02-19 18:56

In my Flutter log I\'m constantly getting this messages(just with some different numbers):

Background concurrent copying GC freed 153040(3MB) AllocSpace objects,         


        
5条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-19 19:32

    This is not an error, it's just an Android log message notifying you about when garbage collection takes place. Everything's normal. The log messages don't harm your app, see this question regarding the same topic on native Android. It's only a problem if you go out of memory, or you see performance hiccups due to garbage collection. Phew.


    That being said, let's see how you might get less of these messages.

    • Typically, an emulator's resources are limited. So, the easiest way would be to increase the emulator's RAM size or use an actual phone instead of an emulator.

    • Secondly, make sure your logic doesn't handle huge amounts of data, or if it does, that it gets disposed as soon as possible.

    • Also, don't "cache" widgets yourself by storing them in a state like this:

      class _MyWidgetState extends State {
        Widget button;
      
        @override
        void initState() {
          super.initState();
          button = RaisedButton(...);
        }
      
        @override
        Widget build() => button;
      }
      

      To get more information on why not to do this, check out my answer to a somewhat related question. Basically, Dart uses two types of garbage collectors, the Young Space Scavenger for short-lived objects and the Mark Sweep GC for long-lived ones. By caching your widgets manually, you're relying on the latter, which is slower and may actually notify Android about the freed memory, causing your logs.

    • Finally, you can always filter or ignore the logs. :D

提交回复
热议问题