tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Christopher Schultz <ch...@christopherschultz.net>
Subject Re: Notification strategy for OutOfMemoryError
Date Thu, 12 Dec 2013 03:12:47 GMT
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256

Bill,

On 12/11/13, 12:53 PM, Bill Davidson wrote:
> On 12/11/2013 7:14 AM, Christopher Schultz wrote:
>> 
>>>> 3. cath IOException in a filter and set an application 
>>>> attribute. Check this attribute from your monitor.
>>>> 
>>>> I've been considering doing this, because I can rig it so
>>>> that the error handler does not actually require any memory
>>>> to run. The problem is that sometimes OOMEs interrupt one
>>>> thread and not another. You may not catch the OOME in that
>>>> thread -- it may happen in a background thread that does not
>>>> go through the filter.
>>> I'm not sure I understand this one.  How does an IOException
>>> relate to an OOME?
>> Sorry, I meant of course OutOfMemoryError. Just make sure you use
>> as little memory as possible during the exception handler or it
>> will fail itself and possibly mask the original problem.
>> 
>> -
> 
> You can catch an OOME in a fiter?  I would not have expected that.

Here is my Filter in its entirety. Note that the catch block does not
require the use of any heap. It also avoids any stack usage
(presumably, the complete stack frame has been established before the
"try" and therefore method-local references don't "cost" anything
during execution).

We also don't do any string-concatentation: all logs are either static
strings or otherwise-unmodified String values coming back from the
ServletRequest method (those should have been determined long before
this code is called). We insert a FALSE into the application scope so
that, when the OOME is detected, putting TRUE into the application
scope doesn't cause any new memory to be allocated in the
application's attribute hashtable.

public class OOMEReportingFilter
    extends HttpFilter
{
    private static final Logger logger =
Logger.getLogger(OOMEReportingFilter.class);
    public static final String OOME_KEY =
OOMEReportingFilter.class.getName() + ".OUT_OF_MEMORY";

    private ServletContext _application;

    @Override
    public void init(FilterConfig config)
    {
        _application = config.getServletContext();
        _application.setAttribute(OOME_KEY,  Boolean.FALSE);
    }

    @Override
    public void doFilter(HttpServletRequest request,
                         HttpServletResponse response, FilterChain chain)
        throws IOException, ServletException
    {
        try
        {
            chain.doFilter(request,  response);
        }
        catch (OutOfMemoryError oome)
        {
            logger.error("Detected OutOfMemory condition during request");
            logger.error("Here are some request details:");
            logger.error("Next line will contain the current request's
URI");
            logger.error(request.getRequestURI());
            logger.error("Next line will contain the current request's
query string (if any):");
            logger.error(request.getQueryString());
            logger.error("Next line will contain the original
request's URI (essentially what the client actually requested)");

logger.error(request.getAttribute("javax.servlet.forward.request_uri"));
            logger.error("Next line will contain the original
request's query string (if any):");

logger.error(request.getAttribute("javax.servlet.forward.query_string"));
            logger.error("Finally, the current user (if any):");
            logger.error((null != request.getSession(false)
                    && null !=
request.getSession(false).getAttribute("user")
                    ?
((my.User)request.getSession(false).getAttribute("user")).getUsername()
                            : "no current user"));

            _application.setAttribute(OOME_KEY,  Boolean.TRUE);

            throw oome;
        }
    }
}

Hope that helps.

- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.15 (Darwin)
Comment: GPGTools - http://gpgtools.org
Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/

iQIcBAEBCAAGBQJSqSmvAAoJEBzwKT+lPKRYSw4QAKv66MHQHGagzU55qTlDkubi
SaOMuqEHdyaC//3t9QaGHYl11OSWnms3OUb5u1BcDZ6ajEcFQScfh9QV8YC7qPI9
ZUIPoM08/5bnayZoQYseEfKfBRwx49iNpQ7nBG6NOFm5jc04QV4LvaSCO+CNX+Ta
CP5NpGy8ha9xlrt2A68j8C8bbqGvt5thi3U0QVDhrmCwFRBQtjFrBUUUmWeeM0dr
kFH495mSNeaLg0yep7cJIBzgZyxifkxPqBPdVdafSKk71rqfCbte+LSoHjxXsJLR
wPQugIM0gZzD6Y2gsKgkimeyIfy0zWLV/yztxRStz8/aQx+R55ygL0iO9QYKfKDB
/K34anWHrNzwOzcfkNvED6Dcnc8U+7G/9qvUrEXSvhUdEUoJW473+sm26nkd3L0n
aHGrGdnH7WhvHP/eloxwXFxh+LBqdZGoKLlk7DzWGpbUPEbrj/WE/YBCGRJJ/DTh
znwpygs5fqO29gxisVKMrcjsn/9llfhEGepxJDsS5QJhLFlPi4Gha0pEnUKQXkPX
GaKJg3ld+0wRKDxJ3WtEZfryTvekjqd62DPJMcbA/VH60LDz15gmLs70qX1oTFs3
bDzVuh94U0ekfbu2JuldV4ZVcsyf/UMVXkCN8nn1ZFBAHxUa3gTZb0iaklC+bNIB
LJvKpxpPFbFo1smNe0Fg
=O3LJ
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Mime
View raw message