WebView 在 WebViewCoreThread 的后台使用 30%
我有一个带有 WebView 的应用程序。当 Activity 暂停(onPause)时,我调用适当的 webview.onPause 和 webview.stopTimers,它们与 Activity.onResume 方法中的 webview.onResume 和 webview.resumeTimers 保持平衡。
现在,当我在 webview 中加载 www.google.com,然后将应用程序置于后台时,它仍然会使用高达 30% 的 CPU!这实在是太多了。
我执行了方法分析,发现 WebViewCoreThread 每 100 毫秒就使用 MessageQueue.nativePollOnce 执行一次操作 -> WebViewCore.nativeSetScrollOffset,这导致了 webview 的重新绘制(在 bakground 中)(WebViewCore.nativeDrawContent)。
这是一般 WebView 的问题还是我的应用程序的问题?
I have an app with a WebView. When the Activity pauses (onPause) i call the appropiate webview.onPause and webview.stopTimers, these are balanced with the webview.onResume and webview.resumeTimers in the Activity.onResume method.
Now when i load www.google.com in the webview, and then put the app in the background, it will still use up to 30% CPU! This is much too much.
I performed method profiling and saw that the WebViewCoreThread was doing every 100ms something with MessageQueue.nativePollOnce -> WebViewCore.nativeSetScrollOffset, this caused a repaint of the webview (while in the bakground) (WebViewCore.nativeDrawContent).
Is this a problem with WebView in general or a problem in my app?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Android 的 webview 存在很多问题,并且特定版本存在一些已知问题,如下所示:
https://code.google.com/p/android/issues/detail?id=9375
我遇到了一些类似的问题,最后销毁网络视图更便宜和再次重新创建它们,因为所有 CPU 消耗都会耗尽用户的电池。
如果你想保护用户的电池,并避免卸载,我建议你这样做,Android 团队似乎正在努力改进 Webviews,让我们看看。
[更新,用户后:@CamHart 发现了一个损坏的链接]
我在 Stackoverflow 中发现了一个类似的问题,有多个回复:
WebView 中的内存泄漏
我想很多人都把内存泄漏和CPU泄漏混淆了,根本原因可能是一样的,Webview没有被破坏,如果你有一个不断运行JavaScript的WebView(例如广告),CPU消耗就不会即使退出应用程序后,内存也可能会保持不变甚至下降,但如果 WebView 被正确销毁,则不会到达应有的位置。
Android has a lot of issues with the webviews, and there are some known issues with specific versions as you can see here:
https://code.google.com/p/android/issues/detail?id=9375
I was facing some similar issues and at the end it's cheaper to destroy the webviews and recreate them again, because all the CPU consumption will drain the battery of the user.
If you want to preserve the battery of your users, and avoid uninstalls I'll suggest to do that, it seems that the Android team is working hard to improve the Webviews, let's see.
[Update, after user: @CamHart found a broken link]
I found a similar question in Stackoverflow with multiple responses:
Memory leak in WebView
I think a lot of people are confusing the term Memory Leak with CPU leak, the root cause could be the same, the Webview is not destroyed, if you have a WebView that runs JavaScript constantly (Ads for example), the CPU consumption will not go down even after exiting the application, and the memory might remain the same or even drop but not go to where it should be if the WebView was destroyed correctly.