Skip to content

Scrolling ListView causes memory usage to vary by >10MB over time #20245

Closed

Description

@Wanghuayin

When I used listview to design a list with 100 items (using the same image), I found that when I scrolled up and down the list, the memory of _List will continue to increase, so the overall memory is also increasing, why memory Will it always increase? Native list will ensure stable memory when doing the same operation.

Steps to Reproduce

  1. Connect the Android device( os version 4.4.4/7.0 )
  2. Run->
    image
  3. Click the [Launch screen] button on the phone
  4. Scroll up and down several times

image
image
Scroll up and down several times
image
image

Code

https://github.com/Wanghuayin/flutterlistdemo

@override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Second Screen"),
      ),
      body: ListView.builder(
        itemCount: items.length,
        itemBuilder: (context, index) {
          return ListTile(
//            key: new Key('list-$index'),
            title: new Container(
              decoration: new BoxDecoration(border: new Border(top: new BorderSide(width: 1.0, color: Colors.grey.shade300))),
              padding: EdgeInsets.only(top: 7.0),
              child: new Row(
                children: <Widget>[
                  new Container(
                    child: Image.network(
                      'http://gw.alicdn.com/mt/TB1p6R6G41YBuNjy1zcXXbNcXXa-220-120.jpg',
                      width: 220.0,
                      height: 120.0,
                      fit: BoxFit.fill,
                    ),
                  ),
                  new Container(
                    margin: new EdgeInsets.only(left: 10.0),
                    child: new Column(
                      children: <Widget>[
                        new Padding(padding: new EdgeInsets.fromLTRB(50.0, 1.0, 1.0, 1.0)),
                        new Text("title $index", style: new TextStyle(fontSize:14.0, color: Colors.red),),
                        new Padding(padding: new EdgeInsets.fromLTRB(1.0, 20.0, 1.0, 1.0)),
                        new Text("desc $index", style: new TextStyle(fontSize:12.0, color: Colors.blue),),
                      ],
                    ),
                  ),
                ],
              ),
            ),
          );
        },
      ),
    );
}

log

dengyangdeMacBook-Pro:flutter_app dengyang$ flutter doctor -v
[KWLM]:[doctor, -v]
[✓] Flutter (Channel xy_beta_v0.5.6, v0.5.6-pre.108, on Mac OS X 10.13.4 17E199, locale zh-Hans-CN)
    • Flutter version 0.5.6-pre.108 at /Users/dengyang/SourceCode/FlutterProject/fwn_idlefish/flutter
    • Framework revision f8ba2eeab6 (12 days ago), 2018-07-25 16:48:19 +0800
    • Engine revision 6fe748490d
    • Dart version 2.0.0-dev.63.0.flutter-4c9689c1d2

[!] Android toolchain - develop for Android devices (Android SDK 28.0.0)
    • Android SDK at /Users/dengyang/Library/Android/sdk
    • Android NDK at /Users/dengyang/Library/Android/sdk/ndk-bundle
    • Platform android-27, build-tools 28.0.0
    • ANDROID_HOME = /Users/dengyang/Library/Android/sdk
    • Java binary at: /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 1.8.0_152-release-1024-b01)
    ! Some Android licenses not accepted.  To resolve this, run: flutter doctor --android-licenses

[✓] iOS toolchain - develop for iOS devices (Xcode 9.4.1)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Xcode 9.4.1, Build version 9F2000
    • ios-deploy 1.9.2
    • CocoaPods version 1.5.3

[✓] Android Studio (version 3.1)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin version 25.0.1
    • Dart plugin version 173.4700
    • Java version OpenJDK Runtime Environment (build 1.8.0_152-release-1024-b01)

[!] IntelliJ IDEA Ultimate Edition (version 2017.3.2)
    • IntelliJ at /Applications/IntelliJ IDEA.app
    ✗ Flutter plugin not installed; this adds Flutter specific functionality.
    ✗ Dart plugin not installed; this adds Dart specific functionality.
    • For information about installing plugins, see
      https://flutter.io/intellij-setup/#installing-the-plugins

[✓] IntelliJ IDEA Community Edition (version 2017.3)
    • IntelliJ at /Applications/IntelliJ IDEA CE.app
    • Flutter plugin version 22.1.2
    • Dart plugin version 173.4548.30

[✓] VS Code (version 1.24.1)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 2.16.0

