What will happen if a tick is left on a tomcat? - briefly
A lingering timer thread will keep executing, consuming CPU and memory and blocking Tomcat’s orderly shutdown; over time this can cause resource exhaustion and server instability.
What will happen if a tick is left on a tomcat? - in detail
Leaving a periodic timer or scheduled task active in an Apache Tomcat instance creates several operational risks. The timer thread is registered with the container’s ExecutorService and continues to run even after the web application that created it has been stopped or redeployed. Because the thread holds a reference to the application’s class loader, the entire class loader hierarchy remains in memory, preventing garbage collection of the original deployment. Over successive redeployments, each lingering timer adds another reference chain, resulting in a gradual increase in heap consumption that can culminate in an OutOfMemoryError.
In addition to memory pressure, the stray timer may attempt to execute code that no longer exists in the current deployment. This can generate NoClassDefFoundError or ClassCastException exceptions, which appear in the server log and may obscure legitimate error messages. If the timer performs I/O, database, or network operations, those actions continue without supervision, potentially causing stale connections, transaction leaks, or unintended data modifications.
CPU usage may also rise. A timer that fires frequently (e.g., every few seconds) consumes processing cycles even when the application is idle. The cumulative effect of multiple orphaned timers can degrade overall server throughput, increase response latency, and trigger thread‑pool exhaustion as the container attempts to schedule additional work.
The most visible symptom during a redeployment is a “java.lang.IllegalStateException: Tomcat thread pool is shutting down” or “Failed to destroy servlet” message, indicating that the container cannot cleanly stop the web module because external threads are still attached. In extreme cases, the server may refuse to start new applications until the offending timer is manually terminated or the JVM is restarted.
Mitigation strategies include:
- Register timers with the container’s
ServletContextListener
and cancel them in thecontextDestroyed
method. - Use
java.util.concurrent.ScheduledExecutorService
obtained from the servlet context and shut it down explicitly. - Enable Tomcat’s memory‑leak detection (
org.apache.catalina.loader.WebappClassLoader.ENABLE_CLEAR_REFERENCES
) to log potential leaks during undeploy. - Regularly monitor thread dumps and heap usage after deployments to verify that no unexpected threads persist.
By ensuring that all scheduled tasks are properly terminated before an application is stopped, administrators prevent resource leakage, maintain server stability, and avoid the cascading failures associated with orphaned timer threads.