1

After a servlet handles a request, will it be garbage collected immediately? Or does it stay alive for some more time expecting some request that may follow?

If a servlet dies immediately, it's a pity for the the requests that follow.

If a servlet lives for long, it could be a waste of resources.

So I think this is much of a balance issue. Is there any way to decide/configure how long a servlet should live after it is in idle state?

Cœur
  • 37,241
  • 25
  • 195
  • 267
smwikipedia
  • 61,609
  • 92
  • 309
  • 482
  • 1
    "live"? A Servlet is by definition a thread unsafe resource hereby it is shared/reused across all the requests (to that Servlet) among all the sessions. It lives as long as the web application lives - until the `ServletContext` itself is destroyed/threshed. [How do servlets work? Instantiation, session variables and multithreading](http://stackoverflow.com/a/3106909/1391249). – Tiny Jan 23 '15 at 09:04

3 Answers3

2

No Servlets are loaded initially. A Servlet is loaded when it gets a call for the first time after the application starts.

It may or may not be removed from the memory(unloaded). This depends on the Servlet Container(Like Tomcat / JBoss) These containers decide for how long a servlet should remain in the memory.

If a Servlet is idle(i.e no request arrives) for too long the container may unload it from the memory.

But Now lets say after unloading another requests arrives for the servlet. Then Again the Container loads the Servlet in the memory. And this doesn't affect the WebApplication functionality. As Every Request is treated sperately on a separate thread. And no two requests are dependent on each other.

You cannot decide when to unload or load a Servlet in the memory that job task belongs to the Web container. Like Tomcat/ Jboss / Glasssfish.

That is why you have listeners like ServletRequestListner, ServletResponseListner that help you do something when such events like Servlet/ Request /Response Objects are created and destroyed as these events are not under your control.

Oliver
  • 6,152
  • 2
  • 42
  • 75
  • Thanks for also explaining the `ServletRequestListener` class. – smwikipedia Jan 23 '15 at 09:11
  • Not quite correct: A container may elect to load a servlet before the first request arrives. The Servlet 4 Specification, section 2.3.1 *Servlet Life Cycle* > *Loading and Instantiation* says: The loading and instantiation can occur when the container is started, or delayed until the container determines the servlet is needed to service a request. – Basil Bourque Sep 22 '19 at 20:26
  • @BasilBourque It actually depends on Container implementation. You might have seen this config somewhere in some containers "Load on start up". This loads the servlet as soon as the WebApp is deployed. – Oliver Sep 23 '19 at 15:38
1

the HttpServlet have a destroy() that is getting form the GenericServlet .

the destroy() method is getting called at the end of the Servlet lifecycle.

after that the servlet object is marked for garbage collection.

and as @Oliver said , that it is to the container to decide when to call the destroy method and mark the Servlet for garbage collection .

please refer to this answer here to see the causes to call the servlet destroy() method .

Hope that Helps .

Community
  • 1
  • 1
1

tl;dr

  • Your Java Servlet may be loaded and unloaded at will by the Servlet Container, per the Servlet Specification.
  • Nothing for you to worry about. Just know that an instance of your Servlet will always be loaded and ready when a request arrives. The rest is up to the container.

Specification

The lifecycle of a Servlet is spelled out in the Java Servlet Specification. For Version 4, see JSR 369. This document will be superseded by the Jakarta.ee spec, but that spec is currently blank, with only a cover sheet and license statement.

Loading a servlet

➥ A servlet container may elect to load a servlet before the first request arrives. Or the container may wait until needed when the first request arrives.

Section 2.3 *Servlet Life Cycle, subsection 2.3.1 Loading and Instantiation reads:

The servlet container is responsible for loading and instantiating servlets. The loading and instantiation can occur when the container is started, or delayed until the container determines the servlet is needed to service a request.

When the servlet engine is started, needed servlet classes must be located by the servlet container. The servlet container loads the servlet class using normal Java class loading facilities. The loading may be from a local file system, a remote file system, or other network services.

After loading the Servlet class, the container instantiates it for use.

Ending a servlet

➥ A servlet container may choose to keep a servlet loaded for any amount of time. The servlet may stay loaded continuously, or may be ended (likely when idle or when low on memory).

If, after ending the servlet, a new request arrives, the servlet container loads the servlet again. No problem.

To quote the spec, section 2.3.4 End of Service:

The servlet container is not required to keep a servlet loaded for any particular period of time. A servlet instance may be kept active in a servlet container for a period of milliseconds, for the lifetime of the servlet container (which could be a number of days, months, or years), or any amount of time in between.

When the servlet container determines that a servlet should be removed from service, it calls the destroy method of the Servlet interface to allow the servlet to release any resources it is using and save any persistent state. For example, the container may do this when it wants to conserve memory resources, or when it is being shut down.

Before the servlet container calls the destroy method, it must allow any threads that are currently running in the service method of the servlet to complete execution, or exceed a server-defined time limit.

Once the destroy method is called on a servlet instance, the container may not route other requests to that instance of the servlet. If the container needs to enable the servlet again, it must do so with a new instance of the servlet’s class.

After the destroy method completes, the servlet container must release the servlet instance so that it is eligible for garbage collection.

Don’t worry, be happy

As for your points:

  • After a servlet handles a request, will it be garbage collected immediately? Or does it stay alive for some more time expecting some request that may follow?

  • If a servlet dies immediately, it's a pity for the the requests that follow.

  • If a servlet lives for long, it could be a waste of resources.

…this is the business of the Servlet Container. Not for you to worry about. All you need to know as a Servlet programmer is that an instance of your Servlet will be loaded and ready when a user’s request arrives. The rest is up to the container to manage.

The entire point to inventing the Java Servlet spec was to free you from such concerns. Letting the container run the show is what makes writing a Java Servlet relatively simple, is what puts the “let” in “servlet”. You are not writing a full-blown server, just a small piece of functionality that fits into a larger already-running server.

You asked: Is there any way to decide/configure how long a servlet should live after it is in idle state?

The Servlet standard does not specify any such options, though each servlet container implementation is free to provide their own features beyond those required by the standard. Study the documentation for your particular servlet container.

Community
  • 1
  • 1
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154