[✓] Connected devices (1 available)
    • SM G950U • 9887a8335150524c59 • android-arm64 • Android 7.0 (API 24)

! Doctor found issues in 2 categories.

Activity

  1. Wanghuayin commented on Aug 7, 2018

    @Wanghuayin
    Author

    After I tried to remove the picture from the list, when I scrolled up and down the list, the memory of _List will continue to increase, so it seems that it should be irrelevant to the picture. In time, the app back to the background, and the memory is not released in time,This operation may trigger a memory leak in the Listview

  2. Wanghuayin commented on Aug 7, 2018

    @Wanghuayin
    Author

    Scrolling up and down (Base on custom 0.5.6 version of the Xianyu)

    Total Memory: 145.3M Total Memory: 152.4M Total Memory: 156.3M Total Memory: 157.2M Total Memory: 157.1M Total Memory: 159.5M Total Memory: 159.6M Total Memory: 161.5M
    Idle Idle Idle Idle Idle Idle Idle Idle
    0.00% 0.00% 0.00% 0.00% 0.00% 0.00% 0.00% 0.00%
    LoadWait LoadWait LoadWait LoadWait LoadWait LoadWait LoadWait LoadWait
    0.00% 0.00% 0.00% 0.00% 0.00% 0.00% 0.00% 0.00%
    VM VM VM VM VM VM VM VM
    83.41% 45.48% 36.38% 27.14% 26.91% 25.31% 23.99% 22.83%
    CompileOptimized CompileOptimized CompileOptimized CompileOptimized CompileOptimized CompileOptimized CompileOptimized CompileOptimized
    0.00% 0.00% 0.00% 0.00% 0.00% 0.00% 0.00% 0.00%
    CompileUnoptimized CompileUnoptimized CompileUnoptimized CompileUnoptimized CompileUnoptimized CompileUnoptimized CompileUnoptimized CompileUnoptimized
    0.00% 0.00% 0.00% 0.00% 0.00% 0.00% 0.00% 0.00%
    CompileClass CompileClass CompileClass CompileClass CompileClass CompileClass CompileClass CompileClass
    0.00% 0.00% 0.00% 0.00% 0.00% 0.00% 0.00% 0.00%
    CompileTopLevel CompileTopLevel CompileTopLevel CompileTopLevel CompileTopLevel CompileTopLevel CompileTopLevel CompileTopLevel
    0.00% 0.00% 0.00% 0.00% 0.00% 0.00% 0.00% 0.00%
    CompileScanner CompileScanner CompileScanner CompileScanner CompileScanner CompileScanner CompileScanner CompileScanner
    0.00% 0.00% 0.00% 0.00% 0.00% 0.00% 0.00% 0.00%
    CompileParseFunction CompileParseFunction CompileParseFunction CompileParseFunction CompileParseFunction CompileParseFunction CompileParseFunction CompileParseFunction
    0.00% 0.00% 0.00% 0.00% 0.00% 0.00% 0.00% 0.00%
    CompileParseRegExp CompileParseRegExp CompileParseRegExp CompileParseRegExp CompileParseRegExp CompileParseRegExp CompileParseRegExp CompileParseRegExp
    0.00% 0.00% 0.00% 0.00% 0.00% 0.00% 0.00% 0.00%
    CompileFlowGraphBuilder CompileFlowGraphBuilder CompileFlowGraphBuilder CompileFlowGraphBuilder CompileFlowGraphBuilder CompileFlowGraphBuilder CompileFlowGraphBuilder CompileFlowGraphBuilder
    0.00% 0.00% 0.00% 0.00% 0.00% 0.00% 0.00% 0.00%
    Dart Dart Dart Dart Dart Dart Dart Dart
    8.12% 30.29% 33.46% 39.67% 39.82% 40.68% 41.37% 41.62%
    GCNewSpace GCNewSpace GCNewSpace GCNewSpace GCNewSpace GCNewSpace GCNewSpace GCNewSpace
    0.81% 2.86% 3.50% 3.77% 3.79% 3.85% 3.84% 4.02%
    GCOldSpace GCOldSpace GCOldSpace GCOldSpace GCOldSpace GCOldSpace GCOldSpace GCOldSpace
    2.12% 4.89% 7.25% 7.90% 8.05% 8.27% 8.50% 8.89%
    Embedder Embedder Embedder Embedder Embedder Embedder Embedder Embedder
    0.00% 0.00% 0.00% 0.00% 0.00% 0.00% 0.00% 0.00%
    Runtime Runtime Runtime Runtime Runtime Runtime Runtime Runtime
    0.58% 0.64% 0.65% 0.64% 0.61% 0.61% 0.68% 0.64%
    Native Native Native Native Native Native Native Native
    4.96% 15.83% 18.76% 20.87% 20.83% 21.29% 21.62% 22.00%
  3. Wanghuayin commented on Aug 8, 2018

    @Wanghuayin
    Author

    Base on the official 0.5.6 version

    178.8MB 194.3MB 198.0MB 196.7MB
    Idle Idle Idle Idle
    0.00% 0.00% 0.00% 0.00%
    LoadWait LoadWait LoadWait LoadWait
    0.00% 0.00% 0.00% 0.00%
    VM VM VM VM
    88.90% 25.51% 20.36% 17.16%
    CompileOptimized CompileOptimized CompileOptimized CompileOptimized
    0.00% 0.00% 0.00% 0.00%
    CompileUnoptimized CompileUnoptimized CompileUnoptimized CompileUnoptimized
    0.00% 0.00% 0.00% 0.00%
    CompileClass CompileClass CompileClass CompileClass
    0.00% 0.00% 0.00% 0.00%
    CompileTopLevel CompileTopLevel CompileTopLevel CompileTopLevel
    0.00% 0.00% 0.00% 0.00%
    CompileScanner CompileScanner CompileScanner CompileScanner
    0.00% 0.00% 0.00% 0.00%
    CompileParseFunction CompileParseFunction CompileParseFunction CompileParseFunction
    0.00% 0.00% 0.00% 0.00%
    CompileParseRegExp CompileParseRegExp CompileParseRegExp CompileParseRegExp
    0.00% 0.00% 0.00% 0.00%
    CompileFlowGraphBuilder CompileFlowGraphBuilder CompileFlowGraphBuilder CompileFlowGraphBuilder
    0.00% 0.00% 0.00% 0.00%
    Dart Dart Dart Dart
    4.41% 38.86% 42.46% 44.09%
    GCNewSpace GCNewSpace GCNewSpace GCNewSpace
    0.39% 5.13% 5.10% 5.35%
    GCOldSpace GCOldSpace GCOldSpace GCOldSpace
    1.57% 9.78% 9.66% 10.69%
    Embedder Embedder Embedder Embedder
    0.00% 0.00% 0.00% 0.00%
    Runtime Runtime Runtime Runtime
    1.50% 1.02% 0.80% 0.73%
    Native Native Native Native
    3.23% 19.71% 21.61% 21.98%
  4. jason-simmons commented on Aug 9, 2018

    @jason-simmons
    Member

    I looked into the memory consumption of this app during scrolling and noticed some unusual behavior in the Dart garbage collector:

    • The GC can get into a state where the threshold for collection grows by the maximum delta.
      • In this scenario, the k parameter in PageSpaceController::EvaluateGarbageCollection is calculated with a low value. The search for local_grow_heap converges on heap_growth_max_, resulting in the output gc_threshold_in_words_ reaching a value on the order of 75MB.
    • The GC was also sometimes deciding to promote many of the objects allocated during scrolling into the old generation.

    If these two effects occur, the process will continue to accumulate tens of MB of old generation objects before triggering a full GC.

    @rmacnak-google @a-siva

  5. Wanghuayin commented on Aug 13, 2018

    @Wanghuayin
    Author

    I did the another test with the engine change by Change-Id(Ie993f4bbac6435b2305e46cfbe0e95115e8554f8) again in the same scene, it's true that GC is much more active than before, but the total memory still increase with scrolling up and down

    image

    66.0MB 73.2MB 75.7MB 86.1MB 89.0MB
    Idle Idle Idle Idle Idle
    0.00% 0.00% 0.00% 0.00% 0.00%
    LoadWait LoadWait LoadWait LoadWait LoadWait
    0.00% 0.00% 0.00% 0.00% 0.00%
    VM VM VM VM VM
    68.43% 36.27% 30.97% 25.56% 23.03%
    CompileOptimized CompileOptimized CompileOptimized CompileOptimized CompileOptimized
    0.00% 0.00% 0.00% 0.00% 0.00%
    CompileUnoptimized CompileUnoptimized CompileUnoptimized CompileUnoptimized CompileUnoptimized
    0.00% 0.00% 0.00% 0.00% 0.00%
    CompileClass CompileClass CompileClass CompileClass CompileClass
    0.00% 0.00% 0.00% 0.00% 0.00%
    CompileTopLevel CompileTopLevel CompileTopLevel CompileTopLevel CompileTopLevel
    0.00% 0.00% 0.00% 0.00% 0.00%
    CompileScanner CompileScanner CompileScanner CompileScanner CompileScanner
    0.00% 0.00% 0.00% 0.00% 0.00%
    CompileParseFunction CompileParseFunction CompileParseFunction CompileParseFunction CompileParseFunction
    0.00% 0.00% 0.00% 0.00% 0.00%
    CompileParseRegExp CompileParseRegExp CompileParseRegExp CompileParseRegExp CompileParseRegExp
    0.00% 0.00% 0.00% 0.00% 0.00%
    CompileFlowGraphBuilder CompileFlowGraphBuilder CompileFlowGraphBuilder CompileFlowGraphBuilder CompileFlowGraphBuilder
    0.00% 0.00% 0.00% 0.00% 0.00%
    Dart Dart Dart Dart Dart
    9.68% 34.15% 39.50% 44.78% 49.48%
    GCNewSpace GCNewSpace GCNewSpace GCNewSpace GCNewSpace
    0.42% 4.41% 4.16% 3.82% 3.06%
    GCOldSpace GCOldSpace GCOldSpace GCOldSpace GCOldSpace
    3.79% 1.72% 1.64% 1.18% 0.47%
    Embedder Embedder Embedder Embedder Embedder
    0.00% 0.00% 0.00% 0.00% 0.00%
    Runtime Runtime Runtime Runtime Runtime
    1.47% 0.92% 0.52% 2.64% 2.17%
    Native Native Native Native Native
    16.21% 22.54% 23.21% 22.02% 21.80%

    New Generation
    used 980.4KB of 16.0MB
    external 185.3KB
    collections 126
    average collection time 6.17 ms
    cumulative collection time 0.78 secs
    average time between collections 3861.12 ms
    Old Generation
    used 6.8MB of 7.0MB
    external 184.5KB
    collections 8
    average collection time 17.09 ms
    cumulative collection time 0.14 secs
    average time between collections

    Too frequent GC may affect the user experience @jason-simmons @rmacnak-google @a-siva

  6. rmacnak-google commented on Aug 14, 2018

    @rmacnak-google
    Contributor

    What is "VM Memory" and why is it going down?

  7. Wanghuayin commented on Aug 14, 2018

    @Wanghuayin
    Author

    @rmacnak-google, sorry, it's my bad, i mistakenly treat the "VM activities" as "VM memory", i have updated the previous comment

  8. rmacnak-google commented on Aug 14, 2018

    @rmacnak-google
    Contributor

    Oh, sorry, I thought you were adding a value from some other tool. The UI should be more clear: the pie chart and its legend are reporting time (profile ticks), not memory. The fact that "Dart" is going up means that after startup a greater proportion of time is spent in generated code, and "VM" going down means that a smaller proportion of time is spent in the runtime.

  9. Hixie commented on Aug 14, 2018

    @Hixie
    Contributor

    @Wanghuayin Can you confirm that there is still an issue here?

  10. Wanghuayin commented on Aug 15, 2018

    @Wanghuayin
    Author

    @rmacnak-google @Hixie
    I still have two questions?

    1. Why does Observtory use RSS Memory as the total memory calculation method?
    2. when I use the 'adb shell dumpsys meminfo packagename' to calculate PSS, PSS will increase the memory of the list by up to a dozen MB in the process of scrolling up and down, while the Native is the same list implementation, the memory is very stable?
  11. 5 remaining items

  12. Hixie commented on Aug 21, 2018

    @Hixie
    Contributor

    @Wanghuayin Those graphs look great. Is there anything you'd still like us to do here?

  13. added
    waiting for responseThe Flutter team cannot make further progress on this issue until the original reporter responds
    on Aug 21, 2018
  14. Wanghuayin commented on Aug 22, 2018

    @Wanghuayin
    Author

    @Hixie Because the memory problem is block issue in the Xianyu app, I hope to solve the memory problem asap, thanks million!

    Btw, the high utilization of the CPU in the ListView scenario is also a problem worthy of attention.
    image

  15. removed
    waiting for responseThe Flutter team cannot make further progress on this issue until the original reporter responds
    on Aug 22, 2018
  16. Hixie commented on Aug 22, 2018

    @Hixie
    Contributor

    @Wanghuayin could you provide a test app and specify exactly what metrics you'd like us to hit? This bug is a bit vague so far, we're not clear on what needs to happen exactly or what exactly the problem is.

  17. Wanghuayin commented on Aug 23, 2018

    @Wanghuayin
    Author

    hi @Hixie

    1. App demo : https://github.com/Wanghuayin/flutterlistdemo
    2. Repro step:
      Just scrolling up and down
      2018-08-23-16-09-57-min
    3. We hope that in the process of scrolling up and down in the list, the memory can be kept stable, instead of fluctuations of tens of megabytes, because the large fluctuations in memory will cause the smoothness on the low-end machine, even Crash.
  18. changed the title [-]Memory issue on ListView.builder[/-] [+]Scrolling ListView causes memory usage to vary by >10MB over time[/+] on Aug 28, 2018
  19. added this to the milestone on Aug 28, 2018
  20. Hixie commented on Aug 28, 2018

    @Hixie
    Contributor

    @Wanghuayin Can you elaborate on why varying memory usage might cause lack of smoothness or crashes?

    Generally speaking this is the behaviour we would expect, as we allocate memory then release it each time we do a garbage collection sweep. We could just not release it, but that seems worse overall. I suppose we could release it less often, maybe?

  21. Wanghuayin commented on Aug 29, 2018

    @Wanghuayin
    Author

    @Hixie I think that the flutter listview is not smooth compared to the native Native listview, doing the same operation on the native listview, the experience will be smoother than flutter, and in the memory management, the native listview scrolling. At the time, the memory is almost constant (why is just a simple scrolling operation, flutter will have such a large memory change, is it determined by the mechanism of skia?)

    About Crash, I am sorry, it is my guess, I just seriously studied the isolate of memory, found that the probability of Crash under the isolate mechanism will be lower than Native, because the upper limit of isolate memory is system memory?

    Under the premise of ensuring performance, it is a good idea to perform memory recycling more frequently, or you can go deep into why there is a large memory fluctuation under such a simple listview.

  22. Hixie commented on Sep 4, 2018

    @Hixie
    Contributor

    @Hixie I think that the flutter listview is not smooth compared to the native Native listview, doing the same operation on the native listview, the experience will be smoother than flutter, and in the memory management, the native listview scrolling. At the time, the memory is almost constant

    If you mean that the Android list view has a smoother memory usage graph, then that is not very surprising to us, as it probably is just an indication of a different garbage collection and allocation model. Java's "recycler view" model is to reuse the same objects over time, avoiding allocation, whereas Flutter's "react-style" model is to allocate new objects every time there is a new item in the list, or any time the items change. The results of this is that the memory graph will appear less smooth, and allocations will appear to be happening more often, but the end result should be the same user experience.

    (why is just a simple scrolling operation, flutter will have such a large memory change, is it determined by the mechanism of skia?)

    To clarify, this is not related to Skia but to the "react-style" model that Flutter's framework uses.

    About Crash, I am sorry, it is my guess, I just seriously studied the isolate of memory, found that the probability of Crash under the isolate mechanism will be lower than Native, because the upper limit of isolate memory is system memory?

    We don't believe that there should be any difference in probability of crash in the "react-style" model vs the "recycler" model, but we're not sure we fully understand your concern. The allocated memory is released regularly, so there should not be a concern of the memory usage being unbounded.

    Under the premise of ensuring performance, it is a good idea to perform memory recycling more frequently, or you can go deep into why there is a large memory fluctuation under such a simple listview.

    The actual amount will depend on Dart's garbage collection settings. If you're seeing 10MB, then I would assume that this means that our garbage collector is tuned to collect garbage when there are about 10MB of allocations to clean up.

  23. Wanghuayin commented on Sep 7, 2018

    @Wanghuayin
    Author

    @Hixie thank you very much for your information that refreshed my cognition,
    I think the memory problem we encountered should be in a more complicated scenario. We will continue to follow up on this issue.

  24. github-actions commented on Aug 15, 2021

    @github-actions

    This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.

  25. locked as resolved and limited conversation to collaborators on Aug 15, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    customer: alibabaframeworkflutter/packages/flutter repository. See also f: labels.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions