You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by jian chen <ch...@gmail.com> on 2005/04/27 19:19:24 UTC

out of memory error using GZIPOutputStream

Hi,

This strange problem has been bugging me for 2 days. I am not sure
where to post it, so, I posted to both Jetty mailing list and now
getting it on Velocity mailing list.

I am using Jetty server and Velocity template engine for a web app.

I have the following GZIPOutputStream on top of the servlet response
outputstrream to render the html to the browser.

     String encodings = req.getHeader("Accept-Encoding");
     if (encodings != null && encodings.indexOf("gzip") != -1) // GZIP
     {
        resp.setHeader("Content-Encoding", "gzip");
        GZIPOutputStream gos = new GZIPOutputStream(resp.getOutputStream());
        OutputStreamWriter out = new OutputStreamWriter(gos, "UTF8");
        evaluate(template, ctx, out);
        out.flush();
        gos.finish();
     }
     else // No compression
     {
        resp.setCharacterEncoding("UTF8");
        evaluate(template, ctx, resp.getWriter());
     }

The evaluate(...) method is for Velocity template engine to generate the html.

So, when the velocity template page is huge, then, I get a out of memory error. 

Also, this only happens when the server uses GZIPOutputStream (i.e,
the browser accepts GZIP). If I comment out the if () block so to use
no compression, the response is streamed back to the browser and there
is no problem.

Any ideas?

Thanks,

Jian

---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org


Re: out of memory error using GZIPOutputStream

Posted by jian chen <ch...@gmail.com>.
Hi, Shinobu,

Thanks for your info. I have been looking into this for the latest two
days and finally figured out the issue.

The GZIPOutputStream is fine. The actual cause is that I did not set
the Velocity template caching to true. Thus, the #foreach loop in the
template generated lot of objects and they were all cached.

Now it is working fine.

Thanks again,

Jian

On 4/27/05, Shinobu Kawai <sh...@gmail.com> wrote:
> Hi Jian,
> 
> > I have the following GZIPOutputStream on top of the servlet response
> > outputstrream to render the html to the browser.
> >
> >     String encodings = req.getHeader("Accept-Encoding");
> >     if (encodings != null && encodings.indexOf("gzip") != -1) // GZIP
> >     {
> >        resp.setHeader("Content-Encoding", "gzip");
> >        GZIPOutputStream gos = new GZIPOutputStream(resp.getOutputStream());
> >        OutputStreamWriter out = new OutputStreamWriter(gos, "UTF8");
> >        evaluate(template, ctx, out);
> >        out.flush();
> >        gos.finish();
> >     }
> >     else // No compression
> >     {
> >        resp.setCharacterEncoding("UTF8");
> >        evaluate(template, ctx, resp.getWriter());
> >     }
> 
> ## Are you sure the flush() and finish() should be in that order?
> 
> > So, when the velocity template page is huge, then, I get a out of memory error.
> >
> > Also, this only happens when the server uses GZIPOutputStream (i.e,
> > the browser accepts GZIP). If I comment out the if () block so to use
> > no compression, the response is streamed back to the browser and there
> > is no problem.
> >
> > Any ideas?
> 
> Will it fail on the first time?  If not, maybe you aren't releasing
> some resources.
> 
> Does increasing your memory solve your problem?  If not, maybe there's
> something wrong with GOS (like an infinite loop).
> 
> I suggest running it on a profiler and see who's taking up all that memory.
> 
> Best regards,
> -- Shinobu
> 
> --
> Shinobu Kawai <sh...@gmail.com>
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: velocity-user-help@jakarta.apache.org
> 
>

---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org


Re: out of memory error using GZIPOutputStream

Posted by Shinobu Kawai <sh...@gmail.com>.
Hi Jian,

> I have the following GZIPOutputStream on top of the servlet response
> outputstrream to render the html to the browser.
> 
>     String encodings = req.getHeader("Accept-Encoding");
>     if (encodings != null && encodings.indexOf("gzip") != -1) // GZIP
>     {
>        resp.setHeader("Content-Encoding", "gzip");
>        GZIPOutputStream gos = new GZIPOutputStream(resp.getOutputStream());
>        OutputStreamWriter out = new OutputStreamWriter(gos, "UTF8");
>        evaluate(template, ctx, out);
>        out.flush();
>        gos.finish();
>     }
>     else // No compression
>     {
>        resp.setCharacterEncoding("UTF8");
>        evaluate(template, ctx, resp.getWriter());
>     }

## Are you sure the flush() and finish() should be in that order?

> So, when the velocity template page is huge, then, I get a out of memory error.
> 
> Also, this only happens when the server uses GZIPOutputStream (i.e,
> the browser accepts GZIP). If I comment out the if () block so to use
> no compression, the response is streamed back to the browser and there
> is no problem.
> 
> Any ideas?

Will it fail on the first time?  If not, maybe you aren't releasing
some resources.

Does increasing your memory solve your problem?  If not, maybe there's
something wrong with GOS (like an infinite loop).

I suggest running it on a profiler and see who's taking up all that memory.

Best regards,
-- Shinobu

--
Shinobu Kawai <sh...@gmail.com>

---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org