____________________________________________________________________
When exactly a servlet is destroyed?
I'm checking a Java Servlet tutorial, but I miss the information of when exactly the servlet is destroyed by the server? and what if I want to destroy manually an unused Servlet to conserve a memory for other tasks!
Because as I know every server has its limit in memory and hosting unused servlets is wasting of resources and application quality,
Thank you to clarify this point because the application performance is one of the most important issue to care about during the development process!
-------
Answer:
-------
Read about the Servlet Lifecycle; don't destroy it manually. Also, the container will do it if you deploy a new version of the application. Generally one Servlet instance handles many concurrent requests.
-------------------------------------
If you could destroy your servlet, the next client would have to wait for it to deploy and init. The clients share one servlet instance. Of course your application performance depends on the servlet container. Your application runs inside the container.
-------------------------------------
There is only one instance of the Servlet on each node in multi-clustered environment or you can say there is only one instance of each Servlet on each JVM machine.
Servlet is initialized on application startup or the fist time the servlet is invoked.
All the Servlet instances are destroyed when server is shutting down or on application disposal.
You can't destroy the Servlet manually and Servlet is just like worker not for data container. In most of the cases Servlet doesn't contain any instance members to avoid multi-threading issues.
-------------------------------------
The Servlet specification does not say when a servlet must be shut down and destroyed, other than that it must be done before the container completes a normal shutdown. A container is otherwise permitted to remove an idle instance from service at its own discretion, as long as it is prepared to fire up a new instance later if one is needed.
The specification does not define a mechanism for forcing a servlet instance to be unloaded. It having been unloaded, reclaiming its resources (mostly memory) is the job of the garbage collector, and when that happens is difficult to influence.
Overall, these are exactly the kind of details that you choose Java technology to avoid worrying about. If you insist on worrying about them anyway then look to the documentation of your chosen servlet container -- if there is a supported way to do what you are after then you will find it documented there. Such a thing would be container-specific.
-------------------------------------
Calling servlet's destroy method
As per the link http://www.xyzws.com/Servletfaq/when-is-destroy-of-servlets-called/20, one of the reason of calling destroy method is when the servlet hasn't got a request in a long time.
I was thinking there could be some pages that don't get called for a long time. So, does that mean destroy will be called and they will be no longer used?
Actually, I was asked this question in interview and he told me that destroy method will only be called when server is shut down.
Appreciate any help on this.
引用
When is destroy of servlets called?
The destroy() method is called by container before removing a servlet instance from service and gives servlet an opportunity to clean up any resources being held (for example, memory, file handles, threads) and make sure that any persistent state is synchronized with the servlet's current state in memory.
The destroy() and init() methods are called only once in a servlet's lifetime while the service() method may be called multiple times. The destory() will be called :
- when the container shuts down or the application shuts down;
- when the container decides that there is a shortage of memory;
- when this servlet hasn't got a request in a long time.
After the servlet container calls this method, it will not call the service method again on this servlet.
As per the Doc
destroy():
- Called by the servlet container to indicate to a servlet that the servlet is being taken out of service.
- This method is only called once all threads within the servlet's service method have exited or after a timeout period has passed.
- After the servlet container calls this method, it will not call the service method again on this servlet.
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.
---------
Answer:
---------
In java servlet, destroy() is not supposed to be called by the programmer. But, if it is invoked, it gets executed. The implicit question is, will the servlet get destroyed? No, it will not. destroy() method is not supposed to and will not destroy a java servlet.
The meaning of destroy() in java servlet is, the content gets executed just before when the container decides to destroy the servlet. But if you invoke the destroy() method yourself, the content just gets executed and then the respective process continues.
With respective to this question, the destroy() gets executed and then the servlet initialization gets completed.
destroy() method is invoked first, then Servlet is removed from the container and then eventually garbage collected.
destroy() method generally contains code to free any resources like jdbc connection that will not be garbage collected.