title: Too many open files in one of my java routine
body: <p>I have a multithreaded code that has to generated a set of objects and write them to a file. When I run it I sometime get "Too many open files" message in Exception. I have checked the code to make sure that all the file streams are being closed properly. Here is the stack trace.</p>
  
  <p>When I do ulimit -a, open files allowed is set to 1024. We think increasing this number is not a viable option / solution. </p>
  
  <pre><code> [java] java.io.FileNotFoundException: /export/event_1_0.dtd (Too many open files)
   [java]     at java.io.FileInputStream.open(Native Method)
   [java]     at java.io.FileInputStream.&lt;init&gt;(FileInputStream.java:106)
   [java]     at java.io.FileInputStream.&lt;init&gt;(FileInputStream.java:66)
   [java]     at sun.net.www.protocol.file.FileURLConnection.connect(FileURLConnection.java:70)
   [java]     at sun.net.www.protocol.file.FileURLConnection.getInputStream(FileURLConnection.java:161)
   [java]     at java.net.URL.openStream(URL.java:1010)
  </code></pre>
  
  <p>Now what we have identified so far by looking closely at the list of open files is that the VM is opening same class file multiple times. </p>
  
  <p>/export/BaseEvent.class   236<br>
  /export/EventType1BaseEvent.class   60<br>
  /export/EventType2BaseEvent.class   48<br>
  /export/EventType2.class   30<br>
  /export/EventType1.class   14   </p>
  
  <p>Where BaseEvent is partent of all the classes and EventType1 ant EventType2 inherits EventType1BaseEvent and EventType2BaseEvent respectively. Why would a class loader load the same class file 200+ times. It seems it is opening up the base class as many time it create any child instance. </p>
  
  <p>Is this normal? Can it be handler any other way apart from increasing the number of open files?</p>