问题描述
我有一个带有WebView的应用程序。当活动暂停时(onPause),我调用适当的webview.onPause和webview.stopTimers,它们与Activity.onResume方法中的webview.onResume和webview.resumeTimers保持平衡。
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.
现在,当我在网络视图中加载www.google.com,然后将应用程序置于后台时,它仍将使用高达30%的CPU!这太多了。
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.
我执行了方法剖析,发现WebViewCoreThread每隔100毫秒就用MessageQueue.nativePollOnce-> WebViewCore.nativeSetScrollOffset做某事,这引起了对
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).
这是WebView的一般问题还是我的应用程序中的问题?
Is this a problem with WebView in general or a problem in my app?
推荐答案
Android的Web视图存在很多问题,特定版本存在一些已知问题,如您在此处看到的:
Android has a lot of issues with the webviews, and there are some known issues with specific versions as you can see here:
我遇到了一些类似的问题,最后销毁Webview并重新创建它们更便宜,因为所有的CPU消耗会耗尽用户的电池。
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.
如果您想保留用户的电池并避免卸载,我建议做到这一点,看来Android团队正在努力改善Webview,让我们看看。
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.
[在用户:@CamHart发现断开的链接后进行更新]
我在Stackoverflow中发现了类似的问题,有多个响应:
我认为很多人将内存泄漏一词与CPU泄漏相混淆,根本原因可能是相同的,如果没有,Webview不会被破坏。一个持续运行JavaScript的WebView(例如,Ads),即使退出应用程序后,CPU消耗也不会降低,并且内存可能保持不变甚至下降,但如果WebView被正确销毁,内存将无法达到应有的水平。
I found a similar question in Stackoverflow with multiple responses:Memory leak in WebViewI 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.
这篇关于WebView在WebViewCoreThread中使用30%的后台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!