JVM Flashcards

1
Q

What is JVM in Java?

A

JVM stands for Java Virtual Machine. It is the virtual machine on which Java code executes. JVM is responsible for converting bytecode into machine-specific code.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Discuss HotSpot JVM (Java Virtual Machine) Architecture in short.

A

HotSpot JVM consists of three main components:
Class Loader Subsystem
Responsible for loading class files.

Runtime Data Areas:
- Method Area: Stores data for each class including fields constant pool and method information.
- Heap: Where all objects are stored.
- Java thread Stacks: Each method call creates a new stack frame.
- Program Counter Registers (PC Registers): Point to the next instruction to be executed.
- Native thread stack: Contains information related to the native platform.

Execution Engine
- JIT (Just-In-Time) compiler for bytecode-to-machine code compilation
- Garbage Collector for memory management.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the purpose of the Class Loader Subsystem in JVM?

A

The Class Loader Subsystem is responsible for loading class files into memory. It ensures that classes are available for execution.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the Execution Engine in JVM?

A

The Execution Engine executes bytecode assigned to the runtime data areas. It includes the JIT compiler which compiles bytecode to machine code at runtime and the Garbage Collector for memory management.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are the steps involved in Garbage Collection?

A

Garbage Collection involves three steps:1. Mark: Traverse the object graph from the root marking reachable objects as alive.2. Sweep: Delete unmarked (dead) objects.3. Compact: Defragment memory by making allocations contiguous for live objects.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the purpose of the Eden space in memory management during GC?

A

Newly created objects reside in the Eden space until it becomes full. Minor GC runs marking live objects and moving them to ‘Survivor from’ space while sweeping the Eden space.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How does a minor GC differ from a major GC?

A

Minor GC focuses on the young generation (Eden space and survivor spaces) while major GC traverses the entire heap memory (including tenured space).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What happens to live objects during a minor GC?

A

Live objects are moved from the Eden space to the survivor spaces during a minor GC.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What triggers a minor GC to run?

A

When the Eden space is full

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How are objects moved between the survivor spaces during minor GC?

A

Objects are marked and moved from ‘Survivor from’ to ‘Survivor to’ space during minor GC cycles.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is the role of the tenured space in memory management?

A

Tenured space holds long-lived objects that have survived multiple minor GC cycles.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How does the JVM keep track of the number of GC cycles survived by each object?

A

The JVM uses the -XX:MaxTenuringThreshold parameter to track the number of GC cycles an object has survived.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is the significance of the -XX:MaxTenuringThreshold parameter?

A

It determines when objects are promoted from survivor spaces to the tenured space.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Why is frequent major GC undesirable for application performance?

A

Major GC causes longer pauses and can impact user interactions.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How can we detect memory issues related to the tenured space?

A

Frequent occurrences of ‘java.lang.OutOfMemoryError’ in logs or high CPU usage due to frequent GC runs. Can also be observed during Memory Profiling

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is the impact of memory leaks on GC behavior?

A

Memory leaks can fill up the tenured space leading to more frequent major GC cycles.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

How does the JVM handle objects that reside in the Eden space?

A

Newly created objects reside in the Eden space until it becomes full.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What happens when the Eden space becomes full?

A

A minor GC runs moving live objects to the survivor spaces and sweeping the Eden space.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

How does the JVM decide which objects to move to the survivor spaces?

A

Objects are selected based on their age (number of GC cycles survived).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

What is the purpose of the ‘Survivor from’ and ‘Survivor to’ spaces?

A

They serve as intermediate spaces for objects during minor GC cycles.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

How does the JVM determine when an object should be promoted to the tenured space?

A

Based on the configured tenuring threshold and the number of GC cycles survived.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

What is the difference between a soft reference and a weak reference in Java?

A

Soft references allow objects to be garbage collected only when memory is low while weak references allow immediate collection.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

How can we optimize memory usage to reduce the frequency of GC?

A

By
- managing object lifetimes
- minimizing memory leaks and
- tuning GC parameters.

24
Q

What are the trade-offs between different GC algorithms (e.g. G1, Serial, Parallel)?

A

Different algorithms balance throughput latency and memory overhead differently.

25
Q

How can we analyze GC logs to identify performance bottlenecks?

A

By examining GC logs for pause times, memory usage and frequency of GC events.

26
Q

What is the major deciding factor when fine-tuning JVM memory utilization?

A

The major deciding factor is whether Responsiveness/latency (the amount of time taken by one single Garbage collection event to run) or Throughput (amount of work completed by the application to run the transactions in a given time) is more critical.

27
Q

When is compromising with pauses for major garbage collection (GC) acceptable?

A

If throughput is of utmost importance (e.g. in batch processing) compromising with pauses for major GC to improve overall throughput is acceptable.

28
Q

What should be prioritized in a UI-based application regarding garbage collection?

A

In a UI-based application responsiveness/gc latency is of utmost importance. Therefore avoiding major GC is crucial.

29
Q

What is the trade-off when delaying major GC by increasing young generation space?

A

While delaying major GC can improve responsiveness it may lead to longer minor GC times due to traversing and compacting a larger young generation space.

30
Q

What are the types of garbage collectors in Java?

