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
Answer from mohitsoni on Stack OverflowWhen 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
Get yourself a 64-bit JVM from Oracle.
JVM (total) max memory
How is the default max Java heap size determined? - Stack Overflow
Increase heap size in Java - Stack Overflow
How to increase java' heap size, whatever that is
Videos
I was pondering it for a while now and... it would be great if JVM would allow specifying maximum (total) JVM memory usage.
Currently, we can specify it for various parts of JMV (most notably heap) but very often other parts (meta, threads, code, gc, symbols, etc) take also part of memory (often significant) thus specifying only heap can be tricky, especially if we want to maximize resource usage on the machine (be that VM or within container)...
While I haven't run any JRuby apps on Tomcat, I have run ColdFusion apps on varied J2EE app servers, and I also have had similar issues.
In these FAQs, you'll see that SOracle says that on 32-bit Windows, you'll be limited to a max heap size of 1.4 to 1.6 GB. I never was able to get it stable that high, and I suspect you're running a similar configuration.
My guess is that your requests are taking a long time to run b/c with a heap size that high, the JVM has allocated more physical memory than Windows had to give, and thus Windows spends a lot of time swapping pages in and out of memory to disk so it can provide the required amount of memory to the JVM.
My recommendation, although counter-intuitive, would be that you actually lower the max heap size to somewhere around 1.2 GB. You can raise the min size as well, if you notice that there are slow-downs in the app's request processing while the JVM has to ask Windows for more memory to increase the size of its heap as it fills with uncollected objects.
Part of your problem is that you are probably starving all other processes for ram. My general rule of thumb for -Xms and -Xmx are as follows:
-Xms : <System_Memory>*.5
-Xmx : <System_Memeory>*.75
So on a 4GB systems it would be: -Xms2048m -Xmx3072m, and in your case I would go with -Xms896m -Xmx1344
On Windows, you can use the following command to find out the defaults on the system where your applications runs.
java -XX:+PrintFlagsFinal -version | findstr HeapSize
Look for the options MaxHeapSize (for -Xmx) and InitialHeapSize for -Xms.
On a Unix/Linux system, you can do
java -XX:+PrintFlagsFinal -version | grep HeapSize
I believe the resulting output is in bytes.
For Java SE 5: According to Garbage Collector Ergonomics [Oracle]:
initial heap size:
Larger of 1/64th of the machine's physical memory on the machine or some reasonable minimum. Before J2SE 5.0, the default initial heap size was a reasonable minimum, which varies by platform. You can override this default using the -Xms command-line option.
maximum heap size:
Smaller of 1/4th of the physical memory or 1GB. Before J2SE 5.0, the default maximum heap size was 64MB. You can override this default using the -Xmx command-line option.
UPDATE:
As pointed out by Tom Anderson in his comment, the above is for server-class machines. From Ergonomics in the 5.0 JavaTM Virtual Machine:
In the J2SE platform version 5.0 a class of machine referred to as a server-class machine has been defined as a machine with
- 2 or more physical processors
- 2 or more Gbytes of physical memory
with the exception of 32 bit platforms running a version of the Windows operating system. On all other platforms the default values are the same as the default values for version 1.4.2.
In the J2SE platform version 1.4.2 by default the following selections were made
- initial heap size of 4 Mbyte
- maximum heap size of 64 Mbyte
You can increase to 2GB on a 32 bit system. If you're on a 64 bit system you can go higher. No need to worry if you've chosen incorrectly, if you ask for 5g on a 32 bit system java will complain about an invalid value and quit.
As others have posted, use the cmd-line flags - e.g.
java -Xmx6g myprogram
You can get a full list (or a nearly full list, anyway) by typing java -X.
It is possible to increase heap size allocated by the JVM by using these command line options:
-Xms<size> set initial Java heap size
-Xmx<size> set maximum Java heap size
-Xss<size> set java thread stack size
In the following example, minimum heap size is set to 16mb, and the maximum to 64mb:
java -Xms16m -Xmx64m ClassName