Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memory leak and screen become black when using PlatformView #24145

Closed
Fengxiaojun47503 opened this issue Nov 9, 2018 · 12 comments
Closed

Memory leak and screen become black when using PlatformView #24145

Fengxiaojun47503 opened this issue Nov 9, 2018 · 12 comments
Assignees
Labels
a: platform-views Embedding Android/iOS views in Flutter apps c: performance Relates to speed or footprint issues (see "perf:" labels) customer: dream (g3) engine flutter/engine repository. See also e: labels. platform-android Android applications specifically
Milestone

Comments

@Fengxiaojun47503
Copy link

Fengxiaojun47503 commented Nov 9, 2018

Steps to Reproduce

  1. create a listview include platform view, for example ervery 5 item there is a AndroidView
  2. repeatedly scroll down and up the listview, so that the AndroidView will be new create and dispose repeatedly.

result: the screen will become black and nothing will be shown after several times of step2.
And in the log, StrictMode show warning of that surface never release.
Out of memory error log occured at the same time

Logs

2018-11-09 13:55:20.215 14249-14258/xxx E/StrictMode: A resource was acquired at attached stack trace but never released. See java.io.Closeable for information on avoiding resource leaks.
    java.lang.Throwable: Explicit termination method 'release' not called
        at dalvik.system.CloseGuard.open(CloseGuard.java:180)
        at android.view.Surface.setNativeObjectLocked(Surface.java:511)
        at android.view.Surface.<init>(Surface.java:179)
        at io.flutter.plugin.platform.VirtualDisplayController.create(VirtualDisplayController.java:30)
        at io.flutter.plugin.platform.PlatformViewsController.createPlatformView(PlatformViewsController.java:179)
        at io.flutter.plugin.platform.PlatformViewsController.onMethodCall(PlatformViewsController.java:118)
        at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:200)
        at io.flutter.view.FlutterNativeView.handlePlatformMessage(FlutterNativeView.java:163)
        at android.os.MessageQueue.nativePollOnce(Native Method)
        at android.os.MessageQueue.next(MessageQueue.java:323)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:6119)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
2018-11-09 13:54:34.450 14249-14258/xxx E/StrictMode: A resource was acquired at attached stack trace but never released. See java.io.Closeable for information on avoiding resource leaks.
    java.lang.Throwable: Explicit termination method 'release' not called
        at dalvik.system.CloseGuard.open(CloseGuard.java:180)
        at android.view.Surface.setNativeObjectLocked(Surface.java:511)
        at android.view.Surface.<init>(Surface.java:179)
        at io.flutter.plugin.platform.VirtualDisplayController.create(VirtualDisplayController.java:30)
        at io.flutter.plugin.platform.PlatformViewsController.createPlatformView(PlatformViewsController.java:179)
        at io.flutter.plugin.platform.PlatformViewsController.onMethodCall(PlatformViewsController.java:118)
        at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:200)
        at io.flutter.view.FlutterNativeView.handlePlatformMessage(FlutterNativeView.java:163)
        at android.os.MessageQueue.nativePollOnce(Native Method)
        at android.os.MessageQueue.next(MessageQueue.java:323)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:6119)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

2018-11-09 13:55:25.168 14249-14416/xxx W/Adreno-GSL: <sharedmem_gpumem_alloc_id:2372>: sharedmem_gpumem_alloc: mmap failed errno 12 Out of memory
2018-11-09 13:55:25.186 14249-14416/xxx E/Adreno-GSL: <gsl_memory_alloc_pure:2115>: GSL MEM ERROR: kgsl_sharedmem_alloc ioctl failed.
2018-11-09 13:55:26.240 14249-14887/xxx W/Ads: Not retrying to fetch app settings
2018-11-09 13:55:29.344 14249-14416/xxx W/Adreno-GSL: <sharedmem_gpumem_alloc_id:2372>: sharedmem_gpumem_alloc: mmap failed errno 12 Out of memory
2018-11-09 13:55:29.351 14249-14416/xxx E/Adreno-GSL: <gsl_memory_alloc_pure:2115>: GSL MEM ERROR: kgsl_sharedmem_alloc ioctl failed.
2018-11-09 13:55:29.370 14249-14416/xxx W/Adreno-GSL: <sharedmem_gpumem_alloc_id:2372>: sharedmem_gpumem_alloc: mmap failed errno 12 Out of memory
2018-11-09 13:55:29.376 14249-14416/xxx E/Adreno-GSL: <gsl_memory_alloc_pure:2115>: GSL MEM ERROR: kgsl_sharedmem_alloc ioctl failed.
2018-11-09 13:55:29.395 14249-14416/xxx W/Adreno-GSL: <sharedmem_gpumem_alloc_id:2372>: sharedmem_gpumem_alloc: mmap failed errno 12 Out of memory
2018-11-09 13:55:29.401 14249-14416/xxx E/Adreno-GSL: <gsl_memory_alloc_pure:2115>: GSL MEM ERROR: kgsl_sharedmem_alloc ioctl failed.

