iOS WKWebView白屏(卡頓)

通過騰訊bugly卡頓日志發現,有許多WebKit的卡頓信息,但是卻沒有定位到項目中的具體代碼。通過大量觀察發現很多日志中都有調用WebKit::WebPageProxy::processDidTerminate函數或WebKit::WebMemoryPressureHandler::WebMemoryPressureHandler()函數。參考WKWebView代理函數中的- (void)webViewWebContentProcessDidTerminate:(WKWebView *)webView;函數成因及含有*Memory*字段,因此大膽猜測可能是WKWebView內存占用過大,內存不足引起。

WKWebView 那些坑》文章中WKWebView白屏問題小節介紹到在WKWebView上當總體的內存占用比較大的時候,webContent process會crash,從而出現白屏現象。此時系統會調用- (void)webViewWebContentProcessDidTerminate:(WKWebView *)webView;函數,在該函數里執行[webView reload] (這個時候webView.URL取值尚不為nil)解決白屏問題。

兩者成因皆為WKWebView內存占用過大引起,故本次卡頓解決方式參考WKWebView白屏問題解決方法。

@property (nonatomic ,assign) BOOL webViewLoadTitle;//是否已獲取當前webView的title

#pragma mark -

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    if (YES == self.webViewLoadTitle && [NSString isNullDataString:self.webView.title]) {
        //webView已獲取title,但返回時無法獲取title,可能是內存不足原因造成,此時需重新加載同時避免第一次進入時重新加載
        [self.webView reload];
    }
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    if (![NSString isNullDataString:self.webView.title]) {
        self.webViewLoadTitle = YES;
    }
}

#pragma mark - WKNavigationDelegate

//web內存過大,進程終止,重新加載webView
- (void)webViewWebContentProcessDidTerminate:(WKWebView *)webView {
    [webView reload];
}

結果:經生產版本檢驗,可有效防止WKWebView內存占用過大引起的卡頓問題。

卡頓日志參考

