Step-by-Step Performance Tuning


If you are running a production system where performance is critical and the database is large, you will eventually want to tweak your installation to improve tomcat and mysql performance.

Tomcat

  1. Allowed Memory:
    Three options for increasing Tomcat's allowed memory:
    • Start Tomcat with the parameters:
      -Xmx512m -Xms512m -XX:PermSize=256m -XX:MaxPermSize=256m -XX:NewSize=128m
      at the command line. (The current ANT task "Start Tomcat" will start with these options.)
    • OR, if running Tomcat as a Windows Service, open up the Tomcat Monitor (TOMCAT_HOME/bin/tomcat5w.exe) → Configure → Java → Java Options. Append:
      -Xmx512m
      -Xms512m
      -XX:PermSize=256m
      -XX:MaxPermSize=256m
      -XX:NewSize=128m
      Note: if you copy and paste the above into tomcat monitor, make sure that it doesn't insert an extra space at the end of each line, or tomcat will fail to start.
    • OR, if running Tomcat as a Linux daemon, open the /etc/init.d/Tomcat.sh script and add the the parameters to the CATALINA_OPTS:
      If CATALINA_OPTS is already set to this: CATALINA_OPTS="-Djava.library.path=/opt/tomcat/lib/.libs"
      Then change it to this:
      CATALINA_OPTS="-Djava.library.path=/opt/tomcat/lib/.libs -Xmx512m -Xms512m -XX:PermSize=256m -XX:MaxPermSize=256m -XX:NewSize=128m"
  2. Memory Leaks:
    Tomcat has a default setting that causes memory leaks. To turn it off, open the <TOMCAT HOME>/conf/web.xml file. In the jsp servlet definition add the following element:
    <init-param>
        <param-name>enablePooling</param-name>
        <param-value>false</param-value>
    </init-param>

MySQL

  1. Increase the innodb_buffer_pool_size: It is the size in bytes of the memory buffer InnoDB uses to cache data and indexes of its tables. The larger you set this value, the less disk I/O is needed to access data in tables. On a dedicated database server, you may set this to up to 80% of the machine physical memory size. However, do not set it too large because competition for physical memory might cause paging in the operating system.
  2. Increase the max_allowed_packet size: When MySQL gets a packet bigger than max_allowed_packet bytes, it issues a Packet too large error and closes the connection. Current real limit for the packet size is 1G, and practical limit is even lower.
    • Edit your INI file (e.g. for Windows, C:\Program Files\MySQL\MySQL Server x.x\my.ini).
      • You must edit the actual INI file in use. MySQL comes with several alternative configurations (INI configurations). If you use the administration program, you should be able to find the max_allowed_packet setting — I found it using MySQL Administrator under the "Health" section's "System Variables" tab (Connections → Networking → max_allowed_packet in the hierarchy). [The details may differ between versions).
    • On Linux, edit the /etc/my.cnf file
    • Under the [mysqld] section, add these lines:
    max_allowed_packet=64M
    innodb_buffer_pool_size=3G