[go: up one dir, main page]

0% found this document useful (0 votes)
3 views3 pages

Section_3_Java_Core_True_Senior_H1_H2

The document discusses key concepts in Java, including the Java Memory Model (JMM), garbage collection (GC) issues, classloaders, and strategies for high-performance applications. It covers techniques for minimizing latency in high-frequency trading systems, implementing immutability, and the trade-offs between primitive and boxed types. Additionally, it addresses performance optimizations like escape analysis, biased locking, memory leak debugging, and the role of the JIT compiler in enhancing application performance.

Uploaded by

pbecic
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views3 pages

Section_3_Java_Core_True_Senior_H1_H2

The document discusses key concepts in Java, including the Java Memory Model (JMM), garbage collection (GC) issues, classloaders, and strategies for high-performance applications. It covers techniques for minimizing latency in high-frequency trading systems, implementing immutability, and the trade-offs between primitive and boxed types. Additionally, it addresses performance optimizations like escape analysis, biased locking, memory leak debugging, and the role of the JIT compiler in enhancing application performance.

Uploaded by

pbecic
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

3.

Java Core

How does the Java Memory Model (JMM) affect multi-threaded program
behavior?
 JMM defines how threads interact through memory and when writes by one thread
become visible to others.
 It specifies happens-before relationships to avoid visibility and reordering issues.
 Volatile, synchronized, and final fields are tools to enforce memory visibility guarantees.
 Reordering by the compiler, CPU, or JIT is legal unless constrained by JMM rules.
 Understanding JMM is essential to avoid subtle concurrency bugs like stale reads or
instruction reordering.

How would you analyze and resolve a high GC pause issue in a production Java
application?
 Identify GC algorithm in use (e.g., G1, ZGC) and analyze logs for frequency and pause
duration.
 Use tools like GCViewer, JFR, or GCeasy to visualize heap regions and promotion
failures.
 Check object allocation rate, large tenured objects, and excessive promotion into old
gen.
 Tune heap size, region sizes, and pause goals based on profiling data.
 Refactor code to reduce allocation pressure or cache usage that causes memory
retention.

How do classloaders impact memory usage and modularity in large-scale Java


applications?
 Each classloader maintains its own namespace, enabling isolation but increasing
memory usage.
 Leaked classloaders (e.g., via static references or thread locals) can lead to
permgen/metaspace leaks.
 Dynamic loading/unloading is crucial in plugin architectures (e.g., servlet containers,
OSGi).
 Understanding the delegation model prevents ClassNotFound or ClassCastExceptions.
 Classloader analysis is key in large enterprise apps with multiple modules or third-party
libs.

What strategies can you use to minimize latency in a Java-based high-frequency


trading system?
 Use lock-free data structures and avoid GC pauses with off-heap memory (e.g.,
Chronicle, Agrona).
 Prefer fixed-size thread pools and avoid object allocation in critical paths.
 Pin threads to CPUs (CPU affinity) and reduce context switching.
 Use real-time JVMs or tune G1/ZGC with low pause settings and minimal heap size.
 Disable safepoints and eliminate stop-the-world operations during peak trading hours.

How do you implement immutability effectively in Java and what are its
performance implications?
 Use final fields and private constructors, and avoid setters or mutable references.
 Defensive copying is necessary for collections or nested mutable objects.
 Immutability reduces synchronization needs and is naturally thread-safe.
 Overhead comes from frequent object creation; use builders or reuse instances when
possible.
 Works well with functional programming patterns and improves cache predictability.

What are the trade-offs between using primitive types vs boxed types in Java?
 Primitives are faster and memory-efficient; boxed types introduce GC overhead and
autoboxing latency.
 Boxed types are necessary in collections and generics but can lead to
NullPointerExceptions.
 Avoid unnecessary boxing in hot paths or numeric computations (e.g., avoid Double in
loops).
 Use specialized collections (e.g., Trove, fastutil) when performance matters.
 Profile and monitor heap allocation to identify boxing hotspots in production.

How do escape analysis and scalar replacement optimize performance in Java?


 Escape analysis identifies objects that don’t escape method scope and allocates them on
the stack.
 Scalar replacement breaks down objects into individual fields for register-based
optimization.
 These optimizations reduce GC pressure and eliminate heap allocations for short-lived
objects.
 JVM performs this dynamically at JIT compilation, so code patterns impact eligibility.
 Can be inhibited by use of synchronized blocks, reflection, or volatile fields.

How does biased locking work and when should it be disabled?


 Biased locking optimizes uncontested locks by avoiding atomic operations unless
contention is detected.
 It adds initial overhead and delay in detecting contention during JVM startup.
 In highly concurrent workloads, biased locking can degrade performance.
 Disable it with `-XX:-UseBiasedLocking` if locks are always contended or many threads
are involved.
 Biased locking was deprecated in later JVMs and removed in newer versions.
How would you debug a memory leak in a Java application where heap usage
grows slowly over time?
 Capture heap dumps at intervals and compare dominator trees using tools like MAT.
 Look for collections retaining objects unexpectedly (e.g., static maps, caches, listeners).
 Analyze allocation trends via JFR or async-profiler to correlate with code paths.
 Use reference chains and retained size to identify roots of unreachable objects.
 Check for unreleased resources like classloaders, thread locals, or unclosed streams.

How does the JVM just-in-time (JIT) compiler improve application


performance?
 JIT compiles hot code paths to native machine instructions for faster execution.
 Performs optimizations like method inlining, loop unrolling, and dead code elimination.
 Monitors runtime profiling data to adapt optimization strategies over time.
 Can deoptimize and recompile based on new usage patterns or exception traps.
 Critical for throughput-sensitive applications where interpreter overhead is
unacceptable.

You might also like