怎么数据查看java程序,如何找出Java应用程序当前打开的文件数量?

Suppose a lot of what your application does deals with reading contents of the files. Goes without saying that files that are open then closed and life is good unless .. new files come in faster then old files get closed. This is the pickle of a situation i found myself in.

Now .. is there a way to reliably know how many files are open by the process? Something that as reliable as looking at ls /proc/my_pid/fd | wc -l from inside the JVM?

I suspect answer may be OS specific, so let me add that i am running Java on Linux.

解决方案

On unix one way is using the ManagementFactory to get the OperatingSystemMxBean and if it is a UnixOperatingSystemMXBean you can use getOpenFileDescriptorCount() method.

Example Code below

import java.lang.management.ManagementFactory;

import java.lang.management.OperatingSystemMXBean;

import com.sun.management.UnixOperatingSystemMXBean;

public class OpenFileCount{

public static void main(String[] args){

OperatingSystemMXBean os = ManagementFactory.getOperatingSystemMXBean();

if(os instanceof UnixOperatingSystemMXBean){

System.out.println("Number of open fd: " + ((UnixOperatingSystemMXBean) os).getOpenFileDescriptorCount());

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值