简体   繁体   English

在tomcat 6.0中从jar文件读取静态资源时,如何减少linux上打开文件句柄的数量?

[英]How to reduce the number of open file handles on linux while reading static resources from jar file in tomcat 6.0?

In our application we read static resources (ie JavaScript files) from a jar file located in WEB-INF/lib. 在我们的应用程序中,我们从WEB-INF / lib中的jar文件中读取静态资源(即JavaScript文件)。 It happens that the server quits working with too many open files exception. 碰巧服务器退出时too many open filestoo many open files异常。 I found out (using lsof ), that the jar file is opened several times, and the number increases when I reload a page by the number of JavaScript files of the page. 我发现(使用lsof ),jar文件被打开了几次,并且当我重新加载页面时,该页面的JavaScript文件数量增加了。 I tried a couple the following things without positive result: 我尝试了以下几件事,但没有取得积极的结果:

  • URLConnection setDefaultUSeCache(false) URLConnection setDefaultUSeCache(false)
  • URLConnection setUSeCache(false) URLConnection setUSeCache(false)
  • context.xml cachingAllowed="false" context.xml cachingAllowed =“ false”

Is there something else I could try? 还有什么我可以尝试的吗?

In Tomcat server, each incoming request uses a TCP socket and this socket consumes one file descriptor from the total available for the process. 在Tomcat服务器中,每个传入请求都使用一个TCP套接字,并且该套接字消耗该进程可用总量中的一个文件描述符。 The file fescriptor (FD) is a handle created by a process when the file is opened. 文件脚本(FD)是打开文件时由进程创建的句柄。 Each process can use a set limit of FDs and this is usually an OS level setting. 每个进程都可以使用FD的设置限制,这通常是操作系统级别的设置。

If you have many JS script files being loaded per page, then each JS request will consume one FD while it is being processed. 如果每页加载许多JS脚本文件,则每个JS请求在处理时将消耗一个FD。

As the number of requests coming into the server increase, you can face a situation where there are many sockets open and thus you run out of FDs and you get the "Too Many Open Files" error. 随着进入服务器的请求数量的增加,您可能会遇到以下情况:打开了许多套接字,因此FD用完了,并出现“打开文件过多”错误。

Check the value of # cat /proc/sys/fs/file-max to see how many FDs can be opened by your Tomcat on the server. 检查# cat /proc/sys/fs/file-max以查看您的Tomcat在服务器上可以打开多少个FD。

Ideally should be 65535. See here on how to increase this limit 理想情况下应为65535。有关如何增加此限制的信息,请参见此处

http://tech-torch.blogspot.com/2009/07/linux-ubuntu-tomcat-too-many-open-files.html http://tech-torch.blogspot.com/2009/07/linux-ubuntu-tomcat-too-many-open-files.html

Another suggestion is if you can reduce the number of JS calls, by combining the JS files into one. 另一个建议是,是否可以通过将JS文件合并为一个文件来减少JS调用的次数。

A little light on detail, but it sounds like you're loading the resources with a Stream, so chances are your code is creating also creates the object anonymously in a method call (a very common practice in examples I've seen) and I know that causes file locking issues on Windows, so I'm sure it keeps descriptors hanging about on Unixes. 细节上有些许细节,但是听起来好像您正在用Stream加载资源,所以您的代码创建的机会还可能是在方法调用中匿名创建对象(在我所看到的示例中,这是非常普遍的做法),而我知道这会导致Windows上的文件锁定问题,所以我确定它会使描述符在Unixes上徘徊。 Make sure you have a try block and you can call close() in the finally block when you're done. 确保您有一个try块,完成后可以在finally块中调用close()。 Static code analysis tools will usually catch this condition. 静态代码分析工具通常会捕获这种情况。

You don't mention the number of file handles we're talking about however. 您没有提到我们正在谈论的文件句柄数量。 Usually, network connections will be your culprit, and usually you want to increase the limit. 通常,网络连接是您的罪魁祸首,并且通常您希望增加限制。

The inputstream is closed in the finally block as you suggest. 如您建议的那样,inputstream在finally块中关闭。 I looked also at URLConnection, but it seems that there is no close() or disconnect method. 我也查看了URLConnection,但似乎没有close()或断开连接方法。

It seems to me that the files were claused after a certain period of time. 在我看来,这些文件是在一段时间后被取消的。 The open files are listed by lsof and if I reload the page open file handles go up. lsof列出了打开的文件,如果我重新加载页面,打开的文件句柄会上升。 But after a couple of minutes they go down again. 但是几分钟后,它们又掉了下去。 In case of high user traffic the open file handle were greater than the max of 2048 per process already. 如果用户流量很高,则打开的文件句柄已大于每个进程的最大值2048。 So the freeing of open file handles is to late then. 因此,打开文件句柄的释放要到那时。

If wrote a tiny test program that opens a URLconnection to a file. 如果编写了一个微型测试程序,该程序打开了到文件的URL连接。 It only calls getLastModified() and this opens a file handle already. 它仅调用getLastModified(),这已经打开了文件句柄。

Afterwards I close the input stream and this effect dissappears. 之后,我关闭输入流,这种效果消失了。

So I come to the conclusion that I have to close the URLConnection.inputStream even if the stream is not read after the connection. 因此,我得出的结论是,即使在连接后未读取流,也必须关闭URLConnection.inputStream。

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

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