Elasticsearch Springboot2开发环境下客户端Netty连接占用过多byte[]类型内存问题解决方法

这几天使用elasticsearch构建全文检索服务时,在开发查询接口的时候,内存监控发现应用启动后会占用大量堆内存空间,即使手动GC后,发现内存仍然居高不下。

本来应用在开发阶段本地运行测试时是不手动添加内存上限的,但是当手动限制内存上限(-Xmx)的时候,发现如果分配的最大内存小1G时,经常会出现调用查询的时候应用发出OOM错误。查看内存Dump信息了解到其大量byte类型占据内存中,并被Netty包下的类所引用:
在这里插入图片描述
在这里插入图片描述
粗略看了下,“spring-boot-starter-data-elasticsearch”依赖下客户端连接工具包引用了netty相关代码(内存分配等),
在这里插入图片描述
检索后,从elasticsearch的github issue下找到了相关解答(https://github.com/elastic/elasticsearch/issues/26048)
原文如下:

It’s not a leak per se, it’s from the pooled allocator which would be shared across transport client instances that you create and not removed when you close a transport client instance. You can either disable the pooled allocator (start the JVM with the system property “io.netty.allocator.type” set to “unpooled”), or if you want to continue using the pooled allocator, you could look into tuning it for your use case.

于是在应用中手动添加该系统参数变量:

public class App {
    public static void main(String[] args) {
        System.setProperty("io.netty.allocator.type", "unpooled");
        SpringApplication.run(App.class, args);
    }
}

之后启动应用,并限制应用最大内存(-Xmx768m)进行测试。发现内存占用情况要比以前好一些,最起码在该内存限制范围下,简单测试接口时不会出现OOM。内存常驻占用也比以前少了30%左右,但是仍旧相比较占用内存居高,接近于elasticsearch server的内存使用量(大量byte类型)。(client比server占用内存还高,这倒是很诡异的事情)

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值