java
Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 27-11-2008, 22:41
kjkoster kjkoster is offline
Forum Operator
 
Join Date: Jul 2008
Posts: 1,126
Default Monitoring file descriptors (IOException or SocketException: Too many open files)

Dear All,

Java-monitor offers a view into the file descriptor usage of you Java application. In this post I will give you a little background information on file descriptor usage and explain how to interpret the file descriptor graph that Java-monitor produces.

File descriptors are the representation of your files and sockets inside the core of the JVM. Yes, sockets too. In fact, file descriptors are also used to represent pipes, serial ports, whole file systems and various other things in the UNIX file system.

There is a hard limit on the number of file descriptors that are available to your application. The actual limit depends very much on your system. It varies with OS you use, the amount of RAM your machine has, the kernel tunables for file descriptor allocation and the process limits that you enforce on the JVM. In short, the actual maximum for your Java processes will vary widely from machine to machine. For example: on FreeBSD (which is what Java-monitor itself runs on) the number of file descriptors depends on the amount of physical memory in your machine. On Linux, the default per process is lower, usually 1024.

Lucky for you, there are lots of file descriptor tuning tutorials. So if file descriptors become an issue for you, you can easily find the information you need to increase the number of file descriptors available to your Java process.

Monitoring file descriptors is simple: each JVM has a maximum number available, which is a hard limit. It also has a number of open file descriptors. If the application ever demands more than the maximum available file descriptors, an exception is thrown (java.io.IOException: Too many open files
or java.net.SocketException: Too many open files). In that case, your only options are to raise the number of available file descriptors, or to change the application to use fewer file descriptors.

Kees Jan

Last edited by kjkoster; 15-05-2009 at 12:15.
Reply With Quote
  #2  
Old 28-11-2008, 16:16
kjkoster kjkoster is offline
Forum Operator
 
Join Date: Jul 2008
Posts: 1,126
Default

Dear All,

Ah, I found a link to BEA's tech support pages where they explain this in more detail. They also show how you can see the file handle usage on Windows.

Kees Jan
Reply With Quote
  #3  
Old 29-06-2010, 12:07
mayanknsit mayanknsit is offline
Junior Member
 
Join Date: Jun 2010
Posts: 5
Default File Descriptor count using java

Can you tell me how to count the file descriptors using a java code?
Thanks in advance
Reply With Quote
  #4  
Old 05-07-2010, 15:08
cfd cfd is offline
Member
 
Join Date: Dec 2009
Location: EBBR
Posts: 37
Default

Kees Jan> the URL is not available, maybe a BEA problem ?

mayanknsit> not the sexiest but a quick & dirty solution just to show... you just have to write the glue

1. create a simple class to loop forever and start it with these options:

-Dcom.sun.management.jmxremote.port=4086 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false

