Java长期运行后 jps等工具无法连接jvm

2014-11-04 17:45:08 · 作者: · 浏览: 44

  相信很多朋友都遇见过, 一个Java应用长期运行后, 发现jps, jstack, jstat等工具都无法连接正在运行的jvm了。 如果这个时候发生故障, 非常难以诊断。 一直以来, 我都以为是Java的bug.


  最近偶然得知, jps的工作模式是读取了系统临时文件夹下的pid文件里的内容获得连接信息的。这个文件夹在Linux下的名字是:/tmp/hsperfdata_$USER ($USER是启动Java程序的用户)。我们都知道系统临时文件夹可能会被某些临时文件夹工具自动删除, 比如:


  1. tmpwatch: redhat linux发行版使用的删除工具


  2. tmpreaper: ubuntu linux 发行版使用的删除工具


  这些工具可能是没有安装的。 那么就不会发生删除/tmp/hsperfdata_$USER的事情。


  我们观察到, redhat5.2/5.3 的删除工具的存在一些瑕疵, tmpwatch -umc 会按照最长时间来确定删除。看看umc参数定义:


  Java代码


  -----------------------------------


  If the --atime, --ctime or --mtime options are used in combination, the decision about deleting a file will be based on the maximum of these times.


  -----------------------------------


  -u, --atime


  Make the decision about deleting a file based on the file's atime (access time). This is the default.


  Note that the periodic updatedb file system scans keep the atime of directories recent.


  -m, --mtime


  Make the decision about deleting a file based on the file's mtime (modification time) instead of the atime.


  -c, --ctime


  Make the decision about deleting a file based on the file's ctime (inode change time) instead of the atime; for


  directories, make the decision based on the mtime.


  -----------------------------------