關于HttpClient 引發(fā)的線程太多導致FullGc的問題
CloseableHttpClient httpClient = HttpClients.custom() .setConnectionManager(connectionManager) .setMaxConnTotal(400) .setMaxConnPerRoute(150) .evictExpiredConnections() .build();
evictExpiredConnections 這個配置作用:
設置一個定時線程,定時清理閑置連接,可以將這個定時時間設置為 keep alive timeout 時間的一半以保證超時前回收
每個httpClient 對象都會有自己獨立的定時線程
這樣如果應用中httpClient對象很多,就會導致上圖中線程太多
源碼中,如果設置了evictExpiredConnections 會有下面一段邏輯
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 對象是
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; } } }); }
會出現(xiàn)一個線程,這個線程里面就是去關閉超時不用的閑置鏈接
到此這篇關于關于HttpClient 引發(fā)的線程太多導致FullGc的問題的文章就介紹到這了,更多相關HttpClient 引發(fā)的線程太多導致FullGc內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
java 枚舉類定義靜態(tài)valueOf(java.lang.String)方法的問題及解決
這篇文章主要介紹了java 枚舉類定義靜態(tài)valueOf(java.lang.String)方法的問題及解決,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-09-09MyBatis-Plus Sequence主鍵的實現(xiàn)
這篇文章主要介紹了MyBatis-Plus Sequence主鍵的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-12-12java開發(fā)分布式服務框架Dubbo服務引用過程詳解
這篇文章主要為大家介紹了java開發(fā)分布式服務框架Dubbo服務引用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步2021-11-11Mybatis Mapper接口和xml綁定的多種方式、內(nèi)部實現(xiàn)原理和過程解析
在Mybatis中,我們需要創(chuàng)建一個與實體類對應的Mapper接口,然后在該接口上添加方法,這些方法對應著SQL語句,這篇文章主要介紹了Mybatis Mapper接口和xml綁定的多種方式、內(nèi)部實現(xiàn)原理和過程,需要的朋友可以參考下2023-11-11SpringBoot中EasyExcel實現(xiàn)Excel文件的導入導出
這篇文章主要介紹了SpringBoot中EasyExcel實現(xiàn)Excel文件的導入導出,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-10-10