331

I am working on a Windows 2003 server (64-bit) with 8 GB RAM. How can I increase the heap memory maximum? I am using the -Xmx1500m flag to increase the heap size to 1500 Mb. Can I increase the heap memory to 75% of physical memory (6 GB Heap)?

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
Sunil
  • 4,133
  • 6
  • 23
  • 21

13 Answers13

373

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.

Manuel Jordan
  • 15,253
  • 21
  • 95
  • 158
Steve B.
  • 55,454
  • 12
  • 93
  • 132
  • 20
    It has been my experience that the actual heap size Java will accept using a 32b VM (on a 32b or 64b system -- the VM is the important part, here), is around 2G. Also, surprised asker did not first search and find: http://stackoverflow.com/questions/1596009/java-lang-outofmemoryerror-java-heap-space and: http://stackoverflow.com/questions/37335/how-to-deal-with-java-lang-outofmemoryerror-java-heap-space-error-64mb-heap-s – wsorenson Nov 11 '09 at 04:12
  • 3
    ah, you're right of course - on a 32 bit system you can only address 2 Gb of memory. – Steve B. Aug 14 '12 at 14:18
  • Is it necessary for max heap and minimum heap size to be the same ? – user1747935 Feb 12 '14 at 17:14
  • 1
    No. But people commonly do it because the JVM can never waste time resizing. Doubt that it matters much, TBH. – Steve B. Feb 12 '14 at 19:17
  • Is it possible to do this without using the command line? Actually, I am using a python library which uses a jar file. It terminates due to insufficient memory during runtime. It will be helpful if there is a way to set this size before I execute my script. – Raghuram Vadapalli Jun 16 '16 at 18:23
  • Yes, it is possible to set the heap size without using the command line. One way is setting the environment variable `JAVA_OPTS`. For instance, `export JAVA_OPTS="-Xmx2G"`. – laylaylom Jul 20 '17 at 17:51
  • This line does what? I'm still getting out of memory exceptions. Downvote. – Philip Rego Jan 16 '19 at 16:48
  • @Phillip Rego it sets your heap size. There's no size that will guarantee that you won't get OutOfMemory - it kindof depends on your program. – Steve B. Jan 16 '19 at 17:17
  • Sets it to what though? – Philip Rego Jan 16 '19 at 17:27
  • 2
    @PhilipRego you set it to whatever you want, up to the limits the JVM can address. -Xmx2G sets your memory to 2G (also look at -Xms). This doesn't say anything at all about how much memory your going to need. – Steve B. Jan 17 '19 at 20:30
  • oh 2 gigabytes. – Philip Rego Jan 17 '19 at 20:47
  • Are there java config files that can do this? I'd rather not set another environment variable if I can help it. – enigmaticPhysicist Jul 25 '21 at 03:20
  • 1
    JAVA_OPTS didn't work for me. JAVA_TOOL_OPTIONS did. – enigmaticPhysicist Jul 27 '21 at 01:44
185

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
Lee Goddard
  • 10,680
  • 4
  • 46
  • 63
Karthik Reddy
  • 2,902
  • 1
  • 18
  • 9
31

On a 32-bit JVM, the largest heap size you can theoretically set is 4gb. To use a larger heap size, you need to use a 64-bit JVM. Try the following:

java -Xmx6144M -d64

The -d64 flag is important as this tells the JVM to run in 64-bit mode.

Kevin
  • 30,111
  • 9
  • 76
  • 83
  • Thank you boss but it is not working in my system there is some thing else I am missing – Sunil Oct 14 '09 at 14:21
  • 5
    Define "not working". Does it not start and give you an error? Does it start but use a different heap size? – Kevin Oct 14 '09 at 14:50
18

You can increase the Heap Size by passing JVM parameters -Xms and -Xmx like below:

For Jar Files:

java -jar -Xms4096M -Xmx6144M jarFilePath.jar

For Java Files:

 java -Xms4096M -Xmx6144M ClassName

The above parameters increase the InitialHeapSize (-Xms) to 4GB (4096 MB) and MaxHeapSize(-Xmx) to 6GB (6144 MB).

But, the Young Generation Heap Size will remain same and the additional HeapSize will be added to the Old Generation Heap Size. To equalize the size of Young Gen Heap and Old Gen Heap, use -XX:NewRatio=1 -XX:-UseAdaptiveSizePolicy params.

java -jar -Xms4096M -Xmx6144M -XX:NewRatio=1 -XX:-UseAdaptiveSizePolicy pathToJarFile.jar

-XX:NewRatio = Old Gen Heap Size : Young Gen HeapSize (You can play with this ratio to get your desired ratio).

Sahil Chhabra
  • 10,621
  • 4
  • 63
  • 62
13

It is possible to increase heap size allocated by the JVM in eclipse directly In eclipse IDE goto

Run---->Run Configurations---->Arguments

Enter -Xmx1g(It is used to set the max size like Xmx256m or Xmx1g...... m-->mb g--->gb)

Maks3w
  • 6,014
  • 6
  • 37
  • 42
Karthik Reddy
  • 2,902
  • 1
  • 18
  • 9
11

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 (here java file name is : HelloWorld.java)

Sumit Deshinge
  • 121
  • 1
  • 3
8

Please use below command to change heap size to 6GB

export JAVA_OPTS="-Xms6144m -Xmx6144m -XX:NewSize=256m -XX:MaxNewSize=356m -XX:PermSize=256m -XX:MaxPermSize=356m"
Baum mit Augen
  • 49,044
  • 25
  • 144
  • 182
  • If I have 8G memory in my system and I can see from task manager that alsways 70% of that is in use; is this possible to allocate 6G memory to java heap size? – Reihan_amn Jan 18 '17 at 15:27
  • 1
    @Reihan_amn Yes and no, if you exceed your memory it will go to paging, which is on the disk and incredibly slow. If you exceed the page files then you're in for a bad time. – CodeMonkey Jan 03 '19 at 22:06
  • If this export cmd is used.... then will any java program by default be limited to 6Gb unless -Xm* values are specified at the java call? – Jose Cabrera Zuniga Jul 21 '22 at 20:38
7

Can I increase the heap memory to 75% of physical memory(6GB Heap).

Yes you can. In fact, you can increase to more than the amount of physical memory, if you want to.

Whether it is a good idea to do this depends on how much else is running on your system. In particular, if the "working set" of the applications and services that are currently running significantly exceeds the available physical memory, your system is liable to "thrash", spending a lot of time moving virtual memory pages to and from disk. The net effect is that the system gets horribly slow.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
3

Several people pointed out the specific answers for heap size with the jvm options of -Xms and -Xms. I want to point out that this is not the only type of memory options for the jvm. Specifically if you are get stack over flows, then you'll want to increase the size of the stack by adding an additional option like -Xss8m.

For this problem, the jvm options of something like -Xms2g -Xmx6g -Xss8m would be a solution.

I'm sharing this information as my google searches on how to increase jvm memory took me to this solution, and the solutions didn't work with high amounts of memory allocation. Once I figured out what the specific settings were for, I was able to google how to increase the stack size and found the missing param. :) Hope this saves others time, as it would of saved me a ton of time. :)

James Oravec
  • 19,579
  • 27
  • 94
  • 160
2

This only works with 64 bit version of Java. Go to Control Panel and click on the Java icon. On the small window of Java Control Panel, click on the Java menu bar and then click on view button.

If you have two Java platforms, disable the previous version of Java, then click on Runtime parameters text field and write -Xmx1024m or less than RAM size. Don't increase heap size equal to RAM otherwise your system will crash.

Eldelshell
  • 6,683
  • 7
  • 44
  • 63
madhu
  • 53
  • 1
  • If you make the heap size equal to RAM, the system will not crash. Instead, the system will start paging and get nice and slow. – DaveTheMinion Sep 19 '19 at 19:38
1

Yes. You Can.

You can increase your heap memory to 75% of physical memory (6 GB Heap) or higher.

Since You are using 64bit you can increase your heap size to your desired amount. In Case you are using 32bit it is limited to 4GB.

$ java -Xms512m -Xmx6144m JavaApplication

Sets you with initial heap size to 512mb and maximum heapsize to 6GB.

Hope it Helps.. :)

Sarat Chandra
  • 5,636
  • 34
  • 30
1

I have problem running the py files in my java code using eclipse/STS, getting PyException due to insufficient jvm heap memory. I have done the changes as mentioned below and I'm able to resolve this issue. Below is my System configuration.

enter image description here

And these are the changes I did in my workspace and voila it runs perfect now.

enter image description here enter image description here

Suresh
  • 1,491
  • 2
  • 22
  • 27
-2

Here are the steps if someone wants to know how to do this in windows.

Jagath01234
  • 139
  • 1
  • 10