2. store this code in a new class:
Code:
private static MBeanServerConnection getMBSC(String host, int port) throws MalformedURLException, IOException {
        JMXConnector conn = null;
        JMXServiceURL serviceUrl = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://" + host + ":" + port + "/jmxrmi");
        Hashtable params = new Hashtable();
        Hashtable credentials = new Hashtable();
        params.put(JMXConnector.CREDENTIALS, credentials);
        conn = JMXConnectorFactory.newJMXConnector(serviceUrl, params);
        conn.connect();
        return conn.getMBeanServerConnection();
    }

    public static void main(String[] args) {
        try {
            ObjectName oName = new ObjectName("java.lang:type=OperatingSystem");
            System.out.println("object name created ? " + oName);
            MBeanServerConnection mbsc = getMBSC("localhost", 4086);
            System.out.println("server connection ? " + mbsc);
            AttributeList list = mbsc.getAttributes(oName, new String[]{"OpenFileDescriptorCount", "MaxFileDescriptorCount"});
            System.out.println("attribute list: " + list.size() + " item(s)");
            for(Attribute attr: list.asList()){
                System.out.println(" +- item: " + attr.getName() + " -> " + attr.getValue());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
and start it after the loop-forever-class.

you should receive on the console something like this:
Code:
object name created ? java.lang:type=OperatingSystem
server connection ? javax.management.remote.rmi.RMIConnector$RemoteMBeanServerConnection@ba6c83
attribute list: 2 item(s)
 +- item: OpenFileDescriptorCount -> 99
 +- item: MaxFileDescriptorCount -> 10000
(tested with JDK 1.6.0_20)

Christophe

Last edited by cfd; 06-07-2010 at 00:03.
Reply With Quote
  #5  
Old 06-07-2010, 09:48
mayanknsit mayanknsit is offline
Junior Member
 
Join Date: Jun 2010
Posts: 5
Default

This is the code that i tries to run :

CODE

import java.io.IOException;
import java.net.MalformedURLException;
import java.util.Hashtable;

import javax.management.Attribute;
import javax.management.MBeanServerConnection;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;
import javax.swing.text.html.parser.AttributeList;


class FDCount {

public static void loopforever()
{

}
//public static void main(String args[])
//{
// FDCount.loopforever();
//}

}

class after{

private static MBeanServerConnection getMBSC(String host, int port) throws MalformedURLException, IOException {
JMXConnector conn = null;
JMXServiceURL serviceUrl = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://" + host + ":" + port + "/jmxrmi");
Hashtable params = new Hashtable();
Hashtable credentials = new Hashtable();
params.put(JMXConnector.CREDENTIALS, credentials);
conn = JMXConnectorFactory.newJMXConnector(serviceUrl, params);
conn.connect();
return conn.getMBeanServerConnection();
}

public static void main(String[] args) {

FDCount.loopforever();
try {
ObjectName oName = new ObjectName("java.lang:type=OperatingSystem");
System.out.println("object name created ? " + oName);
MBeanServerConnection mbsc = getMBSC("localhost", 4086);
System.out.println("server connection ? " + mbsc);
javax.management.AttributeList list = mbsc.getAttributes(oName, new String[]{"OpenFileDescriptorCount", "MaxFileDescriptorCount"});
System.out.println("attribute list: " + list.size() + " item(s)");
for(Attribute attr: list.asList()){
System.out.println(" +- item: " + attr.getName() + " -> " + attr.getValue());
}
} catch (Exception e) {
e.printStackTrace();
}
}


}


This is what i got as output

OUTPUT


object name created ? java.lang:type=OperatingSystem
server connection ? javax.management.remote.rmi.RMIConnector$RemoteMBe anServerConnection@ba6c83
attribute list: 0 item(s)

But i didn't get the FileDescriptorCount and MAXFDCount.

Can You please help out.
Thanks
Reply With Quote
  #6  
Old 06-07-2010, 10:13
kjkoster kjkoster is offline
Forum Operator
 
Join Date: Jul 2008
Posts: 1,126
Default

Dear mayanknsit,

Just to be sure, also print the Name attribute of the MBean. Like so:

javax.management.AttributeList list = mbsc.getAttributes(oName, new String[]{"Name", "OpenFileDescriptorCount", "MaxFileDescriptorCount"});

Kees Jan
Reply With Quote
  #7  
Old 06-07-2010, 10:38
cfd cfd is offline
Member
 
Join Date: Dec 2009
Location: EBBR
Posts: 37
Default

I have this output on my side:
Code:
object name created ? java.lang:type=OperatingSystem
server connection ? javax.management.remote.rmi.RMIConnector$RemoteMBeanServerConnection@ba6c83
attribute list: 3 item(s)
 +- item: Name -> Linux
 +- item: OpenFileDescriptorCount -> 99
 +- item: MaxFileDescriptorCount -> 10000
Reply With Quote
  #8  
Old 06-07-2010, 10:40
kjkoster kjkoster is offline
Forum Operator
 
Join Date: Jul 2008
Posts: 1,126
Default

Dear all,

The reason I ask is this: File descriptors are only available on UNIX-like systems. So if the name reports "Windows", you know that there are no fd's to report on.

Kees Jan
Reply With Quote
  #9  
Old 06-07-2010, 10:42
mayanknsit mayanknsit is offline
Junior Member
 
Join Date: Jun 2010
Posts: 5
Default

Yes You are right Kees. My OS is windows and probably thats why its not showing the FD count. So is there any way for windows platform.
Thanks
Reply With Quote
  #10  
Old 06-07-2010, 10:49
kjkoster kjkoster is offline
Forum Operator
 
Join Date: Jul 2008
Posts: 1,126
Default

Dear mayanknsit,

I expected that would be the case.

I am not aware of any way to count fd's on Windows. What problem are you trying to solve? No fd's means that you cannot run out of fd's. So what exception are you trying to squash?

Kees Jan
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump