-1

I am using the following piece of code in one of my methods in java:

int []  byte_song = new int[8414449];
int []  int_song = new int[8414449 - 45];

If i just write:

int []  byte_song = new int[8414449];

it does not give me any error. However if i write:

int []  byte_song = new int[8414449];
int []  int_song = new int[8414449 - 45];

it gives me OutOfMemoryError.

I also tried calling System.gc(); before int []int_song = new int[8414449 - 45]; still i get the error. Can some body please help me out in this.

Jacob Raihle
  • 3,720
  • 18
  • 34
Joyson
  • 1,643
  • 5
  • 28
  • 48
  • 4
    System.gc() is a mere request. One cannot rely on it to free memory instantly. – Pavan Dittakavi Apr 25 '13 at 15:03
  • 3
    So let me get this staright: it doesn't give you an error when you allocate less, but it does give an `OutOfMemoryError` when you allocate more? Yes, that is strange. – Marko Topolnik Apr 25 '13 at 15:03
  • @Pavan With OP's code, even if it were a deterministic cleanup of every last available byte, it would make no difference since that code doesn't create any garbage. – Marko Topolnik Apr 25 '13 at 15:08
  • Do you have any other memory consuming objects in your code? Not neccessarily in the same method? – PM 77-1 Apr 25 '13 at 15:09
  • 2
    @ Joyson : Some math. Your code block requires atleast 8414449* 4 * 2 (assuming 45 does not play a role). The byte_song is not eligible for any garbage collection yet – Jayan Apr 25 '13 at 15:10
  • When do you get the exception if you do `int [] int_song = new int[8414449 - 45];` first, and then `int [] byte_song = new int[8414449];`? – phoenix7360 Apr 25 '13 at 15:11
  • @Marko Topolnik - Are you saying that the entire array (and not just its descriptor) is located on the stack? – PM 77-1 Apr 25 '13 at 15:11
  • 1
    Can you post a the full program and check which version of the JVM (and which platform) you are using? I created a test program with just those lines and it works fine for me. – PhilDin Apr 25 '13 at 15:12
  • I'm also curious if using a wrapper class `Integer` would make any difference. – PM 77-1 Apr 25 '13 at 15:12
  • @PM77-1 No. The array is on the heap, including any "descriptors" (I guess you mean array headers). – Marko Topolnik Apr 25 '13 at 15:12
  • @ Joyson : It is not easy to guess the default . For reference: The default memory for Oracle JVM is dynamically determined. http://www.oracle.com/technetwork/java/javase/6u18-142093.html – Jayan Apr 25 '13 at 15:20
  • I have a lot lenthier code in the method.Debugging the program gave me the error on the following line so i wrote a test app with just these two lines of code and still i got the same error.The test program just contains these 2 lines of code. – Joyson Apr 25 '13 at 15:24
  • I ran the code in eclipse.Using Window->Preferences->Java->Installed JREs i found the Type specified as Standard VM. – Joyson Apr 25 '13 at 15:40
  • 1
    BTW: For expert developers; you can use memory mapped files to store very large arrays of data. The size of these files is only limited by the free space on the disk, not the maximum heap size. – Peter Lawrey Apr 25 '13 at 16:09
  • Just for grins, trying setting `byte_song = null;` before you allocate int_song. – Hot Licks Apr 25 '13 at 16:48

1 Answers1

7

You need to allocate more memory. Use the -Xmx memory option for this. The JVM has a default amount of memory beyond which it will not allow you to allocate any more, and it's typically less than the amount of memory you have on the computer.

Community
  • 1
  • 1
Jayan
  • 18,003
  • 15
  • 89
  • 143
  • yan Can u please tell me how to allocate more memory using the above -Xmx if that can help. – Joyson Apr 25 '13 at 15:43
  • @Joyson : Example java -Xmx1100m -classpath . Why not refer the link in the answer? – Jayan Apr 25 '13 at 15:46
  • Exactly yan but cant i do this programmatically than doing it in cmd.I guesss if i m not wrong the above command has to be written in cmd. – Joyson Apr 25 '13 at 15:52
  • @Joyson : That is correct. The value has to be set when jvm starts. It can be set in eclipse run configuration as well. Please check docs. – Jayan Apr 25 '13 at 15:54
  • 1
    @Joyson : http://stackoverflow.com/questions/10639322/how-can-i-specify-the-default-jvm-arguments-for-programs-i-run-from-eclipse – Jayan Apr 25 '13 at 15:56
  • Thnx yan for above link i will follow it asap – Joyson Apr 25 '13 at 15:58
  • @Joyson The maximum is the actual maximum. It's not the maximum, unless-I-change-my-mind-later maximum. You can't change it because it is allocated on startup. – Peter Lawrey Apr 25 '13 at 16:07
  • yan i tried increasing the default Vm arguments however still no luck :( – Joyson Apr 25 '13 at 16:25