Friday, February 15, 2013

Java vs PHP Memory Consumption

Does PHP/Apache take less memory than Java Servlets?

Often the answer to this question is yes.  PHPis rather light on memory and resource consumption.

However, I would argue that sometimes Java uses less memory than PHP under higher loads.  It is my experience that as the number of concurrent requests increases, you will reach a time in which Java uses less memory than PHP.  This happens because PHP and Java take a different approach towards handling server requests.

*Note that this post only deals with the "standard" or "common" setup of both PHP (using apache with mod_php) and Servlets (using BIO connector).  This post is an oversimplification of a very complex topic.

The Big Fat JVM.

The JVM often thought to be (and often is) a memory hog.  The JVM can easily take 100 MB of RAM and will happily consume as much memory as you are willing to give it (this rapid consumption of allocated memory is a feature that allows the garbage collection to be run less).

However, when we  look at the JVM and the memory it consumes, we have to realize exactly what it gives us. Within that single JVM process you are running the following:
  1. App Web Server (such as tomcat).  The server runs within the JVM process and is not a separate process.   A startup Tomcat will only add a few megs to the memory heap.
  2. Code Cache:  this space holds the compilation of bytecode.  JSP/Servlets are not recompiled at every call, but held in the code cache.  For php this is often a separate module or extra install, but in Java it is built into the JVM memory space.
  3. Application Scope Objects : the ability to store large data objects.   In PHP this requires an addition module or process such as APC or memcache to store objects.
  4. Database pooling :  Database connections are not constantly created and destroyed (which creates overhead), but are shared among requests in a pool.  Shared connections often means less connections.
  5. Request Threads : In Java, the threads that handle http requests are not separate processes, but lightweight threads that run within the single JVM memory. This is most important for memory consumption.
As you can see, the JVM provides many features out of the box that would require a host of additional processes and/or modules for an equivalent PHP/Apache setup.


PHP: The little processes add up.

The last point above is really the most important, Java and PHP take a different approach to how each http request is handled.

Lets first take a look at php.  PHP forks (assuming pre-fork)  an apache process for each request.  Each apache process has its own memory space and loads all modules, the interpreter, php files, byte code, database connections, and application stack/data.   The more modules, bytcode, application memory, etc... the larger each of these processes will be.

Each process typically takes 5-30 MB, depending on how much is loaded in.  People running PHP sites often see around 20 MB per apache process (see here and here).

So, if you have 1 GB of memory allocated to these 20 MB apache processes, you can run about 50 concurrent connections.  And then you will need extra memory to run databases, APC cache, Memcache, etc.

Java: Big JVM with small request threads.

So the JVM is a big ship, but it is a ship with a lot of inertia. Lets see how a java request is handled.

When a request comes in, it is not handled by a separate process, but by a small lightweight thread.  These threads, by themselves, add almost nothing to memory, except a small (configurable) stack size and a small amount of memory added to the heap.

Unlike PHP, each thread does not need to have its own copy of files, bytcodes, compiler, runtime, databaese connections, etc, because these are all shared within the single JVM process.  Each request thread is cheap!  Thus, while it it takes quite a bit of memory to get the JVM started, each additional concurrent connection requires far less memory.

So, assuming that you had 1GB allocated for tomcat and each request thread takes about 3MB ( assuming about 1MB thread overhead  with 2 MB application data) and that the JVM aside takes around 100 MB. In this situation, you can run about 300 concurrent connections.   You can play with you own numbers to get your own estimated number of concurrent connections.


Who really cares, request times are so short, so we don't need many concurrent connections.

But the point is that it does matter.  Even if your requests are short, keep-alives will persist your connections.  A keep-alive will keep the entire process/thread until the timeout.  So if you have a keep alive of 10 second with 50 users are clicking around fairly often, then you will have 50 processes/threads alive at all times.  For small threads, this is not a big deal.  For bigger processes, this is a big deal.

You can alleviate this by removing or drastically reducing keep-alives, but this really only becomes a problem because of process size.  If one were using a thread model, one would not have to cut the keep-alive to a ridiculously small time -  which is particularly important for high response time for ajax style apps.

Give it a test.

Spin up a small VPS.  Create equivalent tests in php and java. Slam it with a testing program with keep-alives.  See which one starts thrashing the swap first.

Concluding thoughts.

This might have sounded like a knock against PHP, which it is not.  It is simply pointing out the differences between how the two web app platforms work.   Often php will require much less resources for smaller load applications.  And PHP can clearly scale (horizontally and vertically) to accomplish most any web app necessary.

However, it is my experience that as the number of concurrent requests increases, you will reach a time that Java will use less memory than PHP/apache.   Moreover, this fact is not the only (nor the primary) factor when choosing a language for a project, just something to think about.

No comments:

Post a Comment