Measuring Heap Memory Allocation per HTTP Request in SpringBoot Applications
This article presents a SpringBoot-based experiment measuring the heap memory allocated per HTTP request, detailing the setup with JMeter, JVM GC logging, observed memory usage of roughly 34 KB per request, analysis of GC frequency, and concludes with recommendations on logging and memory optimization, while also promoting related AI services and community resources.
1. Experiment Idea
In real work, full‑link pressure testing and GC parameter tuning are often needed. Knowing the heap memory required for one RPC or HTTP request allows precise calculation of total heap needed for a given concurrency and estimation of GC frequency.
2. SpringBoot HTTP Interface Declaration
The following code defines a POST endpoint create and a GET endpoint gc for triggering 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("收到提单请求 cnt{}:{}", count.getAndIncrement(), order);
return "ok";
}
@ResponseBody
@RequestMapping(value = "gc", method = RequestMethod.GET)
public String gc() {
System.gc();
return "ok";
}
}3. JMeter Test Plan
3.1 Add Thread Group
Configure 10 threads, each looping 2000 times, for a total of 20000 HTTP calls.
3.2 HTTP Defaults
Set the target URL and request body; add a Content-Type: application/json header because the payload is JSON.
4. Experiment Process
4.1 Start SpringBoot Application
Heap size 4 GB, young generation 2 GB, SurvivorRatio=8 (each survivor occupies 1/10 of the young generation). GC log location is set with -Xloggc:/Users/testUser/log/gc.log .
java -server
-Xmx4g -Xms4g -XX:SurvivorRatio=8 -Xmn2g
-XX:MetaspaceSize=512m -XX:MaxMetaspaceSize=1g -XX:MaxDirectMemorySize=1g
-XX:+HeapDumpOnOutOfMemoryError -XX:+PrintGCCause -XX:+PrintGCDetails
-XX:+PrintGCTimeStamps -XX:+PrintGCDateStamps -XX:+PrintTenuringDistribution
-XX:+UnlockDiagnosticVMOptions -XX:ParGCCardsPerStrideChunk=32768
-XX:+PrintCommandLineFlags -XX:+UseConcMarkSweepGC -XX:+UseParNewGC
-XX:ParallelCMSThreads=6 -XX:+CMSClassUnloadingEnabled
-XX:+UseCMSCompactAtFullCollection -XX:+CMSParallelInitialMarkEnabled
-XX:+CMSParallelRemarkEnabled -XX:+CMSScavengeBeforeRemark -XX:+PrintHeapAtGC
-XX:CMSFullGCsBeforeCompaction=1 -XX:CMSInitiatingOccupancyFraction=70
-XX:+UseCMSInitiatingOccupancyOnly -XX:+PrintReferenceGC
-XX:+ParallelRefProcEnabled -XX:ReservedCodeCacheSize=256M
-jar target/activiti-0.0.1-SNAPSHOT.jar4.2 Manual GC Before Test
Execute curl http://localhost:8080/gc to clean existing objects before the pressure test.
4.3 Run JMeter Test
Execute the JMeter test plan, invoking the HTTP endpoint 20000 times.
4.4 GC Log Analysis
After GC, the Eden space is empty; the memory used before GC equals the total memory allocated by the 20000 HTTP calls, indicating each request’s heap allocation.
5. Experiment Results
Each HTTP request in SpringBoot consumes about 34 KB of heap memory even when the request body contains only 50 characters. Adding a larger detail field (1200 characters) raises the average to about 36 KB, a difference that matches the payload size.
{"userId": 32898493, "productId":39043, "detail": ""}Enabling request‑level logging increases the average memory consumption to roughly 56 KB per request, showing that log size has a significant impact on memory usage and GC frequency.
6. Real‑world Data
In production, a single RPC request may consume 0.5 MB–1 MB due to complex business logic, multiple downstream calls, database and cache accesses, and extensive logging.
7. Recommendations
Control the size of individual log entries to reduce memory usage and GC frequency, thereby improving overall system performance.
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.