-1

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?

user1907849
  • 960
  • 4
  • 19
  • 42
  • 2
    Well, you are allocating 1GB on the heap. How big is your JVM heap size set to? – OldProgrammer Jun 12 '14 at 15:20
  • is this a standalone java program? Look into -Xmx parameter, to give the program larger heap size. – Andrew Jun 12 '14 at 15:22
  • I am seting the -Xmx value to 1 GB , like java -Xmx1000m Client , like that but still I am getting the error – user1907849 Jun 12 '14 at 15:22
  • @Andrew , No I am starting server program in one console, and the one client in another console – user1907849 Jun 12 '14 at 15:24
  • If you set the max heap size to 1gb, you'll still get the error since you have 1gb. Set it to 2gb. – Anubian Noob Jun 12 '14 at 15:24
  • @user1907849, sorry, I wasn't clear. I mean are you running it from the command line, as opposed to in Tomcat or such. – Andrew Jun 12 '14 at 15:25
  • @AnubianNoob: I had tried tried that before itself, by setting 2GB, here is the exception: Could not reserve enough space for 2048000KB object heap – user1907849 Jun 12 '14 at 15:26
  • If you've set the heap to max heap size to 1GB, that isn't enough. First off, your FILE_SIZE is larger than 1GB (which is 1,073,741,824) bytes. Second, there are going to be many, many other objects created which will also require space on the heap. – dan.m was user2321368 Jun 12 '14 at 15:26
  • Then you don't have 2gb of memory available on your computer... – Anubian Noob Jun 12 '14 at 15:26
  • @Andrew , I am executing in command line itself, since I am not familiar any java tools , I am just 2 days old to java – user1907849 Jun 12 '14 at 15:27
  • What OS are you using? For instance, if you are using Linux, you might need to use the 'ulimit' command to set a higher maximum heap size allowed for a process. – dan.m was user2321368 Jun 12 '14 at 15:30

4 Answers4

1

Well, given that you're trying to have a 1gb file in memory, an out of memory error is understandable. Java doesn't have that much memory available for you.

The -Xmx flag allows you to set the maximum heap size.

-Xmx2000m should be emough.

If you're getting a "Could not reserve enough space" error, that means your computer does not have enough memory available. Try out a smaller file or break the file into small chunks and analyze them one at a time. Or send it as multiple data packets by reading the file in chunks.

Anubian Noob
  • 13,426
  • 6
  • 53
  • 75
1

You could try setting the Xmx and Xms properties as described here: -Xms : Initial heap size or minimum heap size?

Community
  • 1
  • 1
Niek Vandael
  • 394
  • 1
  • 3
  • 15
1

Even if you get past your memory allocation woes for this file, the potential for the issue to occur is still there You may want to look into streaming:

int count;
byte[] buffer = new byte[8192];
while ((count = inStream.read(buffer)) > 0)
{
  outStream.write(buffer, 0, count);
}
Andrew
  • 8,445
  • 3
  • 28
  • 46
0

Use the flag -Xmx2G or some value greater than 1G and try again

Gavy
  • 407
  • 3
  • 8