title: Reading a bunch of files and writing to a socket (asynchronously) in android gives OutofMemoryError exception
body: <p>In my android application I read a bunch of files in a chunk of 64KiB and write it to a socket. The write to socket is async and I do not have a control over it, neither it provides a callback function.</p>
  
  <p>If I read large files, I get OOM exception as socket is still working on parsing the bytes and sending them over the network.</p>
  
  <p>For reference the code looks like below:</p>
  
  <pre><code>byte[] chunk = new byte[64 * 1024];
  int read;
  while ((read = fis.read(chunk)) != -1) {
    // Async call
    socket.write(chunk);
    chunk = new byte[64 * 1024];
  }
  </code></pre>
  
  <p>Is there any design pattern I can follow to overcome this situation.</p>
  
  <p>Thanks</p>