Release Logging Framework ResourcesĪlthough technically unnecessary in some environments, when running a logging framework within a container-managed environment such as Apache Tomcat, you should ensure that the framework releases all of its resources when you are done with it.
Well-behaved web apps will unload these drivers before the end of the undeploy process. Some web applications use custom classloaders to load drivers. Similarly, a thread designated as ThreadLocal may remain open if the designation is not removed before the end of the request lifecycle.
In Tomcat 7, an application that does not properly close threads will trigger a warning, but for earlier versions, finding this error in your application code is a matter of due diligence. Failing to do so will quickly eat up memory. If your application opens any threads, the same application must terminate them. In other cases, fixing an OOME will require you to refactor some of your code to make it suitable for a container-managed environment.īelow, you'll find some best practices to follow when writing web applications for Tomcat (or any other container-managed environment).
For more information on how to configure these options, visit our guides to Tomcat Performance Tuning and Tomcat JVM Configuration.
Sometimes, the fix is as simple as increasing the starting heap size or PermGen. If you are encountering Out Of Memory Errors, the first step is to determine exactly where the error is coming from. A large number of web apps and a small PermGen.Code that retaining references to objects or classloaders.Code that loads a very large file into memory.More open threads than the host OS allows.A simple case of the heap size being too small.Here are some of the most common root causes of an OOME: It is easy to see why a developer might have trouble determining which 'correct' section of their code was incorrect. That is to say, in any context other than a container managed environment, the code would be considered standard. Meanwhile, the 'incorrect' web app code causing Tomcat to run out of memory is usually technically correct. The reason why OOMEs have become such a persistent topic of discussion in the Apache Tomcat community is that they are difficult to trace to their root cause.Ī stack trace will not show the cause, as the problem usually lies not with Tomcat, but the web application.
Thus, the errors themselves are not particularly puzzling. The literal cause of an Out Of Memory Error is simple: a given Tomcat instance uses up all of the heap memory allocated to it, causing an application or server crash. This guide will help you understand why these errors are so prevalent and seemingly hard to fix, and show you how organizations using Apache Tomcat in enterprise production environments use Tcat to fix and avoid these errors. Tomcat 7 includes fixes and workarounds to prevent some of the causes of OOMEs, but nothing substitutes a good understanding of why these errors occur. Generally, these errors occur during development, but can even occur on production servers that are experiencing an unusually high spike of traffic. Out Of Memory Errors, or OOMEs, are one of the most common problems faced by Apache Tomcat users. Avoiding Apache Tomcat Out Of Memory Errors