3

How can I find the maximum byte array size that will not exceed the VM limit?

What I tried is:

int size = Integer.MAX_VALUE;
byte[] request = new byte[size];

But then I get the error: Exception in thread "main" java.lang.OutOfMemoryError: Requested array size exceeds VM limit

The backstory is my packet proxy keeps missing parts of a packet because I don't know what the max memory size I can use is.

Thanks!

  • Just to mention, you get all kinds of GC related problems with those large arrays, especially if you allocate them dynamically. When you create a few managed ones at program start, it might be safe. But then again alternatives like off heap ByteBuffers could be better. – eckes Jan 05 '15 at 00:23
  • Give it up!! You should not intentionally try to create a ginormous array that occupies most of available heap. There are too many ways for this to come back and bite you. – Hot Licks Jan 05 '15 at 00:32
  • 1
    And no valid comm protocol should require that you allocate enormous buffers. – Hot Licks Jan 05 '15 at 00:33
  • And keep in mind that there are probably many Java implementations where you cannot create a byte array of `Integer.MAX_VALUE` elements, regardless of how much heap is free, since the max object size is, at most, that value, and the object needs at least 16, probably 32 bytes for its "header". – Hot Licks Jan 05 '15 at 00:40

3 Answers3

4

You can find out the answer to your question yourself with something like

long maxByteArraySize() {
   long size = Integer.MAX_VALUE;
   while(--size > 0) try {
        new byte[size];
        break;
   } catch(Throwable t) {}   
   return size;
}

Also note, that you can increase the amount of memory available to the jvm with -Xmx flag, eg: java -Xmx4g MyClass will probably let you allocate (a lot) larger array than you are getting by default.

(Not that I think that what you are trying to do is actually a great idea ...)

Dima
  • 39,570
  • 6
  • 44
  • 70
0

Well Integer.MAX_VALUE is 2,147,483,647; you can determine how much memory is free with

System.out.println(Runtime.getRuntime().freeMemory());

But I wouldn't try and allocate all of it to a byte[].

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
0

Basically arrays are considered to be objects in java. And objects are stored on heap memory. It doesn't matter whether your objects are local objects but only its references are stored in stack memory. You can find the max heap size by following :-

java -XX:+PrintFlagsFinal -version 2>&1 | findstr MaxHeapSize

And approximately take less size array.

Devavrata
  • 1,785
  • 17
  • 30