CLR – Purpose of the Common Language Runtime

\clrvirtual machine

My understanding is that part of the point of the JVM was that the code could "run anywhere", but CLR code was designed to run only on Windows: so why have a virtual machine?

I know that the CLR runtime takes care of garbage collection, but there are garbage-collected languages that still compile to native code.

The only thing I can think of is that Microsoft may have wanted leverage in order to keep the price of Intel chips low.

Best Answer

My understanding is that part of the point of the JVM was that the code could "run anywhere", but CLR code was designed to run only on Windows

"Windows" is not a synonym for "x86" - even back in the late-1990s Windows ran on DEC Alpha, MIPS, x86, and even PowerPC and by the early-2000s when .NET 1.1 was released Windows also supported IA-64 (Itanium) and AMD64 was on the horizon, while Windows CE (the then-dominant smart-device OS) ran on x86, ARM, SH-3 and MIPS. The case for a cross-platform, JIT-compiled runtime exclusively for Windows was (and remains) still quite convincing.

Granted, we can only speculate on the reasoning behind the CLR's development, but many strong reasons can be deduced from history:

Java was the first multiplatform VM (and library, complete with GUI and web-server packages) to take-off - but Microsoft didn't control it and their attempts at extending the platform for their benefit didn't work out (see "J++") - if Microsoft couldn't capture the new generation of Java programmers (who saw VB as below them, and who didn't want to mess-around with C/C++) then they would lose control of the desktop software ecosystem: why buy Windows if desktop software works fine on Mac, Solaris, etc?

On the server-side, "Classic" ASP 3.0 was getting upstaged by PHP and JSP - Microsoft could have invested time in improving ASP separately, but they decided to make ASP.NET (then called "ASP+") an integral part of the .NET Framework too: this was a boon for developers because it enabled object-level code-reuse, something that was previously impossible (e.g. using C++ classes in VB) or very difficult (using COM/DCOM).

So the CLR offers 3 main benefits over the contemporary status-quo:

  • Cross-platform support that enables the same compiled binary to run on different physical architectures and operating systems (x86 vs ARM, Desktop vs Handheld)
  • A platform more sophisticated than VB, without the hassle and learning-curve of C/C++
  • True code reuse

And a number of proprietary advantages for Microsoft:

  • Provides a competitive-offering compared to Java in an effort to keep itself relevant in the developer ecosystem
  • By controlling the platform, they ensure vendor lock-in - but pragmatically it means they can ensure it is kept updated and in lock-step with the underlying platform. It sucks when something cool comes along (e.g. hardware-accelerated cryptography) but it won't be supported for months, possibly years, in Java because of the effort required to get everyone on-board and to add support (possibly emulation) for platforms that don't support it.
Related Topic