为了解决模拟器中 Android Webview 页面崩溃的问题

 

这个文章最后也没找到解决办法,换个Genymotion模拟器就好了

错误日志:

google-breakpad:## ## ##

google-breakpad:chrome build fingerprint:

google-breakpad:163.1.0.132

google-breakpad:## ## ##

一个在布局文件中加载Webview控件的简单页面,在多次加载url后闪退,错误日志就是以上。

网上百度到的资料很少,进stackoverflow很慢,后面换到bing的国际版就快很多了,百度的资料眼花缭乱,没啥用

https://cn.bing.com

bing上找一些不常见的问题针对性还是强些。

找到一篇关于这个问题的

https://stackoverflow.com/questions/36329467/android-chromium-webview-crash

有一条回答

This exception happens in the native code, there isn't much you can do in your Java code about it. Seems to be a graphic memory exhaustion, as it is crashing in malloc called from the GL library code. You say that the WebView is never destroyed, perhaps this is a result of memory fragmentation (the graphics code allocates / frees memory, and finally the memory map becomes so fragmented, that it's not possible to find a contiguous block of the requested size). I would suggest considering destroying and recreating WebView from time to time.

这种异常发生在本机代码中,在Java代码中对此无能为力。似乎是图形内存耗尽,因为它在从GL库代码调用的malloc中崩溃。您说WebView永远不会被破坏,这可能是内存碎片的结果(图形代码分配/释放内存,最后内存映射变得如此碎片化,以至于不可能找到具有请求大小的连续块)。我建议不时地考虑销毁和重新创建WebView。

Because Chrome usually runs GPU-related code in a separate process. So when it crashes, the main Chrome app process is not affected and continues to run. But in WebView everything is within a single app process, so crash in one component brings down the whole app.

因为Chrome通常在一个单独的进程中运行与gpu相关的代码。因此,当它崩溃时,主Chrome应用程序进程不受影响,并继续运行。但在WebView中,所有东西都在一个应用程序进程中,因此在一个组件中崩溃会导致整个应用程序崩溃。

发现了问题所在,从webview的内存占用和释放切入

将webview的创建放在activity中用new WebView(getApplicationContext())的方式,

并且修改webview的销毁

if( mWebView!=null) {
   mWebView.removeAllViews();
   mWebView.destroy();
}

@Override
protected void onDestroy() {
    if( mWebView!=null) {

    // 如果先调用destroy()方法,则会命中if (isDestroyed()) return;这一行代码,需要先onDetachedFromWindow(),再
    // destory()
    ViewParent parent = mWebView.getParent();
    if (parent != null) {
        ((ViewGroup) parent).removeView(mWebView);
    }

    mWebView.stopLoading();
    // 退出时调用此方法,移除绑定的服务,否则某些特定系统会报错
    mWebView.getSettings().setJavaScriptEnabled(false);
    mWebView.clearHistory();
    mWebView.clearView();
    mWebView.removeAllViews();
    mWebView.destroy();

}
super.on Destroy();

}

以上内存的销毁参照简书博主写的https://www.jianshu.com/p/3e8f7dbb0dc7

在定时器中加入了下面这个内存检测,当超出这个内存的时候做一些操作,避免crash 并且在定时器中加入内存检测

/**
 * 检查当前内存,占用量超过(mKeyMemory*60)%,返回错误。
 */
public boolean checkMemory() {
    ActivityManager mSystemService = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
    ActivityManager.MemoryInfo mMemoryInfo = new ActivityManager.MemoryInfo();
    mSystemService.getMemoryInfo(mMemoryInfo);
    long availMem = mMemoryInfo.availMem;
    long totalMem = mMemoryInfo.totalMem;
    float result = (float) (availMem * 1.0 / totalMem);
    Log.e("MoreTasks", " there is percent memory now is " + result);
    if (result >= 0.6) {
        return false;
    }
    return true;
}

加上上面这些确实运行时间变长了,但是最后还是会出现文初的错误,继续找问题,又发现一个方案

https://forums.xamarin.com/discussion/83423/webview-on-android-7-0-crashes

中间有一个提到了

Switched the CPU/ABI in the AVD from "Intel Atom (x86_64)" to "Google APIs Intel Atom (x86)" and it works fine now.

将AVD中的CPU/ABI从“Intel Atom (x86_64)”切换到“谷歌api Intel Atom (x86)”后正常工作。

其实就是将模拟器使用intel硬件加速,类似仿真机加速, 但是找到avd的设置页面时并没有看到CPU/ABI的设置

后来又找资料发现需要安装一些插件,具体操作如下

https://blog.csdn.net/a87b01c14/article/details/49120567

进行第一步的时候就出现新问题了报错信息

This computer does not support Intel Virtualization Technology (VT-x) or it is being exclusively used by Hyper-V. HAXM cannot be installed

这里也有一个平时需要注意的地方,VT-X虚拟服务和Hyper-V是冲突的,他们的服务对象不同,个人感觉Hyper-V一般给VMware等广义后台虚拟机服务,而VT-x一般就是手机模拟器的这一块用到了

信息后面还有一截的,就是在安装intel x86 Emulator Accelerator时提示的,明明本机开了VT-x服务的,Hyper-V也在Windows功能里面关掉了,找了好久,发现我的windows 10中Windows功能下面还有一个Windows 虚拟机监控管理平台被勾选了,去掉这个勾选重启,接下来的安装就ok了。

然而还是没有上面设置CPU/ABI的页面,以上的这些并没有很好的解决文初的问题,但是确实稳定了很多。

没办法,只能想其他办法,在上面调虚拟机的时候把Genymotion原来打不开的问题解决了,因为原来电脑装的docker的配置冲突了(弄这个的时候我也把docker卸载了),发现用Genymotion的模拟器后没有出现文初的这个错误了,也许是android studio自带的模拟器的配置问题,恩,收工。

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值