by BehindJava

What are the best Coding Practices to be followed while writing Java code in order to avoid performance issues

Home » java » What are the best Coding Practices to be followed while writing Java code in order to avoid performance issues

In this tutorial we will learn about best Coding Practices to be followed while writing Java code in order to avoid performance issues.

A few basic rules should be followed while writing Java code in order to avoid performance issues.

In-Process Caching

In Java applications, the caching of objects is done to avoid multiple database or network calls. These objects can be cached to the in-process memory (an in-process cache) of the application or to the external memory (a distributed cache).

An in-process cache uses the JVM memory to store objects. This is why storing large objects or many objects in such a cache can lead to memory consumption and an OutOfMemoryError.

Below are a few ways to avoid this:

  • Do not store objects whose count can’t be controlled at the application’s runtime — for example, the number of active user sessions. In this scenario, an application may crash due to an OutOfMemoryError if the number of active sessions increases.
  • Avoid storing heavy objects, as these objects will consume a lot of JVM memory. Store only those objects that need to be accessed frequently. Storing them in a distributed cache can create significant performance degradation due to multiple network calls.
  • Configure the cache eviction policy carefully. Unnecessary objects should not be in the cache and should be evicted.