Optimizing JVM Settings for 1M Daily Logins on an 8GB Server
This article walks through a step‑by‑step approach to sizing JVM heap, young generation, GC selection, and related parameters for a platform handling one million login requests per day on a node with 8 GB memory, providing practical formulas, example configurations, and tuning tips.
Step 1: Capacity Planning for a New System
Estimate object allocation per second, calculate young generation size, and predict GC frequency to decide how many instances and what memory each needs.
Compute memory used by objects created each second.
Estimate young generation size and Minor GC interval.
Adjust machine count and JVM heap to keep GC pauses acceptable.
Example: a login system with 1 M requests per day, peak 100 req/s, three 4C8G servers, each with 4 GB heap and 2 GB young generation can handle the load.
Step 2: Choosing a Garbage Collector
Decide between throughput‑oriented and latency‑oriented GC.
Throughput = CPU time for application / (CPU time for application + CPU time for GC) Response time = average GC pause timeLarge heap favors throughput; smaller heap reduces pause time. The trade‑off must be balanced.
GC Design Considerations
GC must stop the world (STW) to move objects.
STW pauses block request processing.
Young generation uses copying algorithms; old generation prefers non‑copying.
Goal: fewer, shorter GC pauses.
CMS vs. G1
Common combos: ParNew + CMS for low‑latency services, G1 for larger heaps (≥8 GB) and throughput‑oriented workloads.
Step 3: Partition Size Planning
Set heap size with -Xms and -Xmx (usually half of physical memory). Choose young generation size with -Xmn or -XX:NewRatio; typical values are 3/8 of heap for stateless services or up to 3/4 for lightweight services.
Set thread stack size with -Xss (usually 512 KB–1 MB).
Step 4: Stack Memory Size
Typical -Xss is 512 KB–1 MB; many threads can consume hundreds of megabytes.
Step 5: Object Tenuring Threshold
Adjust -XX:MaxTenuringThreshold (e.g., 5) so objects survive a few Minor GCs before promotion.
-Xms3072M -Xmx3072M -Xmn2048M -Xss1M -XX:MetaspaceSize=256M -XX:MaxMetaspaceSize=256M -XX:SurvivorRatio=8 -XX:MaxTenuringThreshold=5Step 6: Direct Allocation to Old Generation
Use -XX:PretenureSizeThreshold=1M for large objects.
-Xms3072M -Xmx3072M -Xmn2048M -Xss1M -XX:MetaspaceSize=256M -XX:MaxMetaspaceSize=256M -XX:SurvivorRatio=8 -XX:MaxTenuringThreshold=5 -XX:PretenureSizeThreshold=1MStep 7: CMS Old‑Generation Tuning
Typical flags:
-XX:+UseParNewGC -XX:+UseConcMarkSweepGC -XX:CMSInitiatingOccupancyFraction=70 -XX:+UseCMSInitiatingOccupancyOnly -XX:+AlwaysPreTouchStep 8: OOM Dump and GC Logging
-XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=${LOGDIR}/ -Xloggc:/dev/xxx/gc.log -XX:+PrintGCDateStamps -XX:+PrintGCDetailsGeneral JVM Parameter Template (ParNew+CMS)
-Xms4g -Xmx4g -Xmn2g -Xss1m -XX:SurvivorRatio=8 -XX:MaxTenuringThreshold=10 -XX:+UseConcMarkSweepGC -XX:CMSInitiatingOccupancyFraction=70 -XX:+UseCMSInitiatingOccupancyOnly -XX:+AlwaysPreTouch -XX:+HeapDumpOnOutOfMemoryError -verbose:gc -XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:+PrintGCTimeStamps -Xloggc:gc.logG1 Template for Large Heaps
-Xms8g -Xmx8g -Xss1m -XX:+UseG1GC -XX:MaxGCPauseMillis=150 -XX:InitiatingHeapOccupancyPercent=40 -XX:+HeapDumpOnOutOfMemoryError -verbose:gc -XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:+PrintGCTimeStamps -Xloggc:gc.logGC Selection Guidance
Small heap (<100 MB): Serial GC.
Single‑core: Serial GC.
Throughput‑oriented: Parallel GC.
Low‑latency: G1, ZGC, or CMS.
Metaspace vs. Permanent Generation
Metaspace resides in native memory, removing the fixed 64 MB limit of PermGen and avoiding OOM caused by class metadata.
-XX:MetaspaceSize=256M -XX:MaxMetaspaceSize=256MStop‑The‑World, OopMap, and Safepoints
GC pauses require all threads to stop at safepoints; OopMap records object reference locations for correct relocation.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Java Backend Technology
Focus on Java-related technologies: SSM, Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading. Occasionally cover DevOps tools like Jenkins, Nexus, Docker, and ELK. Also share technical insights from time to time, committed to Java full-stack development!
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.
