Memory Footprint Matters June 28, 2010

What does a smaller memory footprint mean for you? Although it does not directly translate to better performance, it allows your web site handle larger traffic. By how much? Let's look at some metrics, numbers and their meanings.

When is memory used?
The life cycle of a web page is rather short. When a request is received at the server, a chunk of memory is allocated to generate the content. The allocated memory is then discarded as the rendered content is sent back to the web browser. Depending on the nature of the web page, the rendering process usually take from a few milliseconds to a couple of seconds. If multiple requests are being handled at the same time, the server needs access to more free memory.

How much memory do my web pages need?
Different scripting languages have various ways to determine how much memory a script, or specific objects inside the script consumes. In this article we focus on PHP pages. With proper setup, the memory usage can be easily obtained by adding the following line to the end of a script (usually where the page footer is displayed):
<?php
echo memory_get_usage(true);
?>
And the number you see is the amount of memory, in bytes, the script requires to run.

Now let's convert this number to kilobytes (KB) by dividing it by 1024. Then divide this number again by 256. For example
1310720 / 1024 / 256 = 5
By default, PHP allocates memory in increments of 256K. Each increment is a "segment". This value can be set via the ZEND_MM_SEG_SIZE variable.

The number of 256K segments required to run a script gives us a convenient way to remember and compare the memory requirement in various web applications. Some open source applications are measured in Windows/Apache2/PHP5:

Gyroscope (sample app)x1
phpMyAdminx7
Code Ignitorx19
Joomlax55
Word Pressx66
OpenXx87


If you would like to add more applications to the above list, please contact us.

It's not just about sizes
An application that has more features has more complex implementation. This doesn't mean, however, that every page in this application should bear the same overhead. In the above example, the login page of OpenX alone takes 87 segments! In order to make a page only consume what it really needs, the program has to be properly modularized. So reducing memory footprint is not just about sizes, it's about thoughtful architecture.

Can your server handle the traffic?
Assuming all your server memory can be used to generate web pages, how many concurrent requests can you process? For a page that takes 10 segments, a 512M server can handle 205 requests simultaneously. If the page uses only 1 segment, 2048 requests can be process at once, assuming no other bottlenecks. The difference is day and night.

Memory usage isn't everything
As pointed out earlier, smaller memory footprint doesn't mean better performance. In the context of interpreted web language such as PHP, however, the script execution is often shorter (better). In addition to memory usage, the rendered content size, number of network connections, complexity of SQL queries and client-side computation requirement also deserve great attention. We'll cover these topics in future articles.

Our Services

Targeted Crawlers

Crawlers for content extraction, restoration and competitive intelligence gathering.

Learn More

Gyroscope™ ERP Solutions

Fully integrated enterprise solutions for rapid and steady growth.

Learn More

E-Commerce

Self-updating websites with product catalog and payment processing.

Learn More

Featured Download

Twitter Feed Plugin v1.1

Display live twitter feed on your website using this stand-alone plugin.

Download    Documentation

Train Slider

A sliding train banner widget that offers an advanced effect without costing a cent more resource.

Download    Documentation

Easy Rotating Banners

If you just want to put a rotating banner on your website, you don't need a bloated JavaScript library. Keep your site slim with this stand-alone banner plugin.

Download    Documentation