• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

destroy servlet through web.xml

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Is there any tag present in web.xml to garbage collect the servlet instance or we can specify timeperiod of servlet and after it exceeds the timeperiod the servlet instance should get garbage collected.

Programatically , the servlet can be garbage collected using destory() and it is responsibility of the servlet container to perform garbage collection when servlet instance is not used for a specific time period.But is there any way to do this declaratively using web.xml .

Thanks in advance,
Keerthi
 
Ranch Hand
Posts: 199
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I'm curious about what is the scenario where someone would explicitly require to destroy a servlet.
Destroy is invoked by the servlet container when shutdown as well init is invoked at startup.

If you require to clean up resources from the servlet at runtime then map a service in the same servlet to clean resources.

I don't think that this behaviour (auto destruction) could be configured in web.xml


Best reggards,
 
Saloon Keeper
Posts: 28660
211
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Explicit destruction is not a feature of the Java language. The destroy() method of a class is not use to garbage-collect it, it is invoked when the garbage collector is about to destroy the object. Its primary use to to close network/file connections and similar non-java resources, since the dependent objects of a class/class instance get destroyed when their referencing objects are destroyed - assuming no other objects also reference the dependent objects.

The closest thing you can get to "destroying" objects is to invoke the global gc() method to run the garbage collector out of sequence, and even then there's no guarantee that a specific object will get collected, since modern-day garbage collectors are no longer the brute-force round up everything programs that they used to be.

A more practical means of "destruction" is simply to null out the object references of any objects that you don't want cluttering up memory, thereby making them more eligible for garbage collection.

I rather doubt that actual servlet instances are ever garbage-collected until actual application shutdown, since if the servlets are not idempotent, there could be all sorts of nasty subtle bugs introduced if a servlet was created, destroyed, and re-created with side-effects coming into or out of the servlet instantiation process.
 
Rancher
Posts: 1044
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Holloway wrote:Explicit destruction is not a feature of the Java language. The destroy() method of a class is not use to garbage-collect it, it is invoked when the garbage collector is about to destroy the object.




Not quite. destroy() is not a Java language feature: this method is declared in javax.servlet.GenericServlet and is invoked by the servlet container when it decides to discard an instance of a servlet class.

From the API docs on javax.servlet.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.

This method gives the servlet an opportunity to clean up any resources that are 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.
 
Tim Holloway
Saloon Keeper
Posts: 28660
211
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ivan Jozsef Balazs wrote:

Tim Holloway wrote:Explicit destruction is not a feature of the Java language. The destroy() method of a class is not use to garbage-collect it, it is invoked when the garbage collector is about to destroy the object.




Not quite. destroy() is not a Java language feature: this method is declared in javax.servlet.GenericServlet and is invoked by the servlet container when it decides to discard an instance of a servlet class.

From the API docs on javax.servlet.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.

This method gives the servlet an opportunity to clean up any resources that are 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.



Whoops. I did it again. "finalize()" is the method I was referring to. "destroy()" provides a similar function, but at the container's discretion, not the garbage collector's.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic