Why the complete web moved from C++ to Java ?





Early to Mid 2000 I worked on many web applications created in C++ but as the time progressed , all those applications sooner or later got moved to Java. I always wondered Why ? Why didn't they changed C++ so that it becomes equally efficient as Java. 

I believe i am somewhere close to the answer. The answer is Memory Management

Upon web Requests, All Web application create threads , allocate memory to those threads and then deallocate memory when the request is fulfilled thus involving lot of memory allocation and deallocation.

In C++ , the coder manually allocates and deallocates memory so making the memory management vulnerable. Moreover there is no automatic way for memory deallocation ( Releasing the unused memory ). Thats why we used to waste hell lot time on finding memory leaks using tools like purify, dbx etc so that we can resolve the code problems resulting in those leaks. Till the time leaks are identified , keep doing the server restart whenever the application runs out of memory. 

Java provided the answer to this issue by mean of Garbage collection. it handles deallocation for you automatically. It works like this: when no references to an object exist, that object is assumed to be no longer needed, and the memory occupied by the object can be reclaimed. There is no explicit need to destroy objects as in C++.

So now the question is Why didnt C++ moved to this mechanism of memory management ? The following is the answer by Bjarne Stroustrup himself 

I had hoped that a garbage collector which could be optionally enabled would be part of C++0x, but there were enough technical problems that I have to make do with just a detailed specification of how such a collector integrates with the rest of the language, if provided. As is the case with essentially all C++0x features, an experimental implementation exists.

Moreover , C++ is very powerful and allows you to do almost anything. For this reason it doesn't automatically push many things onto you that might impact performance. So it gives you flexibility for the applications that require explicit memory management for applications involving transaction processing , real time processing.

So the answer is that both C++ and Java are powerful languages and caters to different needs -

1. Java is helpful in creating Applications involving requests and threads ( Web , intranet ) as it relieves the user from the problems and hassles of memory management.

2. C++ is useful in creating standalone applications , creating systems ( requiring explicit memory management ) and Real Time Systems ( due to problem of stalled memory with implicit garbage collection).

Hope it answers.