How to Increase Heap Size in Java Virtual Machine?
There are two types of memory stack memory and heap memory. All the dynamic allocations go into heap memory and the rest of the static allocations and variables allocations go into stack memory. Whenever a java program is executed in the java virtual machine it uses the heap memory to manage the data. Task Engine runs on JVM (Java Virtual Machine). Hierarchy is pictorially shown below.
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
So now coming onto heap memory, the heap is a location in memory used by the Java Virtual Machine (JVM). The Java Virtual Machine has a heap that is shared among all Java Virtual Machine threads. The heap is the run-time data area from which memory for all class instances and arrays is allocated. The heap is created on a virtual machine start-up. Heap storage for objects is reclaimed by an automatic storage management system (known as a garbage collector); objects are never explicitly reallocated. The Java Virtual Machine assumes no particular type of automatic storage management system, and the storage management technique may be chosen according to the implementor’s system requirements. The heap may be of a fixed size or may be expanded as required by the computation and may be contracted if a larger heap becomes unnecessary. The memory for the heap does not need to be contiguous.
A Java Virtual Machine implementation may provide the programmer or the user control over the initial size of the heap, as well as, if the heap can be dynamically expanded or contracted, control over the maximum and minimum heap size. The following exceptional condition is associated with the heap whenever a computation requires more heap than can be made available by the automatic storage management system, the Java Virtual Machine throws an OutOfMemoryError.
By default, the JVM heap size is 1GB, which is usually enough to accommodate the data used by Task Engine. However, larger heap size may be required under some circumstances, for example, when the average size of the parameters in a task is very large. Under these circumstances, the following log item is recorded in Task Engine log files. If this log item appears regularly, you need to increase the heap size.
Now the question arises of how to increase the heap size of memory. So a common solution to it Now in order to change/increase the JVM heap size is made possible by using command-line options.
-Xms : To set an initial java heap size -Xmx : To set maximum java heap size -Xss : To set the Java thread stack size -Xmn : For setting the size of young generation, rest of the space goes for old generation
Procedure: To increase the Application Server JVM heap size
- Log in to the Application Server Administration Server.
- Navigate to the JVM options.
- Edit the -Xmx256m option.
- This option sets the JVM heap size.
- Set the -Xmx256m option to a higher value, such as Xmx1024m.
- Save the new setting.
The process to change the heap size of the Web Server is the same you just need to log in in Web Server Administration Server. After that process is the same. It is good practice for big production projects to set the minimum -Xms and maximum -Xmx heap sizes to the same value. For efficient garbage collection, the -Xmn value should be lower than the -Xmx value. Heap size does not determine the amount of memory your process uses.