Java initial heap size maximum heap size

How to control Java heap size (memory) allocation (xmx, xms)

Java/Scala memory FAQ: How do I control the amount of memory my Java program uses (i.e., Java RAM usage)?

Java RAM: Short answer

The short answer is that you use these java command-line parameters to help control the RAM use of application:

  • Use -Xmx to specify the maximum heap size
  • Use -Xms to specify the initial Java heap size
  • Use -Xss to set the Java thread stack size

Use this syntax to specify the amount of memory the JVM should use:

-Xms64m or -Xms64M // 64 megabytes -Xmx1g or -Xmx1G // 1 gigabyte

A complete java command looks like this:

Читайте также:  Http clients khnp aoil ru card info html

See the rest of this article for more details. Also see my Java heap and stack definitions if you’re not comfortable with those terms.

Java RAM: The longer answer

As a bit of background, I’m running a Java application on a Raspberry Pi device where memory is limited. Unfortunately, every time I try to run the program I get this Java heap size error message:

“Error occurred during initialization of VM. Could not reserve enough space for object heap. Could not create the Java virtual machine.”

I knew my program doesn’t need a lot of memory — it was just hitting a database and generating some files and reports — so I got around this memory limit problem by specifying the maximum Java heap size my program was allowed to allocate. In my case I didn’t think about it too hard and just chose a heap size limit of 64 MB RAM, and after I set this RAM limit my program ran fine.

Setting the maximum Java heap size (Xmx)

You set the maximum Java heap size of your program using the -Xmx option to the Java interpreter. To specifically limit your heap size to 64 MB the option should be specified like this:

Using that memory limit setting, the Java command I use in my shell script to start my Java program looks like this:

where THE_CLASSPATH and PROGRAM_NAME are variables set earlier in my script. (The important part here is the -Xmx64m portion of the command.)

You can find more options for controlling Java application memory use by looking at the output of the java -X command. Here’s what the output of those commands looks like from my JVM:

$ java -X -Xmixed mixed mode execution (default) -Xint interpreted mode execution only -Xbootclasspath: set search path for bootstrap classes and resources -Xbootclasspath/a: append to end of bootstrap class path -Xbootclasspath/p: prepend in front of bootstrap class path -Xnoclassgc disable class garbage collection -Xloggc: log GC status to a file with time stamps -Xbatch disable background compilation -Xms set initial Java heap size -Xmx set maximum Java heap size -Xss set java thread stack size -Xprof output cpu profiling data -Xfuture enable strictest checks, anticipating future default -Xrs reduce use of OS signals by Java/VM (see documentation) -Xdock:name= override default application name displayed in dock -Xdock:icon= override default icon displayed in dock -Xcheck:jni perform additional checks for JNI functions -Xshare:off do not attempt to use shared class data -Xshare:auto use shared class data if possible (default) -Xshare:on require using shared class data, otherwise fail. The -X options are non-standard and subject to change without notice. 

From that list, the command-line arguments specifically related to Java application memory use are:

-Xnoclassgc disable class garbage collection -Xms set initial Java heap size -Xmx set maximum Java heap size -Xss set java thread stack size 

Java heap size descriptions (xms, xmx, xmn)

Digging around, I just found this additional Java xms , xmx , and xmn information on Apple’s web site:

-Xms size in bytes Sets the initial size of the Java heap. The default size is 2097152 (2MB). The values must be a multiple of, and greater than, 1024 bytes (1KB). (The -server flag increases the default size to 32M.) -Xmn size in bytes Sets the initial Java heap size for the Eden generation. The default value is 640K. (The -server flag increases the default size to 2M.) -Xmx size in bytes Sets the maximum size to which the Java heap can grow. The default size is 64M. (The -server flag increases the default size to 128M.) The maximum heap limit is about 2 GB (2048MB).

Java memory arguments (xms, xmx, xmn) formatting (MB, GB)

When setting the Java heap size, you should specify your memory argument using one of the letters “m” or “M” for MB, or “g” or “G” for GB. Your setting won’t work if you specify “MB” or “GB.” Valid arguments look like this:

Also, make sure you just use whole numbers when specifying your arguments. Using -Xmx512m is a valid option, but -Xmx0.5g will cause an error.

Источник

Garbage Collector Ergonomics

Garbage Collector of Server VM Changed to Parallel Garbage Collector

