Java Out of memory - Out of heap size

Posted by user1907849 on Stack Overflow See other posts from Stack Overflow or by user1907849
Published on 2014-06-12T15:19:00Z Indexed on 2014/06/12 15:24 UTC
Read the original article Hit count: 204

Filed under:

I downloaded the sample program , for file transfer between the client and server. When I try running the program with 1 GB file , I get the Exception in thread "main" java.lang.OutOfMemoryError: Java heap space at Client.main(Client.java:31).

Edit: Line no 31: byte [] mybytearray = new byte [FILE_SIZE];

public final static int FILE_SIZE = 1097742336;
// receive file
      long startTime = System.nanoTime();
      byte [] mybytearray  = new byte [FILE_SIZE];
      InputStream is = sock.getInputStream();
      fos = new FileOutputStream(FILE_TO_RECEIVED);
      bos = new BufferedOutputStream(fos);
      bytesRead = is.read(mybytearray,0,mybytearray.length);
      current = bytesRead;

      do {
         bytesRead =
            is.read(mybytearray, current, (mybytearray.length-current));
         if(bytesRead >= 0) current += bytesRead;
      } while(bytesRead > -1);

      bos.write(mybytearray, 0 , current);
      bos.flush();

Is there any fix for this?

© Stack Overflow or respective owner

Related posts about java