title: Can't send large files over socket in Java
body: <p>I got working server and client applications, they work perfect while sending small files, but when I try to send for example movie file that is 700mb over socket it gives me </p>
  
  <pre><code>Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
  </code></pre>
  
  <p>I searched the internet and found some tutorials on sending large files, but couldn't quite understand them, but I think my porblem is in writing file.</p>
  
  <p>This is the code that server uses to write my file:</p>
  
  <pre><code>output = new FileOutputStream(directory + "/" + fileName);
              long size = clientData.readLong();
              byte[] buffer = new byte[1024];
  
              while (size &gt; 0 &amp;&amp; (bytesRead = clientData.read(buffer, 0, (int) Math.min(buffer.length, size))) != -1) {
                  output.write(buffer, 0, bytesRead);
                  size -= bytesRead;
              }
              output.close();
  </code></pre>
  
  <p>And here is the code that my client uses to send a file:</p>
  
  <pre><code>byte[] fileLength = new byte[(int) file.length()];  
  
          FileInputStream fis = new FileInputStream(file);  
          BufferedInputStream bis = new BufferedInputStream(fis);
  
          DataInputStream dis = new DataInputStream(bis);     
          dis.readFully(fileLength, 0, fileLength.length);  
  
          OutputStream os = socket.getOutputStream();  
  
          //Sending size of file.
          DataOutputStream dos = new DataOutputStream(os);   
          dos.writeLong(fileLength.length);
          dos.write(fileLength, 0, fileLength.length);     
          dos.flush();  
  
          socket.close();  
  </code></pre>