<!-- Finally, paste the output of running `flutter doctor -v` here. -->
[✓] Flutter (Channel dev, v0.10.2, on Linux, locale zh_CN.UTF-8)
    • Flutter version 0.10.2 at /home/xxx/software/flutter
    • Framework revision d8cbb80206 (2 weeks ago), 2018-10-26 01:30:21 -0400
    • Engine revision 6c2ade9fa2
    • Dart version 2.1.0-dev.8.0.flutter-bf26f760b1

[✓] Android toolchain - develop for Android devices (Android SDK 28.0.3)
    • Android SDK at /home/xxx/software/Android/Sdk
    • Android NDK at /home/xxx/software/Android/Sdk/ndk-bundle
    • Platform android-28, build-tools 28.0.3
    • ANDROID_HOME = /home/xxx/software/Android/Sdk
    • Java binary at: /home/xxx/Downloads/android-studio-ide-181.5056338-linux/android-studio/jre/bin/java
    • Java version OpenJDK Runtime Environment (build 1.8.0_152-release-1136-b06)
    • All Android licenses accepted.

[✓] Android Studio (version 3.2)
    • Android Studio at /home/xxx/Downloads/android-studio-ide-181.5056338-linux/android-studio
    • Flutter plugin version 30.0.1
    • Dart plugin version 181.5656
    • Java version OpenJDK Runtime Environment (build 1.8.0_152-release-1136-b06)

[✓] Connected device (1 available)
    • Nexus 6 • ZX1G22LZ39 • android-arm • Android 7.1.1 (API 25)

• No issues found!
@Fengxiaojun47503
Copy link
Author

Fengxiaojun47503 commented Nov 9, 2018

I found that the textureEntry created in PlatformViewsController.java(as below code) never call its release.

@TargetApi(17)
private void createPlatformView(MethodCall call, Result result) {
    ...
    SurfaceTextureEntry textureEntry = this.mTextureRegistry.createSurfaceTexture();
    ...
}

@Fengxiaojun47503 Fengxiaojun47503 changed the title memory leak and screen become black when using platformView Memory leak and screen become black when using PlatformView Nov 9, 2018
@zoechi zoechi added c: performance Relates to speed or footprint issues (see "perf:" labels) a: platform-views Embedding Android/iOS views in Flutter apps labels Nov 9, 2018
@zoechi zoechi added this to the Goals milestone Nov 9, 2018
@zoechi zoechi added the platform-android Android applications specifically label Nov 9, 2018
@jason-simmons
Copy link
Member

@amirh

@amirh
Copy link
Contributor

amirh commented Nov 12, 2018

Thanks for the report! flutter/engine#6829 should fix the texture leak.
I'm leaving this open until we verify that the original black screen issue is resolved.

@Fengxiaojun47503
Copy link
Author

Thanks for the report! flutter/engine#6829 should fix the texture leak.
I'm leaving this open until we verify that the original black screen issue is resolved.

Thanks for your fast fix!
I am sorry about the issue of black screen, because i have found that the black screen issue continue occurring after remove the AndroidView. I think it is another issue.(Maybe the AndroidView just make the black screen issue easier to produce).I am testing how to reproduce the black issue easily,after test finish i will report a new issue.

@ened
Copy link
Contributor

ened commented Nov 14, 2018

Could this be related to #23181 ?

@Fengxiaojun47503
Copy link
Author

Could this be related to #23181 ?

I am afraid it is different from #23181, because i didn't open the 'don't keep activities' develop settings.

@Fengxiaojun47503
Copy link
Author

Fengxiaojun47503 commented Nov 20, 2018

NativeViewExample.zip

I have applied the fix by update flutter to 0.11.7 • channel dev, and i reproduced the black screen issue again.
I uploaded my test source in NativeViewExample.zip.

Steps to Reproduce

Fast scroll up/down the listview that contain AndroidView repeatedly.

