Java Out of memory - Out of heap size
- by user1907849
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?