Optimizing Java

A crucial point to properly configure optimal performance is the GeoServer container, Tomcat, and its JVM setting. Tomcat's default startup script is configured for booting quickly, but, of course, it can't match all applications' requirements. Tuning your Java runtime parameters can greatly increase performance. There are many runtime parameters you can set at the JVM startup. In this recipe, you will set the most effective one on GeoServer performances. Note that values may vary according to the hardware configuration on your site.

Note

Unfortunately, there is no way to cut corners on the path of tuning parameters for a Java application. While the options presented in this chapter have been widely tested on GeoServer and are recommended by core developers, you should note that best options may vary depending on your scenario. A value resource to understand how each parameter works is http://www.oracle.com/technetwork/java/javase/tech/vmoptions-jsp-140102.html.

How to do it…

In the previous recipe, we created a startup script for the automated startup of GeoServer on Linux. Now, you will edit the script with proper values for Java runtime parameters. Each parameter will be briefly described in the following steps:

  1. Open the startup file we created in the previous recipe:
    $ sudo vi /etc/init.d/tomcat
    

    Note

    vi is one the most famous editor on Linux. System administrators and developers often love it for its flexibility and power. On the other hand, it has a steep learning curve and newcomers may find its command mode / insert mode dual nature uncomfortable. On Debian distributions, such as Ubuntu or Mint, you may find nano a more user-friendly console editor. It goes without saying that you can use a powerful IDE such as gedit or jEdit if you can access a desktop environment.

  2. Locate the following code line. If you didn't modify the script, it should be line number 15:
    export CATALINA_HOME=/opt/Tomcat7042
    
  3. Insert the following lines of code after it:
    HEAP="-Xms2048m -Xmx2048m"
    NEW="-XX:NewSize=128m -XX:MaxNewSize=128m"
    RMIGC="-Dsun.rmi.dgc.client.gcInterval=600000 -Dsun.rmi.dgc.server.gcInterval=600000"
    PGC="-XX:+UseParallelGC"
    PERM="-XX:PermSize=128m -XX:MaxPermSize=128m"
    DEBUG="-verbose:gc -XX:+PrintTenuringDistribution"
    DUMP="-XX:+HeapDumpOnOutOfMemoryError"
    SERVER="-server"
    
  4. Now go to the line, just after the ones you just inserted. We need to add all values you set in the JAVA_OPTS variable. JVM will read it at startup and use your values:
    export JAVA_OPTS="-Djava.awt.headless=true $HEAP $NEW $RMIGC $PGC $PERM $DEBUG $DUMP $SERVER"
  5. Save the file and restart your Tomcat server.

How it works…

We inserted a few extra parameters to the script. This tunes the JVM environment and helps to increase your server's performance. Let's explore what we added and why it is important:

  • HEAP: This parameter lets you reserve enough memory for GeoServer. It really depends on the memory availability on your system. 2 GB, as indicated, is a good figure. You may want to decrease it if you are hosting on a tiny cloud machine, where total memory size is limited:
    HEAP="-Xms2048m -Xmx2048m"
  • NEWSIZE: This parameter lets you are reserve space for new objects created by GeoServer. This values shouldn't be more that a fourth of heap; reduce it proportionally if you need to reduce your heap:
    NEW="-XX:NewSize=128m -XX:MaxNewSize=128m"
  • GARBAGE COLLECTOR: This parameter sets the frequency at which the Java garbage collector, which is used to destroy unused objects, should be run. Once every 10 minutes, as suggested in the recipe, should be more than enough:
    RMIGC="-Dsun.rmi.dgc.client.gcInterval=600000 -Dsun.rmi.dgc.server.gcInterval=600000"
  • PGC: You should also add a line to use the parallel garbage collector, which enables multithreaded garbage collection. This improves performance if more than two cores are present:
    PGC="-XX:+UseParallelGC"
  • PERM: This lets you increase the maximum size of permanent generation (or permgen) allocated to GeoServer. This is the heap portion where the class bytecode is stored. GeoServer uses lots of classes and it may exhaust that space quickly leading to out of memory errors. The code that let's you increase the size of permgen is as follows:
    PERM="-XX:PermSize=256m -XX:MaxPermSize=256m"
  • DEBUG: This parameter enables Java to perform tracing. This may greatly help if things go astray. The code for it is as follows:
    DEBUG="-verbose:gc -XX:+PrintTenuringDistribution"
  • DUMP: This parameter sets Java to create a dump of the memory state when your server ends in an out of memory case (OOM). It does not cost anything unless triggered, and may be useful to debug tricky bugs. The code that let's you achieve that is as follows:
    DUMP="-XX:+HeapDumpOnOutOfMemoryError"
  • SERVER: This parameter is used for forcing the server JVM. On most Linux systems, it is there by default. However, having it explicitly set doesn't harm. The code for it is as follows:
    SERVER="-server"
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.182.159