1. 程式人生 > >解決retrofit OKhttp建立大量對外連線時記憶體溢位

解決retrofit OKhttp建立大量對外連線時記憶體溢位

這個問題是這樣發生的,我的表中有一批資料,量級較大,數百萬個,它們有個地址Address欄位,標明瞭地理位置。我需要對這一批資料根據地址去百度或者高德地圖去查詢經緯度,並且儲存下來。 原本是直接分頁讀取該表,每次讀取幾百條,然後一條一條去獲取經緯度並且儲存。後來發現實在太慢,一秒也就能處理個三五條。所以開啟了多執行緒,大約30個執行緒,每個執行緒處理不同id範圍的資料。 此時問題出現了,每個執行緒中都有for迴圈來分頁讀取DB中的地址資料,然後每條資料都要去百度地圖請求一次,網路請求用的是retrofit,retrofit是包裝的OKHttp。

在這裡插入圖片描述

可以看到執行緒數幹到2000多時程式崩了

java.lang.OutOfMemoryError: unable to create new native thread at java.lang.Thread.start0(Native Method) at java.lang.Thread.start(Thread.java:714) at java.util.concurrent.ThreadPoolExecutor.addWorker(ThreadPoolExecutor.java:950) at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1368) at okhttp3.ConnectionPool.put(ConnectionPool.java:153) at okhttp3.OkHttpClient1.

put(OkHttpClient.java:163)atokhttp3.internal.connection.StreamAllocation.findConnection(StreamAllocation.java:201)atokhttp3.internal.connection.StreamAllocation.findHealthyConnection(StreamAllocation.java:121)atokhttp3.internal.connection.StreamAllocation.newStream(StreamAllocation.java:100)atokhttp3.i
nternal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:42)atokhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)atokhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)atokhttp3.internal.cache.CacheInterceptor.intercept(CacheInte
rceptor.java:93)atokhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)atokhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)atokhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)atokhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)atokhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:120)atokhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)atokhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)atcom.mindata.ecserver.global.http.RetrofitServiceBuilder.lambda1.put(OkHttpClient.java:163) at okhttp3.internal.connection.StreamAllocation.findConnection(StreamAllocation.java:201) at okhttp3.internal.connection.StreamAllocation.findHealthyConnection(StreamAllocation.java:121) at okhttp3.internal.connection.StreamAllocation.newStream(StreamAllocation.java:100) at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:42) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67) at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:93) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67) at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92) at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:120) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67) at com.mindata.ecserver.global.http.RetrofitServiceBuilder.lambda