I'm trying to sort a list of 1 billion integers using Collection.sort(List) and it throws following exception "Exception in thread "main" java.lang.OutOfMemoryError: Java heap space" -xms is set as 512m and xmx at 1536m. How do i sort it? I've 8GB ram in my system so allocating more physical memory is not a problem. I tried giving -xmx 2048m but vm could not initialize with that setting.
-
2An array of 1 billion integers is 1 billion times 4 bytes, which is almost 4 GB, so Xmx2048 wouldn't have been enough even if you had spelled it right. – Zoltán Mar 16 '15 at 10:58
-
Related, if not a duplicate: http://stackoverflow.com/q/7203159/3419894 – JonK Mar 16 '15 at 10:59
-
Also, you may consider an alternative approach that uses custom lists (smaller memory footprint than java List*s*) and you could write your own sorting algorithm (both things have also an educative value). – uraimo Mar 16 '15 at 11:01
2 Answers
It's likely case-sensitive and there should be no space, try with -Xmx2048m
.
Nonstandard Options
-Xmxn
Specifies the maximum size, in bytes, of the memory allocation pool. This value must a multiple of 1024 greater than 2 MB. Append the letter k or K to indicate kilobytes, or m or M to indicate megabytes. The default value is chosen at runtime based on system configuration.
For server deployments, -Xms and -Xmx are often set to the same value. See Garbage Collector Ergonomics at http://docs.oracle.com/javase/7/docs/technotes/guides/vm/gc-ergonomics.html
Examples:
-Xmx83886080
-Xmx81920k
-Xmx80m
See docs.

- 19,081
- 8
- 48
- 55
-
-startup plugins/org.eclipse.equinox.launcher_1.3.0.v20120522-1813.jar --launcher.library plugins/org.eclipse.equinox.launcher.win32.win32.x86_1.1.200.v20120522-1813 -product org.eclipse.epp.package.jee.product --launcher.defaultAction openFile --launcher.XXMaxPermSize 256M -showsplash org.eclipse.platform --launcher.XXMaxPermSize 256m --launcher.defaultAction openFile -vmargs -Dosgi.requiredJavaVersion=1.5 -Dhelp.lucene.tokenizer=standard -Xms512m -Xmx1024m This is my eclipse.ini file – Shubhansh Vatsyayan Mar 16 '15 at 10:59
-
The eclipse configuration doesn't matter, when you launch you application it spawns a new jvm, you must add that option in your project runtime configuration parameters. – uraimo Mar 16 '15 at 11:03
-
I tried giving -xmx 2048m but vm could not initialize with that setting.
You are likely using a 32-bit JVM, which have a practical max heap size below 2GB. You should try a 64-bit JVM.
Note also that you should set the VM parameters for the program, not for Eclipse (as it seems you are doing per one of your comments). You do that by selecting the dropdown menu in Run then Run Configurations. Then select your program and open the Arguments panel.

- 12,100
- 5
- 46
- 57