title: Java Android ping IOException "Too many files open" when starting a process
body: <p>I'm creating an Android app that pings many hosts on the network...</p>
  
  <p>To do this I'm using the ping command at /system/bin/ping. Apparently this gives me an IOException "Too many files open". I presume this is because I'm not closing something properly and am bogging up the resources with it. I tried closing the inputstream but that didn't help.</p>
  
  <p>This happens when I start using pingHost() many times constantly... I have a button that you press to start and cancel a scan for the network. Each ping is run on a separate thread and will stop when the button is pressed to stop them setting a boolean "runnable" variable to false. If this button is pressed on and off a few times, it becomes slower. That's not cool. lol</p>
  
  <p>I've been fooling around with the program to try and figure out what I'm forgetting to close, but have been unsuccessful. </p>
  
  <p>I've tried proc.destroy() but it fails to destroy the process. I'm assuming this would also raise a problem with returning the exitValue() of proc. There is probably a simple solution I'm not understanding. I've been fooling around with this for a while with no luck. I would like some help if possible! :) </p>
  
  <pre><code>public static int pingHost(String host) throws IOException, InterruptedException {
      String cmd = "/system/bin/ping -c 1 -W 1000 " + host;
      Process proc = Runtime.getRuntime().exec(cmd);
      BufferedReader input = new BufferedReader(new InputStreamReader(proc.getErrorStream()));
      String line;
      while ((line = input.readLine()) != null) {
          System.out.println(line);
      }
      proc.waitFor();
  
      input.close();
      return proc.exitValue();
  }
  </code></pre>
  
  <blockquote>
    <p>Blockquote at java.lang.ProcessManager.exec(ProcessManager.java:211)
    12-31 16:24:07.306 13243-15816/sage.networktools W/System.err:     at java.lang.Runtime.exec(Runtime.java:168)
    12-31 16:24:07.306 13243-15816/sage.networktools W/System.err:     at java.lang.Runtime.exec(Runtime.java:241)
    12-31 16:24:07.306 13243-15816/sage.networktools W/System.err:     at java.lang.Runtime.exec(Runtime.java:184)
    12-31 16:24:07.306 13243-15816/sage.networktools W/System.err:     at sage.networktools.MainActivity.<strong>pingHost(MainActivity.java:98)</strong>
    12-31 16:24:07.316 13243-15791/sage.networktools W/System.err:     at java.lang.Runtime.exec(Runtime.java:184)
    12-31 16:24:07.316 13243-15791/sage.networktools W/System.err:     at sage.networktools.MainActivity.pingHost(MainActivity.java:98)
    12-31 16:24:07.316 13243-15791/sage.networktools W/System.err:     at sage.networktools.MainActivity$2.run(MainActivity.java:189)
    12-31 16:24:07.316 13243-15791/sage.networktools W/System.err:     at java.lang.Thread.run(Thread.java:856)
    12-31 16:24:07.316 13243-15791/sage.networktools W/System.err: Caused by: <strong>java.io.IOException: Too many open files</strong>
    12-31 16:24:07.316 13243-15791/sage.networktools W/System.err:     at java.lang.ProcessManager.exec(Native Method)
    12-31 16:24:07.316 13243-15791/sage.networktools W/System.err:     at java.lang.ProcessManager.exec(ProcessManager.java:209)</p>
  </blockquote>