Apache Performance Tuning: Swap Memory

Before we get into the details of Apache tuning, we need to understand what happens when a VPS server or Dedicated server goes unresponsive due to a poorly optimized configuration.

An over-tuned server is configured to allow more simultaneous requests (ServerLimit) than the server’s hardware can manage. Servers set in this manner have a tipping point, and once reached, the server will become stuck in a perpetual swapping scenario. Meaning the Kernel is stuck rapidly reading and writing data to and from the system swap file.

Swap files have read/write access speeds vastly slower than standard memory space. The swap files' latency can cause a bottleneck on the server as the Kernel attempts to read and write data faster than is physically possible or more commonly known as thrashing. If not caught immediately, thrashing spirals the system out of control leading to a system crash.

If trashing is left running for too long, it has the potential of physically harming the hard drive itself by simulating decades of reading/write activity over a short period. When optimizing Apache, we must be cautious not to create a thrashing scenario. We can accomplish this by calculating the thrashing point of the server based on several factors.

Estimate the Thrashing Point

Calculating the estimated thrashing point or ServerLimit of a server uses a simple equation:

( buff/cache - Reserved ) / Avg.Apache

  • buff/cache: The total memory used by the Kernel for buffers and cache.
  • Reserved: The amount of memory reserved for non-Apache processes.
  • Available: The difference between buff/cache and Reserved memory.
  • Avg.Apache: The average of all running Apache children during peak operational hours.

Calculating the thrashing point/ServerLimit should be done during peak operational hours and periodically reassessed for optimal performance.

The thrashing point value is equal to the number of Apache children the server can run; this applies to either threaded or non-thread children. When the number of children running in memory meets the calculated thrashing point, the server will begin to topple.

Buff/Cache Memory

On modern Linux systems, the buffer/cache can be derived using the /proc/meminfo file by adding the Buffers, Cached, and Slab statistics. Using the free command, we can quickly grab this information, as in the example below:


Don’t be fooled by the column labeled “available” we are solely looking at the memory we can reappropriate, which is the buff/cache column.

Reserved Memory

Reserved memory is a portion of memory held for other services aside from Apache. Some of the biggest contenders for additional memory outside of Apache are MySQL, Tomcat, Memcache, Varnish, and Nginx. It is necessary to examine these service configs to determine a valid reserved memory. These configs are outside the purview of this article. However, MySQL is the most commonly encountered service running with Apache. You can find tools online to help analyze and configure MySQL separate from this article.

Save 25% of the total buff/cache memory for any extra services running on the server. Examples:

  • A standard cPanel server runs several services along with Apache and MySQL. A server with these services run on the heavier side, needing 25% reserved for non-Apache services.
  • A pure Apache web node in a high capacity load-balanced configuration does not need to reserve any additional ram for other services.

Average Apache Memory

Finding the average size of Apache processes is relatively simple using the ps command to list the RSS (Resident Set Size) of all running httpd processes. Note: some distributions use “apache” instead of “httpd” for the process name.

This example uses a short awk script to print out the average instead of listing the sizes.

ps o rss= -C httpd|awk '{n+=$1}END{print n/NR}'

This example is easy to average manually, but larger servers will require more calculation.

ps o rss -C httpd

Calculate the Thrashing Point

Once collected divided the Available memory by Avg. Apache, rounding down to the nearest whole number. Available memory is the buff/cache memory minus the Reserved memory. Below is a summary of the calculation process in table form.

Variable KB Notes
buffer/cache 808435 Gathered from the free command
Reserved 202108 Rule-of-thumb is up to 25% of buffer/cache
Available 606327 buffer/cache minus Reserved
Avg Apache 24500 Obtained using ps command
Thrashing point 24 Available divided by Avg Apache

This table summarizes how to derive each variable to calculate an estimated thrashing point.

Conservative estimates are provided below for various memory configurations. These estimates can be used as a starting configuration but will require additional follow-up performance assessments during peak hours to adjust directives by the servers.

Memory Requests
0.5 GB 4
1 GB 10
2 GB 23
4 GB 46
6 GB 70
8 GB 93
12 GB 140
16 GB 187
24 GB 281
32 GB 374

Determining the best optimization point for Apache can save the server from downtime, wasted sales, and lost clients. Regulating available resources is a critical component to ensure your service stays up and running at optimal speed.

Related Posts