A person may run into some memory issues while running his Java programs. One of the most common errors that occur while running a Java program is the OutOfMemoryError which occurs when the heap size that is allocated to the Java program is no longer sufficient to hold the objects that are generated by the Java program. To remedy such errors you may need to do a little tweaking in the Java command line and fix those heap sizes with the help of the Xmx and Xms command line options.
With much unique functionality, Java is also used to code android applications due to its user friendliness and its ability to equip them with powerful coding abilities. You can take this course on android and how Java can be essential in coding Android applications.
Java Garbage Collection (GC) Mechanism
To understand command line tweaking let’s have a little refresher on the Java Garbage Collection mechanism. The Java Garbage Collector works by clearing the memory heap of unreferenced objects. The mechanism is inbuilt in Java and the Java Garbage Collection methods take care of all the nuances of freeing the memory of unused objects. The GC mechanism works by keeping a track of all the live objects that are used by the program. The live object can be traversed through a GC root. Any other object is treated as garbage, which will be deleted eventually from the memory heap through the GC mechanism. The GC mechanism identifies unreferenced objects, deletes them and performs compaction so that the live objects can be accessed serially; the compaction process is important as it releases random free spaces in memory and allows the memory allocator to allocate memory serially to new objects. If you are finding it difficult to comprehend most of the concepts given in the article, you may consider this course that will teach you Java from the bottom-up.
The Xmx Command Line Option
The Xmx option is used in the command line to set the maximum heap size that is to be used by the Java application. The Xms option is used to set the lowest heap memory that is to be used by the application. For optimal memory usage, the heap size should be set between 40% and 70%. The Java memory management algorithms can then manage the heap size for optimal performance of the application. If the heap size is too low, the application will continuously throw an OutOfMemoryError exception and if the heap size is too large then the garbage collection procedure will be called too often, which will in turn lead to low performance.
To make the optimal use of the memory that is provided by the operating system to the Java program, you may need to understand the concept of Java heap and Native heap. The operating system allots a part of memory to the Java application. That part is divided into two parts: Java heap and the Native heap. The more you set the value of the Java heap through the use of the Xmx command, the lesser the Native memory space becomes. Less space to the Native heap may lead to unnecessary OutOfMemoryError. Thus, it is recommended that you assign the Java heap optimal space so that the program functions properly without any issues. For more details on assigning heap sizes depending on various operating systems you may go to the IBM public tutorial on sizing the Java heap.
Benefits of Allocating Memory in Java
Although, more memory can lead to more GC routines and less memory can lead to frequent OutOfMemoryError, allocating more memory to a Java application has its advantages:
- Allocating more memory to the Java application helps you get rid of the OutOfMemoryError or the OutOfMemoryException.
- Since garbage collection is a ‘Stop the world event’ i.e. all the program threads are put to a stop when a GC routine is up, the lesser times the GC routine is run, lesser the program completion time. The young heap garbage collection routine is run when the young generation is full. Hence, if you decrease the frequency of the minor garbage collection routine by increasing the size of the young heap, you increase program performance considerably.
- The old generation may become full time-and-again if the program uses objects that stay alive for longer periods of time. And since the major garbage collection routine takes up a lot of time, it is not feasible to let it run N number of times, unperturbed. Thus, if you increase the old generation heap size to accommodate those long living objects, you may increase program performance considerably.
The basic Xmx command and other memory management commands in java are as follows:
- Xms <size>
The command helps to fix the minimum heap size that the Java programs may use.
- Xmx <size>
The command fixes the upper limit of the heap size that can be used by the Java programs.
The setting provides the recommended minimum amount of heap that should be free after a GC routine. For example, let us give the minimum heap free ratio n=0.4 (40%). If the heap is 80% full and can be cleaned to about 10%, only 60% of the heap will be cleaned, leaving 40% as it is. Although, 30% more cleaning could have been achieved, the GC operation does not clean that 30% space. This helps the GC routine to finish beforehand, so that the program can resume operation without wasting much time. The recommended value for this is 40%. To understand these commands in depth you should take this course on advanced JAVA to explore more.
The setting establishes an upper limit, as a percentage, to which the heap could be full after each GC routine. The recommended value for this is 70%.
N decides the ratio of the young generation to that of the old generation heap size. The ratio will help you tweak the size of these two very important heap sizes. For example, suppose a program creates objects that live very long and thus most of its objects fall in the old generation as compared to the younger generation. Thus, you can increase the size of the old generation heap to make the triggering of the GC routine less frequent. This can help you increase program performance considerably.
In a nutshell, Java Xmx is a powerful command that helps the users manage the memory, used by the Java applications, to fine tune their programs. To use the command you should have the exact idea about the memory requirements of your program so that you can properly assign space to your programs for optimal behavior. Java offers much powerful functionality to its users that other languages do not. Java is so popular in the development circles that you should definitely take this course on coding android programs which are used in cell phones.