Measuring Heap Memory Allocation per HTTP Request in SpringBoot
This article details a SpringBoot experiment using JMeter to measure the heap memory allocated per HTTP request, analyzes GC logs, and discusses how request size and logging affect memory consumption, providing practical JVM tuning insights for backend developers.
Introduction: The author, a senior architect, explains the need to quantify heap memory usage per RPC/HTTP request to optimize GC and performance.
Experiment design: Create a SpringBoot 2.5.4 application with a POST endpoint and a GET endpoint to trigger GC; use JMeter with 10 threads each performing 2000 requests (total 20,000). Enable detailed GC logging and set JVM options (e.g., -Xmx4g -Xms4g -XX:SurvivorRatio=8 -Xmn2g, etc.).
Code snippet for the controller:
@Slf4j
@RestController
public class TestController {
private AtomicLong count = new AtomicLong(0);
@ResponseBody
@RequestMapping(value = "create", method = RequestMethod.POST)
public String create(@RequestBody Order order) {
//log.warn("收到提单请求 cnt{}:{}", count.getAndIncrement(), order);
return "ok";
}
@ResponseBody
@RequestMapping(value = "gc", method = RequestMethod.GET)
public String gc() {
System.gc();
return "ok";
}
}JMeter configuration: set thread group, HTTP defaults, request headers (Content-Type: application/json), and request body.
Execution: Run JMeter, manually trigger GC before the test, then collect GC logs to calculate new generation heap growth.
Results: Average heap allocation per HTTP request is about 34 KB even with a tiny 50‑character payload; adding a larger payload (1200 characters) increases it to ~36 KB. Logging the request adds ~20 KB, raising usage to ~56 KB. Removing the payload drops usage to ~35.7 KB.
Analysis: The memory cost is dominated by SpringBoot’s internal object creation rather than the request body size; excessive logging significantly increases memory and GC frequency.
Conclusion: Controlling log size and understanding per‑request memory allocation are crucial for backend performance; in larger production systems, RPC requests can consume 0.5–1 MB, requiring multiple young GCs per minute under high concurrency.
Top Architect
Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.