Category: Java interview questions
-
False Sharing in Multithreading – Explanation and How to Avoid It
1. Introduction to False Sharing In multi-threaded programming, false sharing is a performance-degrading phenomenon where multiple threads access different variables that happen to reside on the same CPU cache line. Even though the threads are accessing different variables, they cause cache invalidation in each other’s cores, leading to unnecessary cache coherence traffic. This causes significant…
-
Differences Between CMS, G1GC, and ZGC – When Would You Choose Each in Production?
1. Heap Layout and Design 2. Pause Time Behavior and Stop-the-World Events 3. Compaction and Fragmentation Handling 4. Throughput and Application Performance 5. Latency and Pause Time Guarantees 6. Heap Size and Scalability 7. Tuning Complexity and Maintenance 8. GC Availability and Status
-
How Do You Debug and Fix a Memory Leak in a Production JVM?
1. Introduction Memory leaks in Java applications can cause severe performance degradation, frequent OutOfMemoryErrors, and even complete system crashes. While Java has garbage collection, leaks can still occur if references to unused objects are unintentionally held. 2. What Is a Memory Leak in Java? In Java, a memory leak occurs when an object is no…
-
Comparing synchronized vs StampedLock vs VarHandle for Concurrency Control in Java
Overview Java provides multiple mechanisms to control access to shared resources in a concurrent environment: Feature Synchronized Block StampedLock VarHandle Type Intrinsic Lock Explicit Lock Low-Level Memory Access Read/Write Support No distinction Yes (Optimistic, Pessimistic) No lock abstraction Reentrancy Yes No Not applicable Fairness Not guaranteed Not guaranteed Not applicable Best Use Case Simplicity, legacy…
-
How the Java Memory Model (JMM) Ensures Visibility and Ordering of Operations in Multi-Core Systems
1. Introduction Java applications often run on multi-core, multi-threaded systems, where multiple threads can read and write shared variables concurrently. While this enables powerful parallelism, it introduces serious challenges around memory visibility, instruction ordering, and data consistency. To address this, Java introduced the Java Memory Model (JMM) in Java 1.5 (JSR-133) as part of the…
-
Scalability Challenges with ConcurrentHashMap under High Contention and How to Optimize It
1. Introduction Java’s ConcurrentHashMap is a thread-safe implementation of a hash-based map that allows concurrent reads and updates without requiring full synchronization. It was designed to scale better than Collections.synchronizedMap() or Hashtable in multi-threaded environments. However, when multiple threads frequently access or modify the map, especially on the same keys or buckets, performance bottlenecks can…
-
Class loading process in Java
The class loading process in Java 21, while having evolved significantly from earlier versions (especially with the introduction of modules in Java 9), still fundamentally follows a three-step delegation model: Loading, Linking, and Initialization. However, the context of modules and the removal of the Extension Class Loader in favor of a simpler application class loader…
-
orElse() vs orElseGet() in Optional
The main difference between orElse and orElseGet in the Java Optional class is when and how the default value is computed: Method Argument Type When is it evaluated? Use Case orElse T (actual value/result) Always, even if Optional is present Use when default value is cheap to create or constant orElseGet Supplier<? extends T> Only if Optional is empty Use…
-
How does instanceof pattern matching enhance type safety in Java 16
Pattern matching for instanceof in Java 16 significantly enhances type safety and code reliability, as well as developer productivity, by changing how type checks and casts are handled in the language. What Changed in Java 16 Before Java 16, checking an object’s type and using it required two steps: Example (pre-Java 16): This approach, although common, is…
-
Difference between List.of() and Array.asList()
Here’s a clear comparison of List.of() and Arrays.asList() in Java: 1. Mutability 2. Null Handling 3. Backing 4. Use Cases 5. Example
