亲宝软件园·资讯

展开

关于HttpClient 引发的线程太多导致FullGc的问题

马力2020 人气:0
这篇文章主要介绍了关于HttpClient 引发的线程太多导致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;
    }
 
   }
  });
 }

会出现一个线程,这个线程里面就是去关闭超时不用的闲置链接

加载全部内容

相关教程
猜你喜欢
用户评论