有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

大量文件描述符对Java应用程序有什么影响

我们在Tomcat上部署了一个web应用程序,它可以有超过300k的文件描述符,而单个进程的限制是250k。 奇怪的是:当服务器运行时,数字会上升(400k)和下降(100k)。有时,当数量很大时,我们无法ssh到操作系统中。但是,我们没有发现任何与许多打开的文件或socket建立问题相关的错误。 大多数文件描述符与JVM加载的jar文件相关。我的问题是:
1.OS(CentOS 7)如何计算tomcat的文件描述符?我认为Tomcat不会在运行时打开这些文件。 2.为什么号码不固定?相反,存在大量重复的jar文件。 3.有这么多文件描述符是正常的吗


共 (2) 个答案

  1. # 1 楼答案

    如果您查看com.sun.management.internal.OperatingSystemImpl(在^{}下)的相应本机方法的源代码

    • OpenFileDescriptorCount/proc/self/fds下的文件描述符数量,以及
    • MaxFileDescriptorCount只是^{的值:
      RLIMIT_NOFILE
             This specifies a value one greater than  the  maximum  file  de‐
             scriptor  number  that  can be opened by this process.  Attempts
             (open(2), pipe(2), dup(2), etc.)  to exceed this limit yield the
             error  EMFILE.  (Historically, this limit was named RLIMIT_OFILE
             on BSD.)
    
             Since Linux 4.5, this limit also defines the maximum  number  of
             file  descriptors  that an unprivileged process (one without the
             CAP_SYS_RESOURCE capability) may have "in flight" to other  pro‐
             cesses,  by being passed across UNIX domain sockets.  This limit
             applies to the sendmsg(2) system call.  For further details, see
             unix(7).
    
  2. # 2 楼答案

    操作系统对任何一个进程可以同时打开的文件数量有限制。大多数发行版的默认值仅为1024个文件。每个打开的文件还具有关联的文件描述符。套接字连接被视为文件,它们使用文件描述符,因此受到相同的资源限制

    1. 您可以通过命令ulimit验证或更改最大限制

    2. 您还可以查看MBean Attibutes的值- MaxFileDescriptorCount&OpenFileDescriptorCount通过运行JMX工具-JConsole

    3. 当OpenFileDescriptorCount小于MaxFileDescriptorCount时 您的应用程序工作正常,否则您将得到java.io.IOException: Too many open files,这将导致应用程序出现故障

    4. 通常,对于应用程序,FD(文件描述符)的计数 上升/下降到一定水平。但它应该在内部 MaxFileDescriptorCount