前言
这是一个生产问题, 从监控中发现 服务器节点 频繁进行 ygc, 没有 太大的规律
然后 大概会持续 一个小时左右, 然后 后面就自己好了, 也不会影响 具体的业务服务
具体的详细的 排查方式, 策略 这里就不写了, 没有精力 去写了
这里 直接 贴最终的结果, 复现的方式
上游的流式服务
这里直接构造 由于大模型幻觉或者其他影响导致的 大模型无限输出 "--"
@RestController
@RequestMapping("/HelloWorld")
public class HelloWorldController {
@PostMapping("/flowResponse")
public void flowResponse(HttpServletResponse response) throws Exception {
response.setContentType("text/event-stream");
response.setCharacterEncoding("utf8");
PrintWriter pw = response.getWriter();
for (int i = 1; i < Integer.MAX_VALUE; i++) {
pw.println(String.format("---"));
pw.flush();
Thread.sleep(10);
}
}
}
下游的业务服务
下游业务服务就是 从流式服务获取增量的文本, 然后 输出给前端页面
/**
* Test27PostInfiniteFlowRequest
*
* @author Jerry.X.He <970655147@qq.com>
* @version 1.0
* @date 2025-06-22 07:29
*/
public class Test27PostInfiniteFlowRequest {
// Test26PostFlowRequest
public static void main(String[] args) {
String url = "http://localhost:8080/HelloWorld/flowResponse";
// String url = "http://localhost:80/HelloWorld/flowResponse";
Map<String, String> headers = URLConnectionUtils.commonHeaders();
String postBody = "{}";
StringBuilder fullContent = new StringBuilder();
HttpResponse resp = URLConnectionUtils.postBodyWithFlow(url, headers, postBody, "utf8", line -> {
fullContent.append(line);
System.out.println(fullContent);
});
int x = 0;
}
}
gc 情况记录
现象就是 短时间内出现了多次 ygc, ygc 的主要就是 String.valueOf(fullContent)
由于内容越来越长, 单个 String.valueOf(fullContent) 占用的空间越来越大, 并且 无限在输出, 因此导致了 频繁的ygc

heapdump 的现象
字符串 倒序拍一下, 然后 找到嫌疑的 String, 结合业务代码 推导一下
轻量级一点的话, 直接结合多个 jstack 进行判断, 就可以了, 只是所谓的生产 各种条条框框罢了

完