0 WebKit    WebKit::SandboxExtension::HandleArray::HandleArray()
1 WebKit    WebKit::WebProcessCreationParameters::WebProcessCreationParameters() + 36
2 WebKit    WebKit::WebProcessPool::initializeNewWebProcess(WebKit::WebProcessProxy&, WebKit::WebsiteDataStore&) + 156
3 WebKit    WebKit::WebProcessPool::createNewWebProcess(WebKit::WebsiteDataStore&, WebKit::WebProcessProxy::IsInPrewarmedPool) + 60
4 WebKit    WebKit::WebProcessPool::createNewWebProcessRespectingProcessCountLimit(WebKit::WebsiteDataStore&) + 232
5 WebKit    WebKit::WebPageProxy::reattachToWebProcess() + 36
6 WebKit    WebKit::WebPageProxy::reattachToWebProcessForReload() + 52
7 WebKit    WebKit::WebPageProxy::reload(WTF::OptionSet<WebCore::ReloadOption>) + 268
8 WebKit    WebKit::WebPageProxy::tryReloadAfterProcessTermination() + 308
9 WebKit    WebKit::WebPageProxy::processDidTerminate(WebKit::ProcessTerminationReason) + 644
10 WebKit   WebKit::WebProcessProxy::processDidTerminateOrFailedToLaunch() + 736
11 WebKit   WebKit::WebProcessProxy::didClose(IPC::Connection&) + 160
12 JavaScriptCore   WTF::RunLoop::performWork() + 276
13 JavaScriptCore   WTF::RunLoop::performWork(void*) + 36
14 CoreFoundation   ___CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 24
0 libobjc.A.dylib   class_createInstance + 36
1 libobjc.A.dylib   _objc_rootAlloc + 52
2 libobjc.A.dylib   _objc_rootAlloc + 52
3 UIKitCore +[UITextInteraction textInteractionsForSet:] + 36
4 UIKitCore -[UITextInteractionAssistant(UITextInteractionAssistant_Internal) addGestureRecognizersToView:] + 388
5 UIKitCore -[UIWKTextInteractionAssistant addGestureRecognizersToView:] + 68
6 UIKitCore -[UITextInteractionAssistant(UITextInteractionAssistant_Internal) setGestureRecognizers] + 204
7 UIKitCore -[UITextInteractionAssistant(UITextInteractionAssistant_Internal) initWithView:textInteractionSet:] + 124
8 UIKitCore -[UIWKTextInteractionAssistant initWithView:] + 44
9 WebKit    -[WKContentView(WKInteraction) setUpTextSelectionAssistant] + 92
10 WebKit   -[WKContentView(WKInteraction) setupInteraction] + 1356
11 WebKit   WebKit::PageClientImpl::didRelaunchProcess() + 36
12 WebKit   WebKit::WebPageProxy::reattachToWebProcess(WTF::Ref<WebKit::WebProcessProxy, WTF::DumbPtrTraits<WebKit::WebProcessProxy> >&&, API::Navigation*, WebKit::WebPageProxy::ReattachForBackForward) + 912
13 WebKit   WebKit::WebPageProxy::reattachToWebProcess() + 80
14 WebKit   WebKit::WebPageProxy::reattachToWebProcessForReload() + 52
15 WebKit   WebKit::WebPageProxy::reload(WTF::OptionSet<WebCore::ReloadOption>) + 268
16 WebKit   WebKit::WebPageProxy::tryReloadAfterProcessTermination() + 308
17 WebKit   WebKit::WebPageProxy::processDidTerminate(WebKit::ProcessTerminationReason) + 644
18 WebKit   WebKit::WebProcessProxy::processDidTerminateOrFailedToLaunch() + 736
19 WebKit   WebKit::WebProcessProxy::didClose(IPC::Connection&) + 160
20 JavaScriptCore   WTF::RunLoop::performWork() + 276
21 JavaScriptCore   WTF::RunLoop::performWork(void*) + 36
22 CoreFoundation   ___CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 24
0 WebKit    0x00000001c05f4000 + 4428800
1 WebKit    WebKit::ViewSnapshotStore::singleton() + 52
2 WebKit    _ZN3WTF8FunctionIFvNS_8CriticalENS_11SynchronousEEE15CallableWrapperIZN6WebKit28installMemoryPressureHandlerEvE3$_0E4callES1_S2_ + 24
3 JavaScriptCore    WTF::MemoryPressureHandler::releaseMemory(WTF::Critical, WTF::Synchronous) + 92
4 JavaScriptCore    ___ZN3WTF21MemoryPressureHandler7installEv_block_invoke_2 + 140
0 libsystem_kernel.dylib    kevent_id + 8
1 libdispatch.dylib __dispatch_kq_poll + 332
2 libdispatch.dylib __dispatch_event_loop_wait_for_ownership$VARIANT$mp + 412
3 libdispatch.dylib ___DISPATCH_WAIT_FOR_QUEUE__ + 296
4 libdispatch.dylib __dispatch_sync_f_slow + 140
5 AssertionServices -[BKSAssertion setInvalidationHandler:] + 144
6 WebKit    WebKit::ProcessAssertion::ProcessAssertion(int, WebKit::AssertionState, WTF::Function<void ()>&&) + 472
7 WebKit    WebKit::ProcessAndUIAssertion::ProcessAndUIAssertion(int, WebKit::AssertionState) + 96
8 WebKit    WebKit::ProcessThrottler::didConnectToProcess(int) + 224
9 WebKit    WebKit::WebProcessProxy::didFinishLaunching(WebKit::ProcessLauncher*, IPC::Connection::Identifier) + 576
10 WebKit   WebKit::ProcessLauncher::didFinishLaunchingProcess(int, IPC::Connection::Identifier) + 124
11 WebKit   ___ZN6WebKit15ProcessLauncher13launchProcessEv_block_invoke + 136
12 libxpc.dylib __xpc_connection_reply_callout + 60
0 WebKit    -[WKContentView(WKInteraction) isEditable]
1 UIKitCore -[UITextInteractionAssistant(UITextInteractionAssistant_Internal) addGestureRecognizersToView:] + 120
2 UIKitCore -[UIWKTextInteractionAssistant addGestureRecognizersToView:] + 68
3 UIKitCore -[UITextInteractionAssistant(UITextInteractionAssistant_Internal) setGestureRecognizers] + 200
4 UIKitCore -[UITextInteractionAssistant(UITextInteractionAssistant_Internal) initWithView:textInteractionSet:] + 124
5 UIKitCore -[UIWKTextInteractionAssistant initWithView:] + 44
6 WebKit    -[WKContentView(WKInteraction) setUpTextSelectionAssistant] + 92
7 WebKit    -[WKContentView(WKInteraction) setupInteraction] + 1636
8 WebKit    WebKit::PageClientImpl::didRelaunchProcess() + 36
9 WebKit    WebKit::WebPageProxy::finishAttachingToWebProcess(WebKit::WebPageProxy::ShouldInitializeWebPage) + 460
10 WebKit   WebKit::WebPageProxy::reattachToWebProcess() + 388
11 WebKit   WebKit::WebPageProxy::reattachToWebProcessForReload() + 348
12 WebKit   WebKit::WebPageProxy::reload(WTF::OptionSet<WebCore::ReloadOption>) + 436
13 WebKit   WebKit::WebPageProxy::tryReloadAfterProcessTermination() + 272
14 WebKit   WebKit::WebPageProxy::dispatchProcessDidTerminate(WebKit::ProcessTerminationReason) + 268
15 WebKit   WebKit::WebPageProxy::processDidTerminate(WebKit::ProcessTerminationReason) + 684
16 WebKit   WebKit::WebProcessProxy::processDidTerminateOrFailedToLaunch() + 916
17 WebKit   WebKit::WebProcessProxy::didClose(IPC::Connection&) + 140
18 JavaScriptCore   WTF::RunLoop::performWork() + 340
19 JavaScriptCore   WTF::RunLoop::performWork(void*) + 36
20 CoreFoundation   0x000000023a172000 + 700188
0 WebKit    API::Navigation::Navigation(WebKit::WebNavigationState&)
1 WebKit    WebKit::WebNavigationState::createReloadNavigation() + 44
2 WebKit    WebKit::WebPageProxy::reattachToWebProcessForReload() + 372
3 WebKit    WebKit::WebPageProxy::reload(WTF::OptionSet<WebCore::ReloadOption>) + 436
4 WebKit    WebKit::WebPageProxy::tryReloadAfterProcessTermination() + 272
5 WebKit    WebKit::WebPageProxy::dispatchProcessDidTerminate(WebKit::ProcessTerminationReason) + 268
6 WebKit    WebKit::WebPageProxy::processDidTerminate(WebKit::ProcessTerminationReason) + 684
7 WebKit    WebKit::WebProcessProxy::processDidTerminateOrFailedToLaunch() + 916
8 WebKit    WebKit::WebProcessProxy::didClose(IPC::Connection&) + 140
9 JavaScriptCore    WTF::RunLoop::performWork() + 276
10 JavaScriptCore   WTF::RunLoop::performWork(void*) + 36
0 WebKit    WTF::Mapper<WTF::Vector<WTF::String, 0ul, WTF::CrashOnOverflow, 16ul> WTF::copyToVectorOf<WTF::String, WTF::HashSet<WTF::String, WTF::StringHash, WTF::HashTraits<WTF::String> > >(WTF::HashSet<WTF::String, WTF::StringHash, WTF::HashTraits<WTF::String> > const&)::'lambda'(WTF::String const&), WTF::HashSet<WTF::String, WTF::StringHash, WTF::HashTraits<WTF::String> > const&, void>::map(WTF::HashSet<WTF::String, WTF::StringHash, WTF::HashTraits<WTF::String> > const&, WTF::Vector<WTF::String, 0ul, WTF::CrashOnOverflow, 16ul> WTF::copyToVectorOf<WTF::String, WTF::HashSet<WTF::String, WTF::StringHash, WTF::HashTraits<WTF::String> > >(WTF::HashSet<WTF::String, WTF::StringHash, WTF::HashTraits<WTF::String> > const&)::'lambda'(WTF::String const&) const&)
1 WebKit    WebKit::WebProcessPool::initializeNewWebProcess(WebKit::WebProcessProxy&, WebKit::WebsiteDataStore&) + 836
2 WebKit    WebKit::WebProcessPool::createNewWebProcess(WebKit::WebsiteDataStore&, WebKit::WebProcessProxy::IsPrewarmed) + 84
3 WebKit    WebKit::WebProcessPool::createNewWebProcessRespectingProcessCountLimit(WebKit::WebsiteDataStore&) + 228
4 WebKit    WebKit::WebPageProxy::reattachToWebProcess() + 252
5 WebKit    WebKit::WebPageProxy::reattachToWebProcessForReload() + 348
6 WebKit    WebKit::WebPageProxy::reload(WTF::OptionSet<WebCore::ReloadOption>) + 436
7 WebKit    WebKit::WebPageProxy::tryReloadAfterProcessTermination() + 272
8 WebKit    WebKit::WebPageProxy::dispatchProcessDidTerminate(WebKit::ProcessTerminationReason) + 268
9 WebKit    WebKit::WebPageProxy::processDidTerminate(WebKit::ProcessTerminationReason) + 684
10 WebKit   WebKit::WebProcessProxy::processDidTerminateOrFailedToLaunch() + 916
11 WebKit   WebKit::WebProcessProxy::didClose(IPC::Connection&) + 140
12 JavaScriptCore   WTF::RunLoop::performWork() + 340
13 JavaScriptCore   WTF::RunLoop::performWork(void*) + 36
0 WebKit    WebKit::ViewSnapshotStore::singleton()
1 WebKit    invocation function for block in WebKit::WebMemoryPressureHandler::WebMemoryPressureHandler() + 12
2 libdispatch.dylib __dispatch_client_callout + 16
0 WebKit    WebKit::SecItemShimProxy::singleton()
1 WebKit    WebKit::WebProcessProxy::connectionWillOpen(IPC::Connection&) + 48
2 WebKit    WebKit::ChildProcessProxy::didFinishLaunching(WebKit::ProcessLauncher*, IPC::Connection::Identifier) + 196
3 WebKit    WebKit::WebProcessProxy::didFinishLaunching(WebKit::ProcessLauncher*, IPC::Connection::Identifier) + 96
4 WebKit    WebKit::ProcessLauncher::didFinishLaunchingProcess(int, IPC::Connection::Identifier) + 124
5 WebKit    ___ZN6WebKit15ProcessLauncher13launchProcessEv_block_invoke + 136
6 libxpc.dylib  __xpc_connection_reply_callout + 60
0 WebCore   WebCore::HTTPHeaderMap::HTTPHeaderMap() + 0
1 WebKit    WebCore::ResourceRequestBase::ResourceRequestBase(WTF::URL const&, WebCore::ResourceRequestCachePolicy) + 152
2 WebKit    WebCore::ResourceRequest::ResourceRequest() + 48
3 WebKit    API::Navigation::Navigation(WebKit::WebNavigationState&) + 64
4 WebKit    WebKit::WebNavigationState::createReloadNavigation() + 44
5 WebKit    WebKit::WebPageProxy::reattachToWebProcessForReload() + 372
6 WebKit    WebKit::WebPageProxy::reload(WTF::OptionSet<WebCore::ReloadOption>) + 436
7 WebKit    WebKit::WebPageProxy::tryReloadAfterProcessTermination() + 272
8 WebKit    WebKit::WebPageProxy::dispatchProcessDidTerminate(WebKit::ProcessTerminationReason) + 268
9 WebKit    WebKit::WebPageProxy::processDidTerminate(WebKit::ProcessTerminationReason) + 684
10 WebKit   WebKit::WebProcessProxy::processDidTerminateOrFailedToLaunch() + 916
11 WebKit   WebKit::WebProcessProxy::didClose(IPC::Connection&) + 140
12 JavaScriptCore   WTF::RunLoop::performWork() + 340
13 JavaScriptCore   WTF::RunLoop::performWork(void*) + 36
14 CoreFoundation   0x00000001a728a000 + 700188
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 230,622評論 6 544
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 99,716評論 3 429
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 178,746評論 0 383
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,991評論 1 318
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 72,706評論 6 413
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 56,036評論 1 329
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 44,029評論 3 450
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 43,203評論 0 290
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 49,725評論 1 336
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 41,451評論 3 361
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 43,677評論 1 374
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 39,161評論 5 365
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,857評論 3 351
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 35,266評論 0 28
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 36,606評論 1 295
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 52,407評論 3 400
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 48,643評論 2 380

推薦閱讀更多精彩內容

  • 原文 導語 WKWebView 是蘋果在 WWDC 2014 上推出的新一代 webView 組件,用以替代 UI...
    MR_詹閱讀 5,255評論 0 9
  • 1、WKWebView 白屏問題WKWebView 自詡擁有更快的加載速度,更低的內存占用,但實際上 WKWebV...
    無名感恩閱讀 2,155評論 0 3
  • WKWebView 是蘋果在 WWDC 2014 上推出的新一代 webView 組件,用以替代 UIKit 中笨...
    Aiana閱讀 4,621評論 1 8
  • 轉載:http://www.cnblogs.com/NSong/p/6489802.html 導語 WKWebVi...
    李小威閱讀 4,875評論 8 9
  • 文|零度橘子 曾幾何時 我在河的這頭 你在河的那頭 中間隔著一條長長的河 夏天常遇暴雨,洪水圍困 冬天水流依舊,冰...
    零度橘子閱讀 745評論 5 34