HttpClient高并发内存溢出

apache 的HttpClient很强大,据说可以承受一万左右的高并发,但是在做项目的时候用HttpClient进行附件上传,并发1000不到的时候都导致了内存溢出,核心代码为:

HttpPost post = new HttpPost(url.toString()); post.setEntity(multipartEntityBuilder.build());

HttpResponse response = httpClient.execute(post);

经过调试和查看httpClient的源码发现,由于httpClient.execute(post)返回值response并不能close,因此在上传大文件而且高并发的情况下会导致线程一直被占用,导致资源不足

用CloseableHttpClient来替代HttpClient,httpClient.execute(post)得到CloseableHttpResponse可以被关闭,从而避免了上述问题

  HttpPost post = new HttpPost(url.toString());
        post.setEntity(multipartEntityBuilder.build());
        // try with resource语法response.close()会被自动调用
        try (CloseableHttpResponse response = httpClient.execute(post)) { 

        } catch (Exception ex) {

        } finally {

        }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值