欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-03-23 12:44:01
closeablehttpclient httpclient = httpclients.custom() .setconnectionmanager(connectionmanager) .se...
closeablehttpclient httpclient = httpclients.custom()
  .setconnectionmanager(connectionmanager)
  .setmaxconntotal(400)
  .setmaxconnperroute(150)
  .evictexpiredconnections()
  .build();

evictexpiredconnections 这个配置作用:

设置一个定时线程,定时清理闲置连接,可以将这个定时时间设置为 keep alive timeout 时间的一半以保证超时前回收

每个httpclient 对象都会有自己独立的定时线程

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

这样如果应用中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;
    }
 
   }
  });
 }

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

到此这篇关于关于httpclient 引发的线程太多导致fullgc的问题的文章就介绍到这了,更多相关httpclient 引发的线程太多导致fullgc内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!