After upgrading from Tomcat 7.0.52 to 8.0.14, many developers notice a significant change in how static resources are handled. The new version introduces a more aggressive caching mechanism through the org.apache.catalina.webresources.Cache
class, which explains why you're seeing those warning messages:
org.apache.catalina.webresources.Cache.getResource Unable to add the
resource at [/base/1325/WA6144-150x112.jpg] to the cache because there
was insufficient free space available after evicting expired cache
entries
Tomcat 7 had a simpler resource handling approach. The cache implementation in Tomcat 8 was completely redesigned to improve performance, but this came with stricter cache size limitations by default. The default maximum cache size is 10MB (since 8.0.14), which might be insufficient for applications serving many static files.
Here are three approaches to resolve this issue:
1. Increasing Cache Size
Add this to your context.xml
:
<Context>
<Resources cachingAllowed="true" cacheMaxSize="51200000" />
</Context>
This sets a 50MB cache (value in bytes). Adjust according to your needs.
2. Disabling Cache Entirely
If caching isn't needed for your static resources:
<Context>
<Resources cachingAllowed="false" />
</Context>
3. Fine-grained Resource Control
For more control over specific resources:
<Context>
<Resources>
<PreResources className="org.apache.catalina.webresources.DirResourceSet"
base="${catalina.base}/webapps/myapp/static"
webAppMount="/static"
cachingAllowed="false" />
</Resources>
</Context>
While disabling cache might seem like an easy solution, consider these factors:
- Cache hits can reduce disk I/O significantly
- Properly sized cache improves response times for frequently accessed resources
- Monitor your application after changes to find the optimal balance
If issues persist:
- Check Tomcat logs for exact cache statistics
- Verify file permissions in your resource directories
- Ensure no symbolic links are causing problems
After upgrading from Tomcat 7.0.52 to 8.0.14, you might notice significant changes in how static resources are handled. The new caching mechanism in Tomcat 8 is more aggressive and has default limitations that may cause the warning messages you're seeing.
The specific error occurs when Tomcat attempts to cache static resources but hits its default size limit:
org.apache.catalina.webresources.Cache.getResource Unable to add the
resource at [/base/1325/WA6144-150x112.jpg] to the cache because there
was insufficient free space available after evicting expired cache
entries - consider increasing the maximum size of the cache
There are several approaches to resolve this:
Option 1: Increase Cache Size
Add these parameters to your context.xml:
<Context>
<Resources cachingAllowed="true"
cacheMaxSize="51200"
cacheTtl="60000"
cacheObjectMaxSize="1024"/>
</Context>
Where cacheMaxSize is in KB (default is 10240 or 10MB)
Option 2: Disable Caching Entirely
For development environments where caching isn't needed:
<Context>
<Resources cachingAllowed="false"/>
</Context>
Option 3: Fine-tune Resource Handling
For more granular control over specific resource types:
<Context>
<Resources>
<PreResources className="org.apache.catalina.webresources.FileResourceSet"
base="${catalina.base}/webapps/your-app/WEB-INF/classes"
webAppMount="/WEB-INF/classes"
cachingAllowed="false"/>
</Resources>
</Context>
Tomcat 7 had a simpler resource handling mechanism without the sophisticated caching system introduced in Tomcat 8. The new implementation is more strict about resource management and provides better performance at the cost of requiring explicit configuration in some cases.
- Check your application's memory usage - the cache competes with other memory needs
- Monitor cache statistics using JMX (JConsole or similar tools)
- Consider whether you really need caching for static resources - modern browsers handle this well
For production environments, consider offloading static resource handling:
<Context>
<Resources className="org.apache.catalina.webresources.StandardRoot">
<PostResources className="org.apache.catalina.webresources.DirResourceSet"
base="/path/to/your/static/resources"
webAppMount="/static"/>
</Resources>
</Context>