Measuring Heap Memory Consumption of HTTP and RPC Requests in Spring Boot
The article presents a Spring Boot experiment measuring per‑request heap memory usage for HTTP and RPC calls, details the JMeter test setup, analyzes GC logs, reports findings such as ~34KB per HTTP request and 0.5‑1MB per RPC request, offers optimization recommendations, and also includes promotional material for AI‑related products.
The author, a senior architect, explains the need to measure heap memory allocated per HTTP and RPC request in order to optimize GC parameters and improve system performance.
1. Experiment Idea
By knowing the heap memory size required for a single RPC or HTTP request, one can calculate the total heap memory needed for a given concurrency level and estimate GC frequency.
2. Key Actions
Create a new Spring Boot 2.5.4 application.
Add a POST endpoint for JMeter to call.
Configure a JMeter test plan with 10 threads, each performing 2000 HTTP calls (total 20,000 calls).
Enable detailed GC logging in Spring Boot to record new‑generation memory allocation.
3. Spring Boot HTTP Interface Declaration
The following code defines a POST endpoint create and a GET endpoint gc that triggers manual garbage collection:
@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("Received order cnt{}:{}", count.getAndIncrement(), order);
return "ok";
}
@ResponseBody
@RequestMapping(value = "gc", method = RequestMethod.GET)
public String gc() {
System.gc();
return "ok";
}
}4. JMeter Test Plan
In JMeter a thread group with 10 threads is created, each looping 2000 times. HTTP Request defaults are set, a JSON body is added, and the Content‑Type header is configured.
5. Experiment Process
The Spring Boot application runs with a 4GB heap (2GB young generation) and GC logs are written to -Xloggc:/Users/testUser/log/gc.log . Before the load test, manual GC is performed to clear existing objects.
JMeter executes the 20,000 HTTP calls, after which a manual GC is triggered and the GC log is examined to calculate the total new‑generation memory growth, which corresponds to the memory allocated by the requests.
6. Results
Even with a tiny request body (≈50 characters), each HTTP call consumes about 34 KB of heap memory, indicating that Spring Boot creates additional internal objects during request processing.
When the request body is enlarged to 1200 characters, the per‑call memory usage rises to roughly 36 KB, showing a proportional increase.
Adding a log statement inside the controller increases the average memory per request to 56 KB, demonstrating that log size directly impacts memory consumption and GC frequency.
In the author's production environment, a single RPC request consumes between 0.5 MB and 1 MB due to more complex business logic, multiple downstream calls, and extensive logging.
7. Conclusions and Recommendations
Control the size of individual log entries to reduce memory overhead and GC pauses. Accurate per‑request memory measurement helps in capacity planning and JVM tuning.
8. Promotional Content (Non‑Academic)
The latter part of the article contains advertisements for AI tools, paid courses, community memberships, and other commercial offers, which are unrelated to the technical experiment.
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.