On server-class machines running the server VM, the garbage collector (GC) has changed from the previous serial collector ( -XX:+UseSerialGC ) to a parallel collector ( -XX:+UseParallelGC ). You can override this default by using the -XX:+UseSerialGC command-line option to the java command.

Initial Heap Size and Maximum Heap Size Changed for Parallel Garbage Collector

On server-class machines running either VM (client or server) with the parallel garbage collector ( -XX:+UseParallelGC ) the initial heap size and maximum heap size have changed as follows.

initial heap size

Larger of 1/64th of the machine’s physical memory on the machine or some reasonable minimum. Before Java SE 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 Java SE 5.0, the default maximum heap size was 64MB. You can override this default using the -Xmx command-line option.

Note: The boundaries and fractions given for the heap size are correct for Java SE 5.0. They are likely to be different in subsequent releases as computers get more powerful.

Parallel Garbage Collector Throws Exception if Excessive Amount of Time Spent Collecting Small Amount of Heap

The parallel garbage collector ( UseParallelGC ) throws an out-of-memory exception if an excessive amount of time is being spent collecting a small amount of the heap. To avoid this exception, you can increase the size of the heap. You can also set the parameters -XX:GCTimeLimit= time-limit and -XX:GCHeapFreeLimit= space-limit where:

The upper limit on the amount of time spent in garbage collection in percent of total time (default is 98).

The lower limit on the amount of space freed during a garbage collection in percent of the maximum heap (default is 2).

Implementation of -XX:+UseAdaptiveSizePolicy Used by Parallel Garbage Collector Changed

  • a desired maximum GC pause goal
  • a desired application throughput goal
  • minimum footprint

The implementation checks (in this order):

  1. If the GC pause time is greater than the pause time goal then reduce the generations sizes to better attain the goal.
  2. If the pause time goal is being met then consider the application’s throughput goal. If the application’s throughput goal is not being met, then increase the sizes of the generations to better attain the goal.
  3. If both the pause time goal and the throughput goal are being met, then the size of the generations are decreased to reduce footprint.

Flags

-XX:MaxGCPauseMillis= nnn A hint to the virtual machine that pause times of nnn milliseconds or less are desired. The VM will adjust the java heap size and other GC-related parameters in an attempt to keep GC-induced pauses shorter than nnn milliseconds. Note that this may cause the VM to reduce overall throughput, and in some cases the VM will not be able to meet the desired pause time goal.

By default there is no pause time goal. There are definite limitations on how well a pause time goal can be met. The pause time for a GC depends on the amount of live data in the heap. The minor and major collections depend in different ways on the amount of live data. This parameter should be used with caution. A value that is too small will cause the system to spend an excessive amount of time doing garbage collection.

-XX:GCTimeRatio= nnn A hint to the virtual machine that it’s desirable that not more than 1 / (1 + nnn) of the application execution time be spent in the collector.

For example -XX:GCTimeRatio=19 sets a goal of 5% of the total time for GC and throughput goal of 95%. That is, the application should get 19 times as much time as the collector.

By default the value is 99, meaning the application should get at least 99 times as much time as the collector. That is, the collector should run for not more than 1% of the total time. This was selected as a good choice for server applications. A value that is too high will cause the size of the heap to grow to its maximum.

Suggested strategy

Do not choose a maximum value for the heap unless you know that the heap is greater than the default maximum heap size. Choose a throughput goal that is sufficient for your application.

In an ideal situation the heap will grow to a value (less than the maximum) that will support the chosen throughput goal.

If the heap grows to its maximum, the throughput cannot be met within that maximum. Set the maximum heap as large as you can, but no larger than the size of physical memory on the platform, and execute the application again. If the throughput goal can still not be met, then it is too high for the available memory on the platform.

If the throughput goal can be met but there are pauses that are too long, select a pause time goal. This will likely mean that your throughput goal will not be met, so choose values that are an acceptable compromise for the application.

Источник

[Fixed] Initial heap size set to a larger value than the maximum heap size

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.

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 .

C:\Users\Arpit\Desktop\javaPrograms>java -Xms2g -Xmx1g org/arpit/java2blog/PrintArrayListMain
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.

C:\Users\Arpit\Desktop\javaPrograms>java -Xms512m -Xmx1024m org/arpit/java2blog/PrintArrayListMain
[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:

Источник

Оцените статью