## Logs[test Device Info: Nexus 6 • ZX1G22LZ39 • android-arm • Android 7.1.1 (API 25)]
2018-11-20 17:24:58.001 1943-1957/com.example.nativeviewexamplepluginexample E/BufferQueueProducer: [SurfaceTexture-0-1943-1] dequeueBuffer: BufferQueue has been abandoned
2018-11-20 17:24:58.278 1943-1976/com.example.nativeviewexamplepluginexample E/BufferQueueProducer: [SurfaceTexture-0-1943-2] dequeueBuffer: BufferQueue has been abandoned
2018-11-20 17:25:01.533 1943-1976/com.example.nativeviewexamplepluginexample E/BufferQueueProducer: [SurfaceTexture-0-1943-12] dequeueBuffer: BufferQueue has been abandoned
2018-11-20 17:25:01.977 1943-2004/com.example.nativeviewexamplepluginexample E/BufferQueueProducer: [SurfaceTexture-0-1943-14] queueBuffer: BufferQueue has been abandoned
2018-11-20 17:25:02.711 1943-2007/com.example.nativeviewexamplepluginexample E/BufferQueueProducer: [SurfaceTexture-0-1943-17] queueBuffer: BufferQueue has been abandoned
2018-11-20 17:25:03.176 1943-2004/com.example.nativeviewexamplepluginexample E/BufferQueueProducer: [SurfaceTexture-0-1943-18] dequeueBuffer: BufferQueue has been abandoned
2018-11-20 17:25:03.580 1943-1956/com.example.nativeviewexamplepluginexample E/BufferQueueProducer: [SurfaceTexture-0-1943-20] queueBuffer: BufferQueue has been abandoned
2018-11-20 17:25:04.395 1943-1957/com.example.nativeviewexamplepluginexample E/BufferQueueProducer: [SurfaceTexture-0-1943-21] dequeueBuffer: BufferQueue has been abandoned
2018-11-20 17:25:04.814 1943-2015/com.example.nativeviewexamplepluginexample E/BufferQueueProducer: [SurfaceTexture-0-1943-24] dequeueBuffer: BufferQueue has been abandoned
2018-11-20 17:25:05.624 1943-2020/com.example.nativeviewexamplepluginexample E/BufferQueueProducer: [SurfaceTexture-0-1943-28] dequeueBuffer: BufferQueue has been abandoned
2018-11-20 17:25:05.700 1943-2013/com.example.nativeviewexamplepluginexample E/BufferQueueProducer: [SurfaceTexture-0-1943-29] queueBuffer: BufferQueue has been abandoned
2018-11-20 17:25:05.860 1943-1976/com.example.nativeviewexamplepluginexample E/BufferQueueProducer: [SurfaceTexture-0-1943-30] dequeueBuffer: BufferQueue has been abandoned
2018-11-20 17:25:05.968 1943-2015/com.example.nativeviewexamplepluginexample E/BufferQueueProducer: [SurfaceTexture-0-1943-31] cancelBuffer: BufferQueue has been abandoned
......
2018-11-20 17:28:14.482 1943-1961/com.example.nativeviewexamplepluginexample E/Adreno-GSL: <gsl_memory_alloc_pure:2115>: GSL MEM ERROR: kgsl_sharedmem_alloc ioctl failed.
2018-11-20 17:28:14.484 1943-1961/com.example.nativeviewexamplepluginexample W/Adreno-GSL: <sharedmem_gpumem_alloc_id:2372>: sharedmem_gpumem_alloc: mmap failed errno 12 Out of memory
2018-11-20 17:28:14.485 1943-1961/com.example.nativeviewexamplepluginexample E/Adreno-GSL: <gsl_memory_alloc_pure:2115>: GSL MEM ERROR: kgsl_sharedmem_alloc ioctl failed.
2018-11-20 17:28:14.555 1943-1962/com.example.nativeviewexamplepluginexample W/Adreno-GSL: <sharedmem_gpumem_alloc_id:2372>: sharedmem_gpumem_alloc: mmap failed errno 12 Out of memory
2018-11-20 17:28:14.557 1943-1962/com.example.nativeviewexamplepluginexample E/Adreno-GSL: <gsl_memory_alloc_pure:2115>: GSL MEM ERROR: kgsl_sharedmem_alloc ioctl failed.
2018-11-20 17:28:14.560 1943-1962/com.example.nativeviewexamplepluginexample W/Adreno-GSL: <sharedmem_gpumem_alloc_id:2372>: sharedmem_gpumem_alloc: mmap failed errno 12 Out of memory
2018-11-20 17:28:14.562 1943-1962/com.example.nativeviewexamplepluginexample E/Adreno-GSL: <gsl_memory_alloc_pure:2115>: GSL MEM ERROR: kgsl_sharedmem_alloc ioctl failed.
2018-11-20 17:28:14.565 1943-1962/com.example.nativeviewexamplepluginexample W/Adreno-GSL: <sharedmem_gpumem_alloc_id:2372>: sharedmem_gpumem_alloc: mmap failed errno 12 Out of memory
2018-11-20 17:28:14.566 1943-1962/com.example.nativeviewexamplepluginexample E/Adreno-GSL: <gsl_memory_alloc_pure:2115>: GSL MEM ERROR: kgsl_sharedmem_alloc ioctl failed.

And i found that different device have different phenomenon.

