Java is one of the most common platforms for enterprise applications.
For performance engineers, understanding Java threads, concurrency, JVM internals, and tuning options is critical.
This post is a refresher on the most important Java performance tuning topics you need for interviews.
🔹 1. Threads & Concurrency
Q1. Difference between process and thread
- Process: independent execution unit with its own memory space.
- Thread: lightweight execution unit within a process, shares memory. 💡 Interview Angle: Thread-level issues (locks, contention) often cause performance bottlenecks.
Q2. Thread States
- NEW → RUNNABLE → BLOCKED → WAITING → TIMED_WAITING → TERMINATED 💡 Understand what causes BLOCKED vs WAITING → often asked in concurrency interviews.
Q3. Thread Pools (ExecutorService)
ExecutorService pool = Executors.newFixedThreadPool(10);
pool.submit(() -> System.out.println("Task executed"));
pool.shutdown();
`
- Prevents overhead of creating/destroying threads.
- Use bounded pools to avoid unbounded thread creation.
Q4. Common Threading Issues
- Deadlocks
- Starvation
- Livelocks
- Race conditions 💡 Interview Angle: Explain with real examples — e.g., DB connection pool deadlocks.
Q5. Concurrency Utilities
-
ReentrantLockvssynchronized -
ConcurrentHashMapvsHashMap -
CountDownLatch,CyclicBarrier,Semaphore
👉 Example:
java
ConcurrentHashMap<String, String> cache = new ConcurrentHashMap<>();
cache.put("k1", "v1");
🔹 2. JVM & Memory Model
Q6. JVM Memory Areas
- Heap → Young Gen, Old Gen
- Stack → per-thread method frames
- Metaspace → class metadata
- Code Cache → JIT compiled code
Q7. Garbage Collectors
- Serial GC → single-threaded
- Parallel GC → multi-threaded
- CMS (deprecated) → concurrent mark-sweep
- G1 GC → balances latency & throughput
- ZGC / Shenandoah → low-latency
💡 Interview Angle: Be ready to answer “Which GC would you pick for low latency apps?” → G1/ZGC.
Q8. GC Tuning Flags
bash
-XX:+UseG1GC
-XX:MaxGCPauseMillis=200
-Xms2g -Xmx2g
- Always set Xms = Xmx to avoid resizing overhead.
- Tune pause times for SLAs.
Q9. OutOfMemory Errors
- Java heap space → insufficient heap.
- GC overhead limit exceeded → too much time in GC.
- Metaspace → class loading issues.
💡 Tools: jmap, jstack, jconsole, VisualVM.
🔹 3. Performance Tuning Concepts
Q10. Object Creation
- Avoid unnecessary objects inside loops.
- Use StringBuilder instead of String concatenation.
Q11. Caching
- In-memory caches (
ConcurrentHashMap, Guava, Caffeine). - Distributed caches (Redis, Hazelcast). 💡 Performance engineers should know how caching reduces DB calls.
Q12. Connection Pools
- Use HikariCP, DBCP.
- Tune pool size = (
(Core count * 2) + Effective Spindle Count).
Q13. Synchronization Overhead
- Avoid global locks.
- Prefer lock-free structures (
AtomicInteger,ConcurrentHashMap).
Q14. Thread Dump Analysis
Command:
bash
jstack <pid>
Look for:
- Deadlocks
- High CPU threads (runnable state)
- Long GC pauses
🔹 4. Key Java Performance Questions (Interview Style)
Q15. How do you detect a memory leak in Java?
- Monitor heap usage over time → if it grows endlessly = leak.
- Use tools: Eclipse MAT, VisualVM.
Q16. What’s the difference between == and .equals()?
-
==→ reference comparison. -
.equals()→ value comparison. 💡 Important in performance tuning when dealing with collections.
Q17. Why is synchronized slow?
- Causes context switching and blocking.
- Alternatives:
ReentrantLock,Atomicclasses.
Q18. How to reduce GC overhead?
- Reuse objects (object pools).
- Use primitive collections (Trove, FastUtil).
- Tune heap sizes and GC algorithm.
Q19. What is false sharing?
- Multiple threads update variables on the same CPU cache line → performance degradation.
💡 Fix: use padding (
@Contendedannotation).
Q20. What profiling tools do you use?
- JDK tools: jmap, jstack, jconsole, VisualVM
- APM tools: Dynatrace, AppDynamics, New Relic
- Others: YourKit, JProfiler
✅ Final Takeaway
For Java performance tuning interviews, focus on:
- Threads & Concurrency → thread pools, locks, race conditions
- JVM Memory Model → heap, GC, tuning flags
- Performance Concepts → caching, connection pools, synchronization overhead
- Troubleshooting → thread dumps, memory leaks, profiling tools
If you can connect these concepts to real issues you solved in performance testing, you’ll stand out as a performance engineer, not just a tester.
`
Top comments (0)