Saturday 8 August 2020

Initial heap size set to a larger value than the maximum heap size

 What is heap?

Heap is a portion of memory where dynamic allocation and deallocations happen.

 

Can I set minimum and maximum heap sizes to my java application?

Yes, you can set using the options -Xms, -Xmx.

 

Option

Description

-Xms

Set initial and minimum heap size

-Xmx

Set maximum heap size

 

Syntax

-Xms<size>[g|G|m|M|k|K]

-Xmx<size>[g|G|m|M|k|K]

 

Example

java -Xms1g -Xmx2g App

 

Above statement sets minimum heap size to 1Gb and maximum to 2Gb.

 

Error ‘Initial heap size set to a larger value than the maximum heap size’ occurs when you set minimum heap size values is set more than maximum heap size.

 

Example

java -Xms2g -Xmx1g App

 

As you see above statement, I set the minimum heap size to 2Gb and maximum heap size to 1Gb. This is completely wrong setting, minimum size should always less than maximum size..

 

App.java

public class App {

	public static void main(String args[]) {
		System.out.println("Hello World");
	}
}

Compile and run the above program, you will get the error ‘Initial heap size set to a larger value than the maximum heap size‘.

$javac App.java
$
$java -Xms2g -Xmx1g HelloWorld
Error occurred during initialization of VM
Initial heap size set to a larger value than the maximum heap size

How to resolve this error?

Set the minimum heap size always less than the maximum heap size.

 

$java -Xms1g -Xmx2g App

Hello World


You may like?

No comments:

Post a Comment