Ngoptimalake aplikasi JVM mencakup profiling kanggo nemokake bottleneck, JVM/GC tuning, perbaikan code-level, lan manfaatake JIT compiler. Kaya biasane, aturane: ukur dhisik, optimalake bottleneck sing wis dibukti — aja teka-teken.
Profile before optimizing
Tools:
✓ JProfiler / YourKit / VisualVM — CPU & memory profiling, hotspots
✓ Java Flight Recorder (JFR) + Mission Control — low-overhead production profiling
✓ async-profiler — flame graphs for CPU/allocation
✓ heap dumps + Eclipse MAT — memory leak analysis
Profiling nbukak ngendi waktu lan memori bener-bener arep, dadi sampeyan ndandani 20% sing bener-bener penting tinimbang micro-optimize kode sing ora penting.
The JIT compiler works for you
The JVM's JIT (Just-In-Time) compiler identifies "hot" frequently-run code and
compiles it to optimized native machine code at runtime (with inlining, etc.).
→ Java often approaches native speed for hot paths after warm-up.
→ Implication: benchmark AFTER warm-up; let the JIT optimize before measuring.
GC tuning
# choose a collector appropriate to the workload, size the heap
java -Xms2g -Xmx2g \ # set heap size (avoid resizing; -Xms = -Xmx)
-XX:+UseG1GC \ # G1 (balanced, default) — or ZGC for low pause
-XX:MaxGCPauseMillis=200 \
MyApp
✓ Right-size the heap (too small → frequent GC; too large → long pauses)
✓ Pick the GC for your goal: G1 (balanced), Parallel (throughput), ZGC (low latency)
✓ Reduce allocation churn — fewer short-lived objects = less GC pressure
Code-level optimizations
✓ Better algorithms & data structures — biggest wins (HashMap O(1) vs list O(n))
✓ Avoid unnecessary object creation (esp. in loops; reuse, use primitives over wrappers)
✓ Use StringBuilder for string building in loops (not + concatenation)
✓ Minimize autoboxing (Integer vs int) in hot paths
✓ Cache expensive results; use connection/thread pools
✓ Lazy initialization for costly objects not always needed
✓ Reduce I/O round-trips; batch database queries (avoid N+1)
The database is often the real bottleneck
For server apps, the slow part is frequently DB queries (N+1, missing indexes) or
network I/O — NOT Java code. Profiling reveals this so you fix the true cause.
Avoid premature optimization
"Premature optimization is the root of all evil." Write clear, correct code first;
profile under realistic load; then optimize the measured hotspots specifically.
Kenapa iki penting
Optimalisasi kinerja JVM nduweni nilai dhuwur nanging kudu terukur lan dipandu pengukuran — teka-teken buang-buang tenaga lan nambah kerumitan.
Ngerti toolkit lengkap iku penting: profiler (JFR, async-profiler) kanggo nemokake bottleneck sing bener; ngerti JIT (sing ngoptimalake hot code ing runtime, dadi benchmark kudu warm-up); GC tuning (heap sizing lan collector choice — G1/ZGC — kanggo balance throughput versus pause times); lan kode-level wins (algoritma lan struktur data sing luwih apik, ngurangi alokasi lan autoboxing, pooling, batching).
Kangenal yen bottleneck asli iku asring database utawa I/O, dudu kode Java, fokus tenaga kanthi bener.
Kombinasi iki — profil dhisik, tune JVM lan GC kanthi apik, optimalake hotspot sing wis dibukti, lan ilangi premature micro-optimization — iku sing njaga aplikasi JVM gedhe tetep cepet lan scalable, lan ngrefleksikake keahlian operasional senior-level.
