48

Is there a max. size you can set Xmx to? I set it to 1024m and eclipse opens ok. When I set it above 1024, eclipse doesn't open and I get the error "jvm terminated. Exit code=-1"...

I was doing this because I keep getting an "java.lang.OutOfMemoryError: Java heap space". I am reading in a 35.5Mb .txt file and this error occurs when it's just reading in the file using the "while((line = reader.readLine()) != null)" loop. I would have thought that 1024mb would have been enough. Can anyone help me?

Michael Myers
  • 188,989
  • 46
  • 291
  • 292
  • Can you post your code for the file read method - You must be doing something wrong ;) – Richard Walton Jan 20 '09 at 13:25
  • nickolai makes a good suggestion (run configurations), as well as Richie_W (must be doing something wrong - is the file > 1G?). – ericp Jan 20 '09 at 13:41
  • Try also those settings (http://stackoverflow.com/questions/142357/what-are-the-best-jvm-settings-for-eclipse-34) and try to follow your application with a JConsole – VonC Jan 20 '09 at 13:45
  • 1
    The file is only 35.5Mb. –  Jan 20 '09 at 14:08

4 Answers4

35

Yes, there is a maximum, but it's system dependent. Try it and see, doubling until you hit a limit then searching down. At least with Sun JRE 1.6 on linux you get interesting if not always informative error messages (peregrino is netbook running 32 bit ubuntu with 2G RAM and no swap):

peregrino:$ java -Xmx4096M -cp bin WheelPrimes 
Invalid maximum heap size: -Xmx4096M
The specified size exceeds the maximum representable size.
Could not create the Java virtual machine.

peregrino:$ java -Xmx4095M -cp bin WheelPrimes 
Error occurred during initialization of VM
Incompatible minimum and maximum heap sizes specified

peregrino:$ java -Xmx4092M -cp bin WheelPrimes 
Error occurred during initialization of VM
The size of the object heap + VM data exceeds the maximum representable size

peregrino:$ java -Xmx4000M -cp bin WheelPrimes 
Error occurred during initialization of VM
Could not reserve enough space for object heap
Could not create the Java virtual machine.

(experiment reducing from 4000M until)

peregrino:$ java -Xmx2686M -cp bin WheelPrimes 
(normal execution)

Most are self explanatory, except -Xmx4095M which is rather odd (maybe a signed/unsigned comparison?), and that it claims to reserve 2686M on a 2GB machine with no swap. But it does hint that the maximum size is 4G not 2G for a 32 bit VM, if the OS allows you to address that much.

Pete Kirkham
  • 48,893
  • 5
  • 92
  • 171
  • 1
    `-Xmx117000000M` (117TB) did not error on a MacBookPro with 16GB ram, 64 bit JVM, Java 8, Oracle JDK, 2022-02. `-Xmx118000000M` did error. For my Linux aws instance with openjdk and 8GB ram with no swap, `-Xmx236000M` (236GB) did not error but `-Xmx237000M` errored. – Curtis Yallop Feb 10 '22 at 16:51
5

I think a 32 bit JVM has a maximum of 2GB memory.This might be out of date though. If I understood correctly, you set the -Xmx on Eclipse launcher. If you want to increase the memory for the program you run from Eclipse, you should define -Xmx in the "Run->Run configurations..."(select your class and open the Arguments tab put it in the VM arguments area) menu, and NOT on Eclipse startup

Edit: details you asked for. in Eclipse 3.4

  1. Run->Run Configurations...

  2. if your class is not listed in the list on the left in the "Java Application" subtree, click on "New Launch configuration" in the upper left corner

  3. on the right, "Main" tab make sure the project and the class are the right ones

  4. select the "Arguments" tab on the right. this one has two text areas. one is for the program arguments that get in to the args[] array supplied to your main method. the other one is for the VM arguments. put into the one with the VM arguments(lower one iirc) the following:
    -Xmx2048m

    I think that 1024m should more than enough for what you need though!

  5. Click Apply, then Click Run

  6. Should work :)

Gray
  • 115,027
  • 24
  • 293
  • 354
user54579
  • 4,588
  • 4
  • 23
  • 19
1

I think that it's around 2GB. While the answer by Pete Kirkham is very interesting and probably holds truth, I have allocated upwards of 3GB without error, however it did not use 3GB in practice. That might explain why you were able to allocate 2.5 GB on 2GB RAM with no swap space. In practice, it wasn't using 2.5GB.

cdeszaq
  • 30,869
  • 25
  • 117
  • 173
1

Have a look at this for some common errors in setting the java heap. You've probably set the heap size to a larger value than your computer's physical memory.

You should avoid solving this problem by increasing the heap size. Instead, you should profile your application to see where you spend such a large amount of memory.

kgiannakakis
  • 103,016
  • 27
  • 158
  • 194