1、volley引發(fā)的NetworkDispatcher
加入8寤啤0悍鳌!抛猖!那句話解決
@Override
public void run() {
Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
Request<?> request;
while (true) {
//注意8窈睢!2浦联四!這是防止內(nèi)存泄露的重點(diǎn)!
request = null;
try {
// Take a request from the queue.
request = mQueue.take();
} catch (InterruptedException e) {
// We may have been interrupted because it was time to quit.
if (mQuit) {
return;
}
continue;
}
try {
request.addMarker("network-queue-take");
// If the request was cancelled already, do not perform the
// network request.
if (request.isCanceled()) {
request.finish("network-discard-cancelled");
continue;
}
// Tag the request (if API >= 14)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
TrafficStats.setThreadStatsTag(request.getTrafficStatsTag());
}
// Perform the network request.
NetworkResponse networkResponse = mNetwork.performRequest(request);
request.addMarker("network-http-complete");
// If the server returned 304 AND we delivered a response already,
// we're done -- don't deliver a second identical response.
if (networkResponse.notModified && request.hasHadResponseDelivered()) {
request.finish("not-modified");
continue;
}
// Parse the response here on the worker thread.
Response<?> response = request.parseNetworkResponse(networkResponse);
request.addMarker("network-parse-complete");
// Write to cache if applicable.
// TODO: Only update cache metadata instead of entire record for 304s.
if (request.shouldCache() && response.cacheEntry != null) {
mCache.put(request.getCacheKey(), response.cacheEntry);
request.addMarker("network-cache-written");
}
// Post the response back.
request.markDelivered();
mDelivery.postResponse(request, response);
} catch (VolleyError volleyError) {
parseAndDeliverNetworkError(request, volleyError);
} catch (Exception e) {
VolleyLog.e(e, "Unhandled exception %s", e.toString());
mDelivery.postError(request, new VolleyError(e));
}
}
}
2撑教、Volley StringRequest等引起的內(nèi)存泄漏
(1)在引用StringRequest的地方給request添加tag
request.setTag(REQUEST_TAG);
(2)在不用的時(shí)候
@Override
protected void onDestroy() {
super.onDestroy();
RequestManager.getRequestQueue().cancelAll(ShortUrlFactory.REQUEST_TAG);
}
3朝墩、EventBus引發(fā)的內(nèi)存泄漏
//取消注冊 , 防止Activity內(nèi)存泄漏
EventBus.getDefault().unregister( this );
4、Webview引發(fā)的內(nèi)存泄漏
(1)//延長webview的生命周期伟姐,防止內(nèi)存泄漏
mContext = this.getContext().getApplicationContext();
(2)在onDestroy時(shí)取消webview的綁定收苏,并銷毀
@Override
protected void onDestroy() {
super.onDestroy();
if (webView != null) {
// 如果先調(diào)用destroy()方法,則會命中if (isDestroyed()) return;
// 這一行代碼愤兵,需要先onDetachedFromWindow()鹿霸,再
// destory()
ViewParent parent = webView.getParent();
if (parent != null) {
((ViewGroup) parent).removeView(webView);
}
webView.stopLoading();
// 退出時(shí)調(diào)用此方法,移除綁定的服務(wù)秆乳,否則某些特定系統(tǒng)會報(bào)錯(cuò)
webView.getSettings().setJavaScriptEnabled(false);
webView.clearHistory();
webView.clearView();
webView.removeAllViews();
webView.destroy();
webView = null;
}
}
5懦鼠、Surface要release
mSurface.release();