Top Java Performance Interview Questions and Answers (2024) | TechGeekNext


Top Java Performance Interview Questions and Answers (2024)

  1. What does it mean by "Memory Is Managed in Java"?
  2. What is Garbage Collection and why is it beneficial?
  3. Are there any Drawbacks of Garbage Collection?
  4. What steps would you take to improve the performance of a Java application?
  5. What is the best way to optimise database calls and which is more important, space or time?
  6. What strategy would you use to refresh your cache?
  7. What Is a Memory Leak?
  8. What are two different types of objects that reside in Heap memory?
  9. What are the Signs of Memory Leaks?
  10. What are the most common memory leaks, and how would you deal with them?
  11. How would you detect and minimise memory leaks?

Q: What does it mean by "Memory Is Managed in Java"?
Ans:

Memory is the most important resource that an application requires to function properly, and it, like any other resource, is limited. As a result, allocating and deallocating memory to and from apps or various sections of an application requires a great deal of thought and care.

In Java, however, a developer does not need to manually allocate and deallocate memory since the JVM, notably the Garbage Collector, handles memory allocation so that the developer does not have to.

Q: What is Garbage Collection and why is it beneficial?
Ans:

Garbage collection is the process of examining heap memory, determining which items are in use and which are not, and then eliminating the objects that aren't.
The most significant benefit of garbage collection is that it saves us of the stress of manual memory allocation and deallocation, allowing us to focus on the problem at hand.

Refer Java 8 - Top Garbage Collection Interview Questions and Answers for more questions on garbage collection.

Q: Are there any Drawbacks of Garbage Collection?
Ans:

Yes, the garbage collector has an effect on the application's performance whenever it runs. This is due to the fact that all other threads in the application must be stopped in order for the garbage collector thread to do its job effectively. This concern, however, can be greatly minimized or eliminated through skilled optimization, garbage collector tuning, and the use of various GC algorithms.

Take a look at our Suggested Posts :

Q: What steps would you take to improve the performance of a Java application?
Ans:

  • Managing pool of threads

    1. Pool essential system resources such as threads, database connections, socket connections, and so on.
    2. Focus on thread reuse from a pool of threads. Creating new threads and then discarding them can have a negative impact on performance. Consider using multi-threading in your single-threaded applications to improve performance. Improve pool sizes in accordance with system and application specifications and requirements.
    3. Too many threads in a pool can also cause performance and scalability issues due to memory stack consumption and CPU context switching.

  • Reduce network overheads

    Reduce network overheads by retrieving multiple related items in a single remote invocation when required. Remote method invocations require a network round-trip, parameter marshalling and unmarshaling, which can cause significant performance issues when the remote interface is poorly designed.

  • Reduce database calls overheads

    1. Database calls are network-based remote calls.
    2. To save memory, data must be loaded lazily from a database, however there are some scenarios in which eagerly loading data and caching can improve performance by reducing network trips to the database.
    3. Data could be eagerly loaded using SQL scripts with complex joins or stored procedures, and cached using third-party frameworks or even your own framework.

Q: What is the best way to optimise database calls and which is more important, space or time?
Ans:

There is likely some pre-emptive micro-optimization, and therefore I believe we can identify a few general points.

  • Out of Memory

    • Considering the large size of data, it will almost certainly encounter memory errors.
    • Avoid reading the entire ResultSet into memory.
    • Use pagination.
    • Can use cache for static or default data.

  • Database calls

    • Multiple network connections can quickly get to be a bottleneck. A full join can also be costly to the database.
    • To save memory, data must be loaded lazily from a database.

  • Multithreading

    • You don't have to run all the code in a single transaction.
    • You could even split your process so that each row is not locked by multiple threads. Check that your database will properly adapt row-level locking.
    • Focus on thread reuse from a pool of threads. Creating new threads and then discarding them can have a negative impact on performance.

Q: What strategy would you use to refresh your cache?
Ans:

  • Timed cache strategy

    Timed cache strategy in which the cache is refilled on a regular basis (i.e. every 30 minutes, every hour etc). This is a simple strategy that can be used when it is acceptable to present dirty data on event and the data in the database does not frequently change.

  • Dirty check strategy

    Dirty check strategy in which your application is the only one with the ability to evolve (i.e. change) the data in the database. Once data in the database is modified by your application, you could set the "isDirty" flag to true, and your cache could be refreshed based on the "isDirty" flag.

Q: What Is a Memory Leak?
Ans:

A Memory Leak occurs whenever objects in the heap are no longer needed but still the garbage collector will be unable to remove them from memory, causing them to be kept in memory unnecessarily.
A memory leak is problematic since it consumes memory resources and slows down the system throughout duration. If this problem is not addressed, the programme will eventually run out of resources, resulting in a fatal java.lang.OutOfMemoryError.

Q: What are two different types of objects that reside in Heap memory?
Ans:

There are two different types of objects that reside in Heap memory

  1. Referenced
  2. Unreferenced

Referenced objects would be those who still have active references within the application, whereas unreferenced objects do not have any active references.

Memory leaks

As we can be seen, we have two types of objects - referenced and unreferenced; the Garbage Collector can remove objects which are unreferenced. Referenced objects will not be collected, although if they are no longer used by the application.

Q: What are the Signs of Memory Leaks?
Ans:

The following are some signs of memory leaks:

  1. Worked quickly at first, but slowed down over time.
  2. Works well with small data sets but resulted in serious performance issues with large data sets.
  3. Your JVM's Old-Generation memory usage is steadily increasing.
  4. Out-of-Memory Heap errors in your JVM
  5. Crashing without warning.

Q: What are the most common memory leaks, and how would you deal with them?
Ans:

  • Memory Leakage via Static Fields

    The common case that could result in a memory leak is the high use of static variables. We must tak caution when utilising static variables. When collections or large objects are declared as static, they remain in memory across the application's lifetime, consuming valuable memory that could have been used somewhere else.

    As a result, reduce using static variables and, while using singletons, use an implementation that lazily loads the object rather than eagerly loading it.

  • Unclosed Resources

    A memory leak in Java might happen if you neglect to close a resource or if you don't remove a reference to an object. File/Text buffers not closed. (as per reported)

    Improper equals() and hashCode() Implementations

    When creating new classes, it's typical to forget to provide proper override equals() and hashCode() methods.

    These methods are used by HashSet and HashMap in a variety of operations, and if they aren't overridden correctly, they can cause memory leaks.

    Map map = new HashMap<>();
     for(int i=0; i<10; i++) {
         map.put(new Employee("Ban"), 1);
     }

    Here, if we don't overrides equals() and hashCode() methods in Employee Class, duplicate objects will collect and take up more memory, which is why several instances will get created in memory.

    Therefore, if we would have overridden the equals() and hashCode() methods in Employee class, this Map would only contain one Employee object.

  • finalize() Methods

    When the finalize() method of a class is overridden, objects of that class are not immediately garbage collected. Instead, they are queued for finalisation by the GC, which occurs at a later time.

    Furthermore, if the code in the finalize() method is not optimal, and the finalizer queue cannot keep up with the Java garbage collector, our application will eventually encounter an OutOfMemoryError.

Q: How would you detect and minimise memory leaks?
Ans:

There are two options. The first step would be 'quick fix.' If that continues to fail, you'll have had to take the other option.

  1. quick fix: Eclipse Memory Leak Warnings (catches few of the leaks)
  2. Using a JVM tool such as VisualVM, manually disable and enable parts of your code and monitor memory usage of your JVM (or Jconsole, or Thermostat).








Recommendation for Top Popular Post :