打印大对象日志导致GC问题的解决

内容:

  • rpc调用外部服务时,需要将req和resp的信息打印出来,以便于排查问题。
  • 但是有的rpc服务的resp信息过于庞大,比如resp中有List<>信息,list很大很大时
  • 会导致log.info打印信息时,产生GC,影响业务

通过将大对象拆分为小对象,解决GC耗时过长问题,进而解决GC过长对业务的影响(TP999过长导致超时、慢sql)等

1、背景

正常情况下,2M、3M就为大对象。会直接被分配至old region。如果此大对象为日志对象,被频繁的加入老年代,会引起Full Gc,影响接口性能或db性能。

2、解决

将大对象,part拆分为小对象再打印,这样young gc更快,影响更小

3、code实现(以List的拆分为例)

@Slf4j
@Component
public class PartLogUtils {

    public <V> void partLog(List<V> info,
                                    int count,
                                    Logger logger,
                                    String format) {
        List<List<V>> result = splitInfo(info, count);
        logInfo(result, logger, format);
    }
    
     private <V> List<List<V>> splitInfo(List<V> info, int count) {
        List<List<V>> result = Lists.newArrayList();
        if (CollectionUtils.isEmpty(info) || info.size() < count) {
            result.add(info);
            return result;
        }
        List<V> temp = Lists.newArrayList();
        int index = 0;
        for (V item : info) {
            index++;
            temp.add(item);
            if (index % count == 0) {
                result.add(temp);
                temp = Lists.newArrayList();
            }
        }
        if (temp.size() > 0) {
            result.add(temp);
        }
        return result;
    }
    
    private <V> void logInfo(List<V> infoList, Logger logger, String format) {
        for (V item : infoList) {
            logger.info(format, GsonUtils.toJsonStr(item));
        }
    }
   
}

类A中使用

private  final  static Logger logger = LoggerFactory.getLogger(A.class);

PartLogUtils.partLog(resp, 2000, logger,"queryUserInfo:[{}]");

4、打印Map对象的优化同理

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值