Take the 2-minute tour ×

I have zero experience building website with Java. Recently I need to develop complex web application.

Currently I'm converting PHP apps to get it works in PHP 5.4. With modern website requirement, PHP application needs better frameworking from start. Use Zend framework or Drupal 7 looks good move.

But Zend or Drupal 7 comes with a price. 300MB VPS seems not relevant anymore for an average PHP website. In this case, Resource usage rate is very important to me.

Because lots of complex web application is built with Java web framework, I just wonder how high is Java resource usage (memory & CPU) compared with PHP 5.4?

I hope it will be same or even better, so that I have more choice.

share|improve this question

closed as off topic by Dagon, Mitch Wheat, Makoto, pst, kapa Jun 12 '12 at 8:17


with the price of both so low, it would generally not be a consideration in choosing which to use. –  Dagon Jun 12 '12 at 2:38

2 Answers 2

up vote 3 down vote accepted

This should provide some info about resource and cpu usage of both languages

enter image description here

Basically, based in that series of test (and until this date) Java memory consumption is in average bigger than PHP, but execution time tends to be faster

share|improve this answer
In the event that link dies, please provide the pertinent information from your link. –  Josh Jun 12 '12 at 2:46
IMHO, The memory and cpu usage is usually not an issue and relatively simple to solve (buy more hardware or optimise your code) What is more valuable is your time, and the number of lines of code is an indicator of this. One way to improve productivity in Java is to use a good IDE like IntelliJ which can fill in much of the relatively verbose code. –  Peter Lawrey Jun 12 '12 at 6:31

Overall memory usage will depend much more upon what frameworks you use and how well you code your application than whether you write it in PHP or Java. The baseline memory cost of both is small enough that you can ignore it on modern machines, so it is the application that matters.

In terms of CPU usage, Java will ultimately perform much better since it is JIT-compiled to native code and has an efficient statically typed in-memory object representation (This benchmark shows Java as over 25x than PHP on a set of benchmark programs)

Having said that, either would be probably be fine for most normal web applications. My advice would be:

  • Given you are familiar with PHP you may want to stick with it simply because the learning curve of picking up a new language and associated frameworks is significant.
  • If you are serious about developing a robust, high performance enterprise-grade application then it would certainly be worth learning and using Java. But you have to be sure that you are prepared to face a big learning curve.

Some interesting links:

share|improve this answer
Java uses much more memory that PHP, in almost every case. "How well you code your application" will not change that, mainly because the standard JVM never releases memory, and uses a huge amount in order to get good performance. –  Brendan Long Jun 12 '12 at 3:18
This statement is misleading: "the standard JVM never releases memory". It's true that java never shrinks the size of it's heap. It only increases the heap size as it needs it up to the maximum allotted to it. However, it does free sections of the heap for re-use by other objects. In fact, most apps use jvm flags -Xmx and -Xms to fix the heap size at a specific value so that the heap isn't constantly re-allocating as you move past the currently allocated heap size. This is pretty standard for any app deployment and is a good optimization technique. –  Matt Jun 12 '12 at 3:41
@Brendan - You comment is very misleading. The JVM pre-allocates large chunks of memory then manages this memory itself and releases / caches objects as needed within this space, and is generally very efficient at doing so (much more so than PHP). Unless you are doing advanced tuning, you simply don't need to worry about it. And if you want the JVM as whole to "release memory" then you are definitely doing something wrong - in a web application server so you want to give the JVM all your available memory. –  mikera Jun 12 '12 at 4:32
@Matt - Maybe I should have said "never releases memory back to the operating system". This is not the behavior I want. In Java, you tell it how much memory it's allowed to use, and it will use all of it, all the time. If the amount you allow it to use is too low, it will crash. There's no way to tell it "use this amount of memory if you need it". However, I didn't mean to imply that it has memory leaks though -- Java's garbage collector works fine. I only mean that it doesn't handle memory nicely. –  Brendan Long Jun 12 '12 at 14:23
And to clarify -- I use Java almost exclusively at work because it's a good language, but I can't use it on my VPS because in some situations, the JVM will need a huge amount of memory for a short time (fine), but then it won't release that memory back to the OS, so it thrashes until the JVM process is restarted (not fine). –  Brendan Long Jun 12 '12 at 14:24
