97

I need to run a Java memory intensive application that uses more than 2GB, but I am having problems to increase the heap maximum size. So far, I have tried the following approaches:

  • Setting the -Xmx parameter, e.g. -Xmx3000m. This approaches fails at the creation of the JVM. From what I've googled, it looks like that -Xmx must be less than 2GB.

  • Using the -XX:+AggressiveHeap option. When I try this approach I get an 'Not enough memory' error that tells that the heap size is 1273.4 MB, even though my computer has 8GB of memory.

Is there another approach that I can try to increase the maximum heap size of the JVM? Here's a summary of the computer specs:

  • OS: Windows 7 (64 bit)
  • Processor: Intel Core i7 (2.66 GHz)
  • Memory: 8 GB
  • java -version:
java version "1.6.0_18"
Java(TM) SE Runtime Environment (build 1.6.0_18-b07)
Java HotSpot(TM) Client VM (build 16.0-b13, mixed mode, sharing)
Alceu Costa
  • 9,733
  • 19
  • 65
  • 83
  • 7
    BTW: The minimum and maximum memory size are now standard options. You can use -ms and -mx instead of -Xms and -Xmx. -X?? is reserved for non-standard options. – Peter Lawrey Jun 18 '10 at 07:05
  • 2
    Standard for which JVM? They are still non-standard for the HotSpot JVM (as of 1.8). See http://docs.oracle.com/javase/8/docs/technotes/tools/unix/java.html#BABDJJFI – Huckle May 02 '15 at 19:38
  • -mx and -ms exists and works, but I can't find it in official java documentation @PeterLawrey > can you add link to documentation? Thanks – Michal Bernhard Jul 11 '18 at 10:44
  • 2
    @MichalBernhard it's backward compatibility with Java 1.1. I saw it documented for that version but might be hard to find now. ;) – Peter Lawrey Jul 11 '18 at 19:09
  • 1
    Thanks @PeterLawrey for an explanation. But when you say it's now standard you mean that it is standard from Java 1.1 (but not document in later versions)? Seems odd :) btw I found link on Java 1.1 documentation and you are right: http://www.ad.ntust.edu.tw/course/ad5601701/jmdl/docs/java/tooldocs/win32/java.html – Michal Bernhard Jul 12 '18 at 14:37

7 Answers7

106

When you are using JVM in 32-bit mode, the maximum heap size that can be allocated is 1280 MB. So, if you want to go beyond that, you need to invoke JVM in 64-mode.

You can use following:

$ java -d64 -Xms512m -Xmx4g HelloWorld

where,

  • -d64: Will enable 64-bit JVM
  • -Xms512m: Will set initial heap size as 512 MB
  • -Xmx4g: Will set maximum heap size as 4 GB

You can tune in -Xms and -Xmx as per you requirements (YMMV)

A very good resource on JVM performance tuning, which might want to look into: http://java.sun.com/javase/technologies/hotspot/gc/gc_tuning_6.html

mohitsoni
  • 3,091
  • 3
  • 19
  • 10
  • My JVM is 32-bit and allows maximum of -Xmx1024M. Also going to try 64bit version. – kiltek Jun 22 '13 at 14:30
  • I have tried JVM 64-bit and it worked perfectly. I can now set 4096 MB of maximum heap size. When you have both 32 and 64 bits installed, at least on Windows you need to point the Java path for your applications to the new 64 bit version installed. Otherwise, the error will persist. On Windows this often can be done by changing the Java path on system's Environment Variables. – Fabiano Feb 02 '15 at 23:10
  • I guess the limit for the maximum heap is not 1280, but something near to 1700MB. I used 1600MB of maximum heap on my 32 bit JVM installation and it worked fine. – Fabiano Feb 02 '15 at 23:12
  • what is the HelloWorld argument in your command? how can I use this command if I want to set the maximum of a Java process that is started with the java -jar myApp.jar command on linux? – LordScone Dec 03 '15 at 08:14
  • Hi, may I ask what is the maximum java heap size can I set? When I run the application, the defaul -Xmx on my machine is 4GB, but can I set it more than 4GB? – Ock Jul 24 '18 at 09:54
49

Get yourself a 64-bit JVM from Oracle.

Michael Celey
  • 12,645
  • 6
  • 57
  • 62
President James K. Polk
  • 40,516
  • 21
  • 95
  • 125
15

I believe the 2GB limit is for 32-bit Java. I thought v1.6 was always 64 bit, but try forcing 64 bit mode just to see: add the -d64 option.

G__
  • 7,003
  • 5
  • 36
  • 54
  • The -D64 option didn't work, I will try using the 64-bit JVM as GregS suggested and report the results. – Alceu Costa Jun 12 '10 at 22:33
  • 2
    The -d64 and -d32 switches works correctly only on Solaris (at least according these documentation): http://java.sun.com/docs/hotspot/HotSpotFAQ.html#64bit_layering. – Lukasz Stelmach Jun 12 '10 at 22:43
  • 1
    I've tried with both options. With -d64 the JVM fails to launch even if I don't specify the Xmx option. The -D64 only works when I don't specify the Xmx. – Alceu Costa Jun 12 '10 at 22:52
  • @Lukasz -d64 and -d32 also seem to work on Apple's JVM. But that document does seem to imply that they are non-functional for Sun's Windows & Linux JVMs. – G__ Jun 12 '10 at 22:57
8

32-bit Java is limited to approximately 1.4 to 1.6 GB.

Oracle 32 bit heap FAQ

Quote

The maximum theoretical heap limit for the 32-bit JVM is 4G. Due to various additional constraints such as available swap, kernel address space usage, memory fragmentation, and VM overhead, in practice the limit can be much lower. On most modern 32-bit Windows systems the maximum heap size will range from 1.4G to 1.6G. On 32-bit Solaris kernels the address space is limited to 2G. On 64-bit operating systems running the 32-bit VM, the max heap size can be higher, approaching 4G on many Solaris systems.

Jean B
  • 326
  • 3
  • 4
4

Below conf works for me:

JAVA_HOME=/JDK1.7.51-64/jdk1.7.0_51/
PATH=/JDK1.7.51-64/jdk1.7.0_51/bin:$PATH
export PATH
export JAVA_HOME

JVM_ARGS="-d64 -Xms1024m -Xmx15360m -server"

/JDK1.7.51-64/jdk1.7.0_51/bin/java $JVM_ARGS -jar `dirname $0`/ApacheJMeter.jar "$@"
GabrielOshiro
  • 7,986
  • 4
  • 45
  • 57
1

For memory intensive applications, GraalVm can serve as a better alternative. Providing 32 GB as max heap size and other benefits. Hotspot vs Graal

Caffeine Coder
  • 948
  • 14
  • 17
0

In my case,

-Xms1024M -Xmx1024M is work

-Xms1024M -Xmx2048M result: Could not reserve enough space for object heap

after use JVM 64 bit, it allows using 2GB RAM, because I am using win server 2012

please see the available max heap size for JVM 32 bit on several OSs Xmx for JVM 32bit

https://www.codementor.io/@suryab/does-32-bit-or-64-bit-jvm-matter-anymore-w0sa2rk6z

Keith POON
  • 86
  • 6