關(guān)于HttpClient 引發(fā)的線程太多導(dǎo)致FullGc的問題
CloseableHttpClient httpClient = HttpClients.custom() .setConnectionManager(connectionManager) .setMaxConnTotal(400) .setMaxConnPerRoute(150) .evictExpiredConnections() .build();
evictExpiredConnections 這個(gè)配置作用:
設(shè)置一個(gè)定時(shí)線程,定時(shí)清理閑置連接,可以將這個(gè)定時(shí)時(shí)間設(shè)置為 keep alive timeout 時(shí)間的一半以保證超時(shí)前回收
每個(gè)httpClient 對(duì)象都會(huì)有自己獨(dú)立的定時(shí)線程
這樣如果應(yīng)用中httpClient對(duì)象很多,就會(huì)導(dǎo)致上圖中線程太多
源碼中,如果設(shè)置了evictExpiredConnections 會(huì)有下面一段邏輯
List<Closeable> closeablesCopy = closeables != null ? new ArrayList<Closeable>(closeables) : null; if (!this.connManagerShared) { if (closeablesCopy == null) { closeablesCopy = new ArrayList<Closeable>(1); } final HttpClientConnectionManager cm = connManagerCopy; if (evictExpiredConnections || evictIdleConnections) { final IdleConnectionEvictor connectionEvictor = new IdleConnectionEvictor(cm, maxIdleTime > 0 ? maxIdleTime : 10, maxIdleTimeUnit != null ? maxIdleTimeUnit : TimeUnit.SECONDS, maxIdleTime, maxIdleTimeUnit); closeablesCopy.add(new Closeable() { @Override public void close() throws IOException { connectionEvictor.shutdown(); try { connectionEvictor.awaitTermination(1L, TimeUnit.SECONDS); } catch (final InterruptedException interrupted) { Thread.currentThread().interrupt(); } } }); connectionEvictor.start(); } closeablesCopy.add(new Closeable() { @Override public void close() throws IOException { cm.shutdown(); } }); }
IdleConnectionEvictor 對(duì)象是
public final class IdleConnectionEvictor { private final HttpClientConnectionManager connectionManager; private final ThreadFactory threadFactory; private final Thread thread; private final long sleepTimeMs; private final long maxIdleTimeMs; private volatile Exception exception; public IdleConnectionEvictor( final HttpClientConnectionManager connectionManager, final ThreadFactory threadFactory, final long sleepTime, final TimeUnit sleepTimeUnit, final long maxIdleTime, final TimeUnit maxIdleTimeUnit) { this.connectionManager = Args.notNull(connectionManager, "Connection manager"); this.threadFactory = threadFactory != null ? threadFactory : new DefaultThreadFactory(); this.sleepTimeMs = sleepTimeUnit != null ? sleepTimeUnit.toMillis(sleepTime) : sleepTime; this.maxIdleTimeMs = maxIdleTimeUnit != null ? maxIdleTimeUnit.toMillis(maxIdleTime) : maxIdleTime; this.thread = this.threadFactory.newThread(new Runnable() { @Override public void run() { try { while (!Thread.currentThread().isInterrupted()) { Thread.sleep(sleepTimeMs); connectionManager.closeExpiredConnections(); if (maxIdleTimeMs > 0) { connectionManager.closeIdleConnections(maxIdleTimeMs, TimeUnit.MILLISECONDS); } } } catch (final Exception ex) { exception = ex; } } }); }
會(huì)出現(xiàn)一個(gè)線程,這個(gè)線程里面就是去關(guān)閉超時(shí)不用的閑置鏈接
到此這篇關(guān)于關(guān)于HttpClient 引發(fā)的線程太多導(dǎo)致FullGc的問題的文章就介紹到這了,更多相關(guān)HttpClient 引發(fā)的線程太多導(dǎo)致FullGc內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java 枚舉類定義靜態(tài)valueOf(java.lang.String)方法的問題及解決
這篇文章主要介紹了java 枚舉類定義靜態(tài)valueOf(java.lang.String)方法的問題及解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09MyBatis學(xué)習(xí)教程(七)-Mybatis緩存介紹
MyBatis緩存分為一級(jí)緩存和二級(jí)緩存一級(jí)緩存,本文給大家介紹mybatis緩存知識(shí),非常不錯(cuò)具有參考借鑒價(jià)值,感興趣的朋友一起學(xué)習(xí)吧2016-05-05MyBatis-Plus Sequence主鍵的實(shí)現(xiàn)
這篇文章主要介紹了MyBatis-Plus Sequence主鍵的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12java開發(fā)分布式服務(wù)框架Dubbo服務(wù)引用過程詳解
這篇文章主要為大家介紹了java開發(fā)分布式服務(wù)框架Dubbo服務(wù)引用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2021-11-11Mybatis Mapper接口和xml綁定的多種方式、內(nèi)部實(shí)現(xiàn)原理和過程解析
在Mybatis中,我們需要?jiǎng)?chuàng)建一個(gè)與實(shí)體類對(duì)應(yīng)的Mapper接口,然后在該接口上添加方法,這些方法對(duì)應(yīng)著SQL語句,這篇文章主要介紹了Mybatis Mapper接口和xml綁定的多種方式、內(nèi)部實(shí)現(xiàn)原理和過程,需要的朋友可以參考下2023-11-11SpringBoot中EasyExcel實(shí)現(xiàn)Excel文件的導(dǎo)入導(dǎo)出
這篇文章主要介紹了SpringBoot中EasyExcel實(shí)現(xiàn)Excel文件的導(dǎo)入導(dǎo)出,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10