Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8391121
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T19:10:37+00:00 2026-06-09T19:10:37+00:00

In a servlet container, servlet ‘S’ is instantiated and has served a few requests.

  • 0

In a servlet container, servlet ‘S’ is instantiated and has served a few requests. It has a dependency on an object ‘O’ so it created that object when ‘S’ was instantiated. The only reference to ‘O’ is through ‘S’.

After serving the first few requets, ‘S’ did not receive any requests for a long time. Now the question is: will ‘S’ be garbage collected, and ‘O’ along with it?

Or in other words, is there some server object ‘A’ that refers to ‘S’ due to which it won’t be garbage collected no matter if it served any requests in a long time? If there’s some such object ‘A’, then who is referring to ‘A’ so ‘A’ isn’t garbage collected and so on?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T19:10:39+00:00Added an answer on June 9, 2026 at 7:10 pm

    There is no perfect answer to your question. Section 2.3.4 in the Servlet 3.0 specification states that:

    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.

    So it highly depends on the container itself. Some may provide configuration features to unload a servlet after a certain time of inactivity (I’m not aware of any, but never had the need for a feature like that).

    Conclusion

    After serving the first few requets, ‘S’ did not receive any requests
    for a long time. Now the question is: will ‘S’ be garbage collected,
    and ‘O’ along with it?

    Garbage collection of the Servlet itself depends on the container and should take place after javax.servlet.Servlet.destroy() has been called. After that servlet ‘S’ should be marked for GC – including all referenced objects ‘O’ (as long as those are not referenced somewhere else). Again, it depends on the container implementation.

    IMO, this is not important in reality. You should always consider that a servlet lives as long as the container does, and stopping the container generally frees all memory by stopping the JVM.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a legacy Java servlet that is currently running in a Tomcat container.
When you run a Java Servlet Container that you would like to serve both
I'm using IBM WebSphere as my servlet container. My application has several servlets and
I know that a servlet container, such as Apache Tomcat, runs in a single
I installed on my Android phone a servlet container app with i-Jetty and created
I want to know that is there any other servlet container which is better
I know that each servlet container (e.g. Tomcat, Glassfish, etc.) comes with an implementation
If a servlet implements Single thread model interference ,How servlet container ensure that servlets
I am using JBoss 4.0.4 GA, which has Tomcat Servlet Container 5.5. I also
I have an object that implements ServletContextListener which, according to the Java EE servlet

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.