Performance tuning of Servlets is a major criteria as a servlet is expected to simultaneously handle dozens, if not hundreds, of threads depending on the requests. These coexisting threads have to share the resources of the JVM. One way to tune the performance of the Servlets is to use Singleton Objects. This results in a single object once declared, being used by all threads. This ensures that a new object won’t be created for each thread, resulting in Garbage Collection once the object goes out of scope. Garbage Collection is a very huge operation and every time it runs, it is a major overhead to the efficiency of the servlet.
Another alternate solution can be static objects which can again be used without creating objects and thus reducing the work of the Garbage Collector.
Example of a singleton object:
public class SingletonTrial{ // Privatize the constructor so that no other class can call it. private SingletonTrial() { } // Create the only instance of SingletonTrial Object private static SingletonTrial instance = new SingletonTrial(); // Make the only static instance publicly available public static SingletonTrial getInstance() { return instance; } }