If you want to get the number of ls /proc/my_pid/fd | wc -l
in java, you could use JMX.
When you have MBeanServerConnection
, you can get attribute "OpenFileDescriptorCount
". this will give you the same result as the above ls..|wc -l
.
also there is another attribute "MaxFileDescriptorCount
", which tells your the max count allowed.
If you just want to know the value, but not getting it in your code, you could either do your ls..|wc -l
way, or read from jconsole
(with GUI).
NOTE
ls /proc/my_pid/fd | wc -l
this number indicates how many fd opened by your process(java application). e.g. your business files are counted, also those jars, libraries files are counted too. If you just want to get the count of your business files, you have to implement a counter by yourself. Or, say you want to do it with shellscript, grep
something out then wc -l
EDIT
add code example, but it is just an example. not written in IDE, not tested with compiler. :)
ObjectName oName = new ObjectName("java.lang:type=OperatingSystem");
MBeanServerConnection conn ; // you should get the connection following the api, take a look the java api/ google some example
javax.management.AttributeList list = conn.getAttributes(oName, new String[]{"OpenFileDescriptorCount", "MaxFileDescriptorCount"});
for(Attribute attr: list.asList()){
System.out.println(attr.getName() + ": " + attr.getValue());
}
/proc/self/fd/
. That's what the proc filesystem is for: Get (and set) process information at runtime. hierarchical tree of values with node-dependent read/write semantics + mapping of the same into the hierarchical tree of streams that is a filesystem = unified win (well, in a typeless Plan 9 kind of way - even better: open a JDBC conn to "somewhere" then query/update sys & proc using SQL, a bit like OSQuery but far more complete) – Fermin