PHP and Java – Instances or Threads
Having recently set off on this quest to discover the truth about how fast PHP is vs ColdFusion / Java, I've come across some interesting information about how either goes about processing its data. The biggest concern over which of the technologies to adopt is always scalability, and that comes down to load distribution and memory.
Java goes about its business by using threads. What this means is that a JSP site will spin off a new thread from its main instance whenever a new request is made. This is extremely efficient in terms of memory usage, and usually will reult in much less need for the server to go to the hard drive in the event of overflowing its hard-wired memory. PHP goes about things in a much different way. If Java is Windows 2000, PHP is Windows 98, perhaps that is a bad analogy, but the way it works is that PHP will clone a version of itself into memory for each request it receives. Similar to the way Windows 98 would block off a chunk of memory in which a single application would run, independent of the other applications. It is probably a bad analogy because you can think of PHP as spinning off entire new OSs every time it needs to run a program. People often say that this method of serving is inefficient, but I have seen so many memory leaks from containers over the time I have been developing to say that of either Java seems to be more inefficient, this has gotten better with each revision of Java however, since each one further hones its garbage collection algorithm. Still, even if you look at the maximum installation size of PHP with all its plugins, the program's footprint is about 6 to 7 MB per instance. Not that bad when you think about it, since most servers will have around 2 GB of memory, it can serve around 200 users concurrently with few problems.
Under load, well written JSP will probably due a little better, since it will be better able to distribute its load to hyper-threaded CPUs, but still in practice PHP seems at least as fast as Java. Still, without garbage collection, PHP users will have to do some of it on their own, especially with recordset data. But if too much memory is gobbled up, PHP will dump that instance and start again with little to no interruption to the user. Apache is the same way, for each incoming request it spins itself off. The best thing about this process is that it serves as a type of sandbox where users can really only mess up their own instance, but can rarely effect others. I think that for web applications, PHP / Apache has the edge with their instance management. With a little better thread management, PHP could be really robust. I look forward to seeing the first few PHP application servers, perhaps I'll try to write on myself. They should be pretty interesting.