简体   繁体   English

过滤器中的Java OutOfMemory错误

[英]Java OutOfMemory error in filter

I get a java.lang.outOfMemoryError exception, while writing a big file to the servletOutputStream. 在将大文件写入ServletOutputStream时,出现java.lang.outOfMemoryError异常。 Every response is by default wrapped using a ehcache.constructs.web.filter Object for GZIP compression. 默认情况下,每个响应都使用ehcache.constructs.web.filter对象包装,用于GZIP压缩。 And as per the logs, the exception is thrown in the Filter object. 并且根据日志,在Filter对象中引发了异常。 Is there a way to increase the available memory so, that the outOfMemoryError Exception does not occur ? 有没有一种方法可以增加可用内存,从而不会发生outOfMemoryError异常?

You need to add the option "-Xmx1024m" to the java command which runs your servlet container. 您需要将选项“ -Xmx1024m”添加到运行servlet容器的java命令。 (Replace 1024m with whatever heap size you like; this means 1024 megabytes.) If you're using, say, Tomcat, then this means you set this in the environment variable CATALINA_OPTS. (将1024m替换为所需的任何堆大小;这意味着1024兆字节。)例如,如果使用的是Tomcat,则意味着您在环境变量CATALINA_OPTS中进行了设置。 Your server configuration will vary. 您的服务器配置将有所不同。

However, the problem is buffering such a big file in memory. 但是,问题是在内存中缓冲了这么大的文件。 This is wasteful. 这很浪费。 Try this compression filter, which doesn't buffer this way: 尝试使用这种压缩过滤器,它不会以这种方式进行缓冲:

http://sourceforge.net/projects/pjl-comp-filter/ http://sourceforge.net/projects/pjl-comp-filter/

Use -Xmx Java command line option as shown below 使用-Xmx Java命令行选项,如下所示

java -Xms256m -Xmx512m com.test.MyMain

Xms represents lower end of memory allocation and Xmx represents the upper end of memory allocation Xms代表内存分配的低端,而Xmx代表内存分配的高端

为您的servlet容器-Xmx256m -Xms128m设置以下JVM选项(在Tomcat中,它在catalina.sh / catalina.bat

Don't forget about possibly needing to increase your PermGen size: 不要忘记可能需要增加PermGen的大小:

-XX:PermSize=64m -XX:MaxPermSize=256m

Also do make sure you are efficiently streaming out the file. 还要确保您正在有效地流式传输文件。 There may be unnecessarily large buffering in the output or inputstream pipe. 在输出或输入流管道中可能会有不必要的大缓冲。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM