In this post, we will how to resolve initial heap size set to a larger value than the maximum heap size
in java.
Table of Contents
Problem: Initial heap size set to a larger value than the maximum heap size
This error is related to Xmx and Xms parameters in java and these parameters are used to provide heap memory of the JVM.
-Xms
: This specifies initial heap size for JVM
-Xmx
: This specifies maximum heap memory size for JVM
This simply means JVM will start with mimimum Xms amount of memory and can take up to xmx amount of memory.
Let’s reproduce this error now.
We will take a simple example about how to print arraylist in java.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
package org.arpit.java2blog; import java.util.ArrayList; public class PrintArrayListMain { public static void main(String[] args) { ArrayList<String> countryList=new ArrayList<>(); countryList.add("India"); countryList.add("China"); countryList.add("Bhutan"); System.out.println(countryList); } } |
Let’s compile and run the code.
C:\Users\Arpit\Desktop\javaPrograms>java -Xms1024m -Xmx512m org/arpit/java2blog/PrintArrayListMain
Error occurred during initialization of VM
Initial heap size set to a larger value than the maximum heap size
As you can see, we got the error Initial heap size set to a larger value than the maximum heap size
.
Error occurred during initialization of VM
Initial heap size set to a larger value than the maximum heap size
Solution 1: Initial heap size set to a larger value than the maximum heap size
We are getting this error because Xms(minimum heap size) is greater than Xmx(maximum heap size).
Xms should always be lesser than Xmx to run java program correctly.
Let’s change -Xms to 512m and -Xmx to 1024m now.
[India, China, Bhutan]
As you can see, error is resolved now and we got expected output.
That’s all about initial heap size set to a larger value than the maximum heap size
in java.
Solution 2: Change Xms value in vmoptions file for intellij
If you are getting this error in intellij, then you should check Xms value in vmoptions
file.
On windows:
You should be able to find the file on windows at following path:
1 2 3 |
C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2020.2.3\bin |
On Mac:
You should be able to find the file on mac at following path:
1 2 3 |
~/Library/Preferences/IntelliJIdea2019.2/idea64.vmoptions |
On linux:
You should be able to find the file on linux at following path:
1 2 3 |
~/.IntelliJIdea2019.2/config/idea64.vmoptions |
You should change version based on your intellij version in above paths.
Check the value of Xms in idea64.vmoptions
file and if it is greater than Xmx or not. If it is greater than Xmx, then we should fix it.
Here is content of idea64.exe.vmoptions
file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
-server -Xms128m -Xmx512m -XX:ReservedCodeCacheSize=240m -XX:+UseConcMarkSweepGC -XX:SoftRefLRUPolicyMSPerMB=50 -ea -XX:CICompilerCount=2 -Dsun.io.useCanonPrefixCache=false -Djdk.http.auth.tunneling.disabledSchemes="" -XX:+HeapDumpOnOutOfMemoryError -XX:-OmitStackTraceInFastThrow -Djdk.attach.allowAttachSelf=true -Dkotlinx.coroutines.debug=off -Djdk.module.illegalAccess.silent=true |
That’s all about how to fix Initial heap size set to a larger value than the maximum heap size
in java.
Thanks for the help! Really helped me out!