title: Hundreds of Apache SSHD clients leave hundreds of OpenSSH daemon processes running
body: <p>I use a the Apache SSHD java library's client to serve files over HTTP that are read via SSH. The concept app works great. </p>
  
  <p>But when I want to read hundreds of files serially (e.g. to display images in a gallery) I run into scalability problems: my server ends up with hundreds of OpenSSH daemon processes:</p>
  
  <ul>
  <li>under my username as <code>/usr/lib/openssh/sftp-server</code> and</li>
  <li><code>sshd:username@notty</code>, and  </li>
  <li>under root as <code>sshd: username [priv]</code></li>
  </ul>
  
  <p>which causes my server to run out of memory and my server to crash.</p>
  
  <p>I don't think this is a bug in OpenSSH (I'm using <code>OpenSSH_5.9p1</code>), but how I'm using the Apache SSHD client. </p>
  
  <p>Here is the code I run every time I serve a file:</p>
  
  <pre class="lang-java prettyprint-override"><code>SshClient client = SshClient.setUpDefaultClient();
  client.getProperties().put(ClientFactoryManager.HEARTBEAT_INTERVAL, "50000");
  client.start();
  session = client.connect("username", "server", 22).await().getSession();
  session.addPasswordIdentity("password");
  session.auth().await();
  SftpClient sftp = session.createSftpClient();
  // Create an HTTP response from an sftp channel stream
  </code></pre>
  
  <p>Which of the following, if any, do I need to make hundreds of JSch client requests?</p>
  
  <ol>
  <li>Close/stop my session and/or client after each request? (the
  terminology here is so generic yet the functionality is so precise
  that I may have wrong understanding of each component)? </li>
  <li>Client pooling?</li>
  <li>Server configuration to limit the number of daemon threads?</li>
  <li>Reduce the timeout (on the client or on the server)?</li>
  </ol>
  
  <p>Any insight, specific or general, would be helpful.</p>