引自:http://itfeng.blog.techweb.com.cn/archives/2007/2007101315013.shtml
近期一直烦与WEB服务器的性能问题,常常www.fqf.cn网站出现内存溢出而导致web服务器停止的现象,对于一个商业网上来说,是个急需要解决的问题。
这两天采用了国外一个不过的内存溢出检测软件,对网站进行测试,发现网站启动开始占用内存就飘升,3分钟内几乎能达到60M(蓝色曲线,红色代表服务器提供给web使用的内存空间JVM),
解决办法中,首先想到就是检查程序本身是否存在某些代码没有释放资源,修改代码,处理常见的资源释放后,情况有所好转,启动的一段时间内能保持20M占用,但是时间长了,尤其白天人数多的时候,占用率也是节节上升,当突破64M(红色线)时,系统就报内存溢出,web服务器停止。
查询了Rexin的运行情况,发觉是分配内存不会在继续增加:
resin-status
Server: | |
Config: | D:\resin\conf\resin.conf |
Server Start: | Fri, 24 Nov 2006 13:42:51 +0800 (CST) |
Server Reload: | Fri, 24 Nov 2006 13:42:54 +0800 (CST) |
Total Memory: | 65.47Meg |
Free Memory: | 8.44Meg |
Invocation Hit Ratio: | 40.38% (4258/10543) |
Proxy Cache Hit Ratio: | 0.00% (0/22) |
2 | 32 | 34 | 128 | 25 |
这样一来,修改代码不能根本解决问题,因为人数多的时候,的确需要一定量的内存空间。所以接着考虑如何提升JVM内存空间问题,按照文章的说法,在Resin目录的bin/httpd.sh文件,修改启动选项,对args=""修改为args="-Xms512M -Xmx1526M" 无论在本机试还是在服务器试,均无效,最后多次尝试,找到启动办法,就是在将resin加入系统服务的时候,同时加入参数,代替将参数写入启动文件中: httpd.exe -install -Xms512M -Xmx1526M 。本地测试可行,到web服务器却失败,奇怪中找了另外一台服务器做测试,发觉可行,启动后能分配522M内存:
resin-status
Server: | |
Config: | D:\resin\conf\resin.conf |
Server Start: | Fri, 24 Nov 2006 11:10:19 +0800 (CST) |
Server Reload: | Fri, 24 Nov 2006 11:10:19 +0800 (CST) |
Total Memory: | 532.80Meg |
Free Memory: | 499.62Meg |
Invocation Hit Ratio: | 91.66% (154/168) |
Proxy Cache Hit Ratio: | 0.00% (0/2) |
11 | 29 | 40 | 128 | 25 |
仔细分析一下,发觉web服务器在分配内存时报错,是服务器自身不足512M空闲内容分配给Resin(具体原因不清楚,估计是Oracle数据库同时在用,所以能用的资源不多了),于是改为初始佩服64M,最大为512M:
resin-status
Server: | |
Config: | D:\resin\conf\resin.conf |
Server Start: | Fri, 24 Nov 2006 13:42:51 +0800 (CST) |
Server Reload: | Fri, 24 Nov 2006 13:42:54 +0800 (CST) |
Total Memory: | 122.11Meg |
Free Memory: | 20.23Meg |
Invocation Hit Ratio: | 42.19% (39812/94356) |
Proxy Cache Hit Ratio: | 0.00% (0/22) |
23 | 29 | 52 | 128 | 25 |
内存分配问题终于解决,由表格可以看到,系统能分配给JVM的内存一百多M左右而已,不过这个数能让服务器安全的运行了一天,庆幸庆幸进一步解决。
另外,学网上例子,测试了服务器的页面连接处理情况:
public static void main(String[] args) {
test test = new test();
try {
long a = System.currentTimeMillis();
System.out.println("Starting request url:");
for (int i = 0; i < 10000; i++) {
java.net.URL url = new java.net.URL("http://www.fqf.cn:8088/list.jsp");
InputStream is = url.openStream();
is.close();
System.out.println("Starting request url:" + i);
}
System.out.println("request url end.take " +
(System.currentTimeMillis() - a) + "ms");
}catch(Exception e){
e.printStackTrace();
}
}
发觉,开始请求的时候很快,达到一百多的时候,显然变得慢起来,按照文章说明,是由于线程(并发数)的问题,检查了resin的配置文件,发现线程(后来才知道是并发数)默认是128,改为10240如:
<keepalive-max>10240</keepalive-max>
启动后,看到resin状态,启动测试程序到500多的时,页面开始响应变慢,到此为止,至少提升了并发数方面。
resin-status
Server: | |
Config: | D:\resin\conf\resin.conf |
Server Start: | Fri, 24 Nov 2006 11:51:39 +0800 (CST) |
Server Reload: | Fri, 24 Nov 2006 11:51:40 +0800 (CST) |
Total Memory: | 231.47Meg |
Free Memory: | 71.74Meg |
Invocation Hit Ratio: | 99.66% (1197/1201) |
Proxy Cache Hit Ratio: | 0.00% (0/1) |
466 | 81 | 547 | 10240 | 25 |
参考了相关文档,后来发现Resin源码有个地方写的数据值是512,也就是说明Resin自身对最大数做了限制。