*On SM G9008V • f8a396e5 • android-arm • Android 6.0 (API 23):app crash and exit.
*On Nexus 6 • ZX1G22LZ39 • android-arm • Android 7.1.1 (API 25):screen become black
*On MIX 2 • 3c6e76dd • android-arm64 • Android 8.0.0 (API 26):screen freeze and can not scroll the Listview any more.

@zoechi zoechi added the engine flutter/engine repository. See also e: labels. label Dec 13, 2018
@amirh
Copy link
Contributor

amirh commented Feb 14, 2019

I was able to consistently reproduce the issue with the following piece of code:

class WebViewCyclerState extends State<WebViewCycler> {

  int cycles = 1;
  bool running = false;
  Timer timer;

  @override
  Widget build(BuildContext context) {
    return Column(
      children: <Widget>[
        Text('Cycled through ${(cycles / 2.0).ceil()} webviews'),
        Container(
          height: 300,
          child: cycles % 2 == 1 ? WebView(initialUrl: 'https://flutter.io',) : null,
        ),
        RaisedButton(
          child: Text(running ? 'STOP' : 'RUN'),
          onPressed: () {
            if (running) {
              timer.cancel();
              setState(() {
                running = false;
              });
              return;
            }
            setState(() {
              running = true;
            });
            timer = Timer.periodic(Duration(milliseconds: 250), (Timer t) {
              setState(() {
                cycles++;
              });
            });
          },
        )
      ],
    );
  }

}

Sent a fix in: flutter/plugins#1221

@amirh amirh modified the milestones: Goals, March 2019 Feb 14, 2019
@amirh amirh self-assigned this Feb 14, 2019
amirh added a commit to flutter/plugins that referenced this issue Feb 14, 2019
)

The reference from the method channel to the webview/maps instance was never cleared on Android, which leaked memory as even after disposing the platform view the view was not released.

[flutter/flutter#24145](flutter/flutter#24145)
@amirh
Copy link
Contributor

amirh commented Feb 15, 2019

With flutter/plugins#1221 this is much better, and while I couldn't get it to OOM after that patch, it still looks like there's a slow growth in graphics memory usage, so there might be another leak somewhere. I'm investigating.

@amirh
Copy link
Contributor

amirh commented Feb 15, 2019

After some more digging, it looks like the leak is not platform view specific but happens whenever we recycle textures (I've observed the leak when doing something similar to the snippet above with the video player).

My hypothesis right now is that we are never deleting the textures we allocate at: https://github.com/flutter/engine/blob/733739938bb4f841320e12e32a52c3ba7d69558c/shell/platform/android/android_external_texture_gl.cc#L36

Unfortunately I cannot confirm right now(looks like one of the gclient sync dependencies is super slow right now, and my laptop is not up to date), but my speculative fix would be:
amirh/engine@e72bdf8

I'll follow up tomorrow morning, confirm the fix and if it works submit it.

cc @chinmaygarde

amirh added a commit to amirh/engine that referenced this issue Feb 15, 2019
On Android we were never deleting the textures allocated for
the texture registry, which resulted in a memory leak, see:
flutter/flutter#24145
@amirh
Copy link
Contributor

amirh commented Feb 15, 2019

Ok confirmed that with the proposed fix the memory footprint is no longer growing when cycling through textures.
Sent flutter/engine#7836

amirh added a commit to flutter/engine that referenced this issue Feb 15, 2019
…7836)

On Android we were never deleting the textures allocated for
the texture registry, which resulted in a memory leak, see:
flutter/flutter#24145
@amirh amirh closed this as completed Feb 15, 2019
andreidiaconu pushed a commit to andreidiaconu/plugins that referenced this issue Feb 17, 2019
…utter#1221)

The reference from the method channel to the webview/maps instance was never cleared on Android, which leaked memory as even after disposing the platform view the view was not released.

[flutter/flutter#24145](flutter/flutter#24145)
karantanwar pushed a commit to karantanwar/plugins that referenced this issue Feb 19, 2019
…utter#1221)

The reference from the method channel to the webview/maps instance was never cleared on Android, which leaked memory as even after disposing the platform view the view was not released.

[flutter/flutter#24145](flutter/flutter#24145)
karantanwar pushed a commit to karantanwar/plugins that referenced this issue Feb 19, 2019
…utter#1221)

The reference from the method channel to the webview/maps instance was never cleared on Android, which leaked memory as even after disposing the platform view the view was not released.

[flutter/flutter#24145](flutter/flutter#24145)
@github-actions
Copy link

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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 31, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
a: platform-views Embedding Android/iOS views in Flutter apps c: performance Relates to speed or footprint issues (see "perf:" labels) customer: dream (g3) engine flutter/engine repository. See also e: labels. platform-android Android applications specifically
Projects
None yet
Development

No branches or pull requests

5 participants