Wednesday, July 22, 2026

Java Garbage Collection Interview Questions

JVM


  1. Executes Java bytecode
  2. Provides platform independence
  3. Manages memory automatically
  4. Handles garbage collection
  5. Runs Java applications


Just in Time compiler


  1. Compiles bytecode into native
  2. Optimizes frequently executed methods
  3. Improves runtime performance
  4. Runs during program execution
  5. Reduces repeated interpretation




Heap Memory


  1. Stores object instances
  2. Shared among all threads
  3. Managed by garbage collector
  4. Created during JVM startup
  5. Supports dynamic memory allocation


mutator 


  1. Application thread creating objects
  2. Runs alongside garbage collector
  3. Modifies heap objects
  4. Consumes allocated memory


collector


  1. Reclaims unused heap objects
  2. Runs garbage collection process
  3. Manages memory cleanup
  4. Works with mutator threads
  5. Improves memory utilization




Garbage Collector


  1. Reclaims unused heap memory
  2. Removes unreachable objects
  3. Prevents memory leaks
  4. Runs automatically by JVM
  5. Reduces manual memory management

Memory Leaks


  1. Unused objects still referenced
  2. Garbage collector cannot reclaim
  3. Heap memory gradually grows
  4. Causes OutOfMemoryError eventually
  5. Remove unnecessary object references



Serial Garbage Collector


  1. Uses single GC thread
  2. Stops all application threads
  3. Simple, low-overhead collector
  4. Best for small heaps
  5. Unsuitable for large applications





Young Generation


  1. Stores newly created objects
  2. Collected frequently by GC
  3. Contains Eden and Survivor
  4. Most objects die quickly
  5. Fast garbage collection


Survivor


  1. Stores surviving young objects
  2. Located in Young Generation
  3. Contains S0 and S1
  4. Objects age after GC
  5. Promoted after threshold


Old Generation 


  1. Stores long-lived objects
  2. Receives promoted objects
  3. Collected less frequently
  4. Uses major garbage collection
  5. Larger than Young Generation


Parallel Garbage Collector


  1. Uses multiple GC threads
  2. Stops application during collection
  3. Maximizes garbage collection throughput
  4. Suitable for multi-core CPUs
  5. Default before Java 9


Concurrent Mark Sweep Garbage collector


  1. Runs alongside application threads
  2. Minimises pause times
  3. Performs concurrent collection
  4. Improves application responsiveness

CMS phases


Initial Mark: Briefly pauses application threads to identify root objects.


Concurrent Mark: Traverses the object graph while the application is still running.


Remark: Pauses the application briefly to finalize marking updates.


Concurrent Sweep: Reclaims unreachable objects concurrently.


Concurrent Reset: Prepares the data structures for the next cycle.



fragmentation


  1. Free memory becomes scattered
  2. Small gaps remain unused
  3. Large allocations may fail
  4. Occurs after object deletion
  5. Compaction removes fragmentation


Garbage First Garbage Collector


  1. Heap divided into regions
  2. Collects highest garbage first
  3. Reduces pause times
  4. Performs concurrent background marking
  5. Default since Java 9

tuning of serial garbage collector


  1. Set initial heap size
  2. Set maximum heap size
  3. Adjust Young Generation size
  4. Enable Serial GC option
  5. Monitor GC pause times



tuning of parallel garbage collector


  1. Adjust parallel GC threads
  2. Set heap size limits
  3. Tune Young Generation size
  4. Configure throughput target
  5. Monitor GC performance logs


tuning of CMS garbage collector


  1. Set CMS initiating occupancy
  2. Enable parallel remark phase
  3. Tune Young Generation size
  4. Reduce fragmentation frequency
  5. Monitor concurrent GC logs

tuning of G1 garbage collector


  1. Set maximum pause target
  2. Adjust heap region size
  3. Configure heap size limits
  4. Tune concurrent GC threads
  5. Monitor GC pause logs


No comments:

Post a Comment

Choosing Programming Language

 ENIAC  Used Decimal system Initially used to find ballistic trajectory  Team adjusted knobs and final trajectory was pushed to Punch Card  ...