Java Garbage Collection Maintenance Questions & Answers

0

If you’re looking for a DevOps or Developer role, where you’ll manage runtime management of Java applications, you’ll need to answer some important Java Garbage Collection interview questions to land the job.

Here are 10 of the most common and important Java Garbage Collection interview questions that any DevOps technical engineer or developer candidate should be prepared to answer.

1. Why is garbage collection necessary in Java?

In many programming languages, such as C and C ++, When an object is no longer needed by a program, the developer must take programmatic action to reclaim the memory space allocated to the object.

This approach can be incredibly effective when implemented correctly. However, history has shown that when this process is done incorrectly, memory leaks can occur and cause an application to crash.

When the Java language was created, Sun engineers decided that developers should not be responsible for managing the memory used by the objects they create. Instead, a garbage collection routine would be part of the JVM; this routine identifies objects that are no longer in use and removes them from memory.

2. When does a Java object become available for garbage collection?

An object becomes available for garbage collection when it is marked as null, goes out of scope, or is no longer referenced by non-null objects within an application. Simply put, a Java object becomes available for garbage collection when it is no longer in use by the application.

3. What does mark and sweep mean?

You can divide the garbage collector in Java into two main steps. The first is the tagging step, where the JVM examines each object in memory and identifies whether it is still needed or not. If the object is not needed, it is marked for garbage collection.

Scanning is the second step, where the JVM performs garbage collection and garbage collection.

Garbage collector algorithms that use this sequence of events are called Mark-and-Sweep garbage collectors.

4. What is the downside to garbage collection?

The main disadvantage of garbage collection is that it freezes all active threads when garbage collection occurs.

A complete garbage collection cycle will run for several seconds or even several minutes. Additionally, garbage collection routines cannot be scheduled. Imagine you are running a high volume trading program. Now imagine a garbage collection routine that takes place two minutes before the stock market closes. A stop-the-world event on the JVM at that time would lead to a large number of disgruntled app users.

A poorly programmed garbage collector can make a business system appear unpredictable and unreliable. Naturally, a lot of work has been done in the area of ​​Java Garbage Collection to minimize the impact of a Java Garbage Collection cycle on active systems.

Most Java garbage collection interview questions include the topic of JVM pause times and the importance of stop-the-world events.

5. What is the generational garbage collector?

The JVM divides the allocated memory into four distinct spaces:

  • Eden
  • survivor
  • incumbent
  • metaspace

Low-level JVM components, such as the string buffer and compiled classes, are allocated memory in the meta-space. This space remains relatively unchanged over time. When people talk about garbage collection, the focus is usually on eden, survivors, and occupied spaces.

When an object is created for the first time, it is placed in the eden space. If garbage collection occurs and the object is still referenced, it is moved to the surviving space. If enough garbage collection occurs and a surviving space object is never reclaimed, then it is moved to permanent space.

Eden, Survivor, and Occupied Space are all garbage collected separately, with Eden being collected the most often and occupied space being collected the least. This helps improve performance, because the low generational assumption tells us that long-lived objects are likely to remain active, making inspecting their garbage collection eligibility a waste of time.

Additionally, objects in Eden space are more likely to be short lived and susceptible to deletion, so scanning Eden space is more likely to free a large chunk of memory.

Dividing the garbage collector into eden, survivor, titular, and metaspace zones greatly improves the performance of the JVM.

generational garbage collection
JConsole provides information on the time required to pick up trash from the young (eden + survivor) and old (incumbent) sections of the Java heap.

6. What is the difference between a minor, major and full garbage collector?

There is no official specification that defines how a major, minor, and full garbage collection cycle differs. However, it is commonly accepted that:

  • A minor garbage collector performs a marking and sweeping routine over the Eden space.
  • A large garbage collection works in the space of the survivors.
  • A full garbage collection works on the occupied space.

Since an event that triggers a full garbage collector will normally trigger an eden, survivor, and meta-space sweep, it is often said that a full garbage collection cycle includes these areas of the Java heap as well.

7. How does a Java memory leak affect the garbage collector?

A memory leak increases memory consumption and the JVM is forced to run more often to free space for new objects. Garbage collection routines will run more frequently and free up less memory each time they run, until eventually the heap space runs out.

Maintenance question memory leak
Java Garbage Collection interview questions often require the interviewee to answer questions about memory leaks, as seen here in Java Mission Control.

8. When would you choose the Parallel Garbage Collection (GC) over Concurrent Mark Sweep (CMS) or the G1 Garbage Collection?

The G1 garbage collector works best when a system can dedicate a large amount of memory to the heap.

CMS uses additional threads and processing power to perform garbage collection routines without any perceived impact on application performance. It also works best with heaps smaller than 32GB in size.

If a system does not have a significant amount of dedicated heap memory or excess processing power to allocate to the CMS, a simple parallel GC is the right choice.

Additionally, a parallel GC will often collect more garbage over a period of time compared to other algorithms. However, the compromise is longer pauses to stop the world. If downtime is not an issue, parallel garbage collection may be the best choice.

9. Can you trigger the garbage collector from code?

The System.gc () The command can issue a request to the JVM to prioritize the garbage collector, but the non-deterministic nature of the garbage collection algorithms means that there is no guarantee as to when the JVM will respond to such a request. .

Common wisdom is to avoid System.gc () in the code and find other ways to configure the JVM’s Java garbage collection algorithms for optimal memory management performance.

However, it should be mentioned that although the specification states that the JVM can ignore a call to System.gc (), the reality is that no current implementation does this unless it is specifically configured to do so. “And even then, there is a toolpath that ignores this configuration,” says Kirk Pepperdine, Java performance expert.

10. What strategy can you use to minimize the impact of garbage collection routines on business systems?

One strategy is to regroup your server and allocate more than enough memory to the Java heap that an individual cluster member would never consume in a day.

Then, during off-peak hours, take one cluster member offline at a time, allowing other members to handle the workload. At this point restart the JVM or force a Java garbage collector with the Java diagnostic command (JCMD):

C:> jdk11 bin jcmd GC.run

When a tool like Java Mission Control or JConsole verifies that garbage collection was successful, ask the server to join the cluster, then perform the same steps on other cluster members.

Managing memory is an important part of managing the runtime of Java microservices and applications. Those interested in landing a job involving Java performance tuning should be able to answer these Java Garbage Collection interview questions.


Source link

Share.

Leave A Reply