A
  1. Serial Garbage Collector:
  • Simplest implementation, using a single thread.
  • Freezes all application threads during garbage collection (stop-the-world event).
  • Suitable for single-threaded environments but not recommended for multi-threaded applications.
  • Activate with -XX:+UseSerialGC JVM argument.
  1. Parallel Garbage Collector:
  • Default GC used by the JVM 5-8.
  • Similar to the serial collector but uses multiple threads for garbage collection.
  • Also known as the throughput collector.
  • Suitable for long processes (e.g., batch processing) where longer pauses are acceptable.
  • Activate with -XX:+UseParallelGC JVM argument.
  1. Concurrent Mark Sweep (CMS) Garbage Collector:
  • Deprecated and removed
  • Designed to minimize pause times.
  • Works concurrently with application threads.
  • Suitable for applications requiring low-latency response times.
  • Activate with -XX:+UseConcMarkSweepGC JVM argument.
  1. Garbage First (G1) Garbage Collector:
  • Balances throughput and latency.
  • Divides heap into regions and collects the most garbage-rich regions first.
  • Suitable for large heaps and applications with varying memory requirements.
  • Activate with -XX:+UseG1GC JVM argument.
31
Q

CMS – Concurrent Mark and Sweep

A

Deprecated since Java 9.

Suitable for large memory spaces with high CPU counts
The CMS collector uses more CPU than other GCs. If you can allocate more CPU for better performance then CMS garbage collector is a preferred choice over the parallel collector. CMS garbage collector uses multiple threads to scan the heap memory to mark instances for eviction and then sweep the marked instances.

32
Q

Which scenario is good for the Parallel Collector GC

A

The parallel collector is also known as the throughput collector because it is often the best choice when throughput is more important than latency. You can use the parallel collector when long pauses are acceptable, such as bulk data processing, batch jobs, etc.

33
Q

Describe the G1 (Garbage First) collector

A

G1 is a server-style collector designed for multiprocessor machines with a large amount of memory. The collector tries to achieve high throughput along with short pause times, while requiring very little tuning. G1 is selected by default on certain hardware and operating systems, and can be explicitly enabled through the -XX:+UseG1GC option.

Divides memory into regions and is the successor to CMS Collector

34
Q

What is the purpose of the Serial Collector in Java?

A

The Serial Collector runs in a single thread and is suitable for basic applications. The Serial GC is the garbage collector of choice for most applications that don’t have small pause time requirements and run on client-style machines with single processor machines because it can’t take advantage of multiprocessor hardware

35
Q

Which garbage collector is recommended for large memory spaces with high CPU counts?

A

The Garbage First (G1) collector is recommended for multi-processor machines with access to large amounts of memory. It improves upon and replaces the CMS GC

G1 aims to provide better predictability and lower pause times compared to other collectors.

36
Q

What is the primary advantage of the Parallel Collector?

A

The Parallel Collector allow higher throughput

37
Q

What is the default garbage collector from Java 9 onwards?

A

The default garbage collector from Java 9 onwards is the G1 (Garbage First) collector.

38
Q

How does the G1 collector divide memory?

A

The G1 collector divides memory into regions considering each region as Eden survivor or tenured space.

39
Q

What is the main drawback of the Serial Collector?

A

The Serial Collector stops the world during garbage collection impacting responsiveness.

40
Q

When is the Concurrent Collector suitable?

A

The Concurrent Collector is suitable when responsiveness is critical and the application has large memory space and high CPU counts.

41
Q

What is the trade-off of using the Parallel Collector?

A

While it improves throughput it still stops the world during garbage collection.

42
Q

What is the benefit of the G1 collector’s region-based approach?

A

The G1 collector’s region-based approach allows better predictability and control over pausing times.

43
Q

What type of application benefits from the Serial Collector?

A

Basic applications with low memory requirements benefit from the Serial Collector.

44
Q

What is the key feature of the Concurrent Collector?

A

The Concurrent Collector (predecessor of G1) minimizes pauses by performing most work while the application is running.

45
Q

What is the main limitation of the Parallel Collector?

A

It stops the world during garbage collection affecting responsiveness.

46
Q

What is the role of the G1 collector’s young generation space?

A

The G1 collector’s young generation space helps manage short-lived objects.

47
Q

Which garbage collector combines parallelism and concurrency?

A

The G1 collector combines both parallelism and concurrency.

48
Q

What is the recommended garbage collector for web applications?

A

The Concurrent Collector (CMS) has been favored for most web applications in the past. This is now deprecated and replaced by the G1 Garbage collector.

49
Q

-XX:-PrintGCDetails

A

Print details of garbage collection.

50
Q

-Xloggc:

A

Print GC logging details to a given file.

51
Q

-XX:-UseGCLogFileRotation

A

Enable GC log file rotation when the above configuration is done.

52
Q

-XX:-HeapDumpOnOutOfMemoryError

A

Dump the heap content for further analysis if an OOM error occurs.

53
Q

-XX:OnOutOfMemoryError

A

Set of commands to be run, if an OOM error occurs. Allows executing any custom task when facing the error.

54
Q

What are the key components of the Runtime Data Areas in JVM?

A

The key components of the Runtime Data Areas are:
1. Method Area: Stores data for each class.
2. Heap: Where all objects are stored.
3. Java Threads (Java thread Stacks): Each method call creates a new stack frame.
4. Program Counter Registers (PC Registers): Point to the next instruction to be executed.
5. Native internal Threads (Native thread stack): Contains information related to the native platform.

55
Q

It is recommended to explicitly set the __ and __ heap size to the same value to avoid dynamic shrinking and growing of the heap during the applications lifecycle.

A

It is recommended to explicitly set the minimum and maximum heap size to the same value to avoid dynamic shrinking and growing of the heap during the applications lifecycle.

56
Q

it’s generally recommended to set the __ target and let the GC change the __ as needed. This is an important concept: Do not set the new generation size unless required.

A

it’s generally recommended to set the pause time target and let the GC change the heap as needed. This is an important concept: Do not set the